GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/cowjump.java at master · javaarchive/Java
This repository was archived by the owner on Feb 29, 2024. It is now read-only.
Expand file tree/ Copy pathcowjump.javaMore file actions 394 lines (371 loc) · 11.2 KB / Copy pathcowjump.javaFile metadata and controls394 lines (371 loc) · 11.2 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 import java.io.*; import java.util.*; public class cowjump { public static int pointYCompare(Point p1, Point p2) { // Compare points // Check if p2 is above, below, or next to p1 System.out.println("pointycompare(("+p1.x+","+p1.y+"),("+p2.x+","+p2.y+")"); if(p1.y == p2.y) { System.out.println("output: 0"); // Same Y return 0; } if(p2.y < p1.y) { System.out.println("output: -1"); // below return -1; } if(p2.y > p1.y) { System.out.println("output: 1"); // above return 1; } System.out.println("NONE OF THE ABOVE"); // This code should never run, but just keeping this to prevent syntax errors return -9999999; } public static int linesCompare(LineSegement m, LineSegement l) { int output = pointYCompare(m.a, l.a) * pointYCompare(m.b, l.b);; System.out.println("Intersection of line "+m+" and "+l + " is "+output + " == -1"); return output; } public static void testIntersections() { assert Point.intersection(new Point(0,0), new Point(2,9), new Point(0,1), new Point(6,1))== true; //assert Point.intersection(new Point(0,0), new Point(1,1), new Point(3,3), new Point(3,12))== false; assert linesCompare(new LineSegement(new Point(0,0), new Point(2,3)),new LineSegement(new Point(0,3),new Point(9,1))) == -1; System.out.println("All Tests OK!"); } public static boolean sweepCheck(LineSegement s,Point[][] input) { for(int i = 0; i < input.length; i ++) { System.out.println("Checking line "+i); if(input[i][0] == null && input[i][1] == null) { System.out.println("End of segments"); break; } System.out.println("Checking "+s.a.x+" - "+input[i][0].x + " - "+s.b.x); boolean firstWithinLine = (s.a.x <= input[i][0].x && s.b.x >= input[i][0].x); System.out.println("(s.a.x <= input[i][0].x && s.b.x >= input[i][0].x)"); System.out.println((s.a.x <= input[i][0].x)+" && " + (s.b.x >= input[i][0].x)); System.out.println("firstWithinLine = "+firstWithinLine); //|| (s.a.y <= input[i][0].y && s.b.y >= input[i][0].y); if(firstWithinLine) { // TODO check line cross logic LineSegement full = new LineSegement(input[i][0], input[i][1]); if(linesCompare(s,new LineSegement(full.atX_(s.a.x),full.atX_(s.a.y))) == -1) { System.out.println("Intersect!"); return true; }else { System.out.println("No Intersection!"); } //continue; // Both statements may be true } boolean secondWithinLine = (s.a.x <= input[i][1].x && s.b.x >= input[i][1].x); //|| (s.a.y <= input[i][0].y && s.b.y >= input[i][0].y); if(secondWithinLine && !firstWithinLine) { // TODO check line cross logic LineSegement full = new LineSegement(input[i][0], input[i][1]); if(linesCompare(s,new LineSegement(full.atX_(s.a.x), full.atX_(s.b.x))) == -1) { System.out.println("Intersect!"); return true; }else { System.out.println("No Intersection!"); } } } return false; } public static void main(String[] args) throws IOException{ //testIntersections(); //testIntersections(); BufferedReader f = new BufferedReader(new FileReader("cowjump2.in")); int N = Integer.parseInt(f.readLine()); Point[][] input = new Point[N][2]; //System.out.println(input[0][0]); int output = -1; for(int i = 0; i < N; i ++) { StringTokenizer st = new StringTokenizer(f.readLine()); <<<<<<< HEAD <<<<<<< HEAD <<<<<<< HEAD <<<<<<< HEAD Point a = new Point(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())); Point b = new Point(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())); boolean status; if(a.x > b.x) { status = sweepCheck( new LineSegement(b,a), input); }else { status = sweepCheck( new LineSegement(a,b), input); } System.out.println("That was line "+i); output = i; if(status) { break; } input[i][0] = a; input[i][1] = b; /*for(int j = 0; j < i; j ++) { if(Point.intersection(input[j][0], input[j][1], input[i][0], input[i][1])) { PrintWriter pw = new PrintWriter("cowjump.out"); pw.println(i+1); pw.close(); System.exit(0); ======= ======= >>>>>>> parent of c6da410... Long Proofing ======= >>>>>>> parent of c6da410... Long Proofing ======= >>>>>>> parent of c6da410... Long Proofing a = new Point(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken())); b = new Point(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken())); a = a.setIndex(i); b = b.setIndex(i); if(a.compareTo(b) == 1) { lookup[i][0] = b; lookup[i][1] = a; }else { lookup[i][0] = a; lookup[i][1] = b; } points.add(a); points.add(b); segements.add(new LineSegement(a, b)); } f.close(); points.sort(null); // Algorthim int size = points.size(); for(int i = 0; i < size; i ++) { System.out.println(points.get(i)); } for(int i =0 ; i < N; i ++) { System.out.println(Arrays.toString(lookup[i])); System.out.println(segements.get(i)); } int currentY = -1; int index; // boolean newY = true; List<Integer> pz = new ArrayList<Integer>(); int[] tbl = new int[N]; int max = -1; int maxi = -1; for(int i = 0 ; i < N; i ++) { Point p = points.get(i); //index = p.index; /*if(p.y != currentY) { currentY = (int) p.y; continue; }else { */ int state = num(p.index,p,lookup); System.out.println("Endpoint "+ state + " "+p); if(state == 0) { pz.add(p.index); int sz = pz.size() - 1; LineSegement newline = segements.get(p.index); for(int j =0 ;j < sz; j ++ ) { if(Point.intersection(newline,segements.get(pz.get(j)))) { System.out.println("Intersectsion at "+segements.get(p.index)+" -|||- "+segements.get(j)); tbl[i] ++; tbl[j] ++; if(tbl[i] > max) { max = tbl[i]; maxi = i; } if(tbl[j] > max) { max = tbl[j]; maxi = j; } } } }else if(state == 1) { pz.remove(pz.indexOf(p.index)); }else { continue; >>>>>>> parent of c6da410... Long Proofing } }*/ } f.close(); PrintWriter pw = new PrintWriter("cowjump.out"); pw.println(output+1); pw.close(); System.exit(0); } <<<<<<< HEAD <<<<<<< HEAD } } ======= } >>>>>>> parent of 84bd17b... Merge branch 'master' of https://github.com/javaarchive/Java class Point{ double x,y; ======= } class Point implements Comparable<Point>{ double x,y; int index = -1; <<<<<<< HEAD <<<<<<< HEAD <<<<<<< HEAD >>>>>>> parent of c6da410... Long Proofing ======= >>>>>>> parent of c6da410... Long Proofing ======= >>>>>>> parent of c6da410... Long Proofing ======= >>>>>>> parent of c6da410... Long Proofing public Point(double x,double y) { this.x = x; this.y = y; } public Point(int x,int y) { this.x = x; this.y = y; } public Point() { this.x = 0; this.y = 0; } static boolean intersection(Point a, Point b,Point c, Point d) { // OLD CALCULATION CODE Point E = new Point(b.x - a.x, b.y - a.y); Point F = new Point(d.x - c.x, d.y - c.y); Point P = new Point(-E.y, E.x); Point Q = new Point(a.x - c.x, a.y - c.y); double k = F.x * P.x + F.y * P.y; if(k == 0) { // Parallel return false; } double h = (Q.x * P.x + Q.y * P.y)/(k); if(0 <= h && h <= 1) { return true; } return false; } <<<<<<< HEAD public String toString() { return "("+this.x + ","+ this.y + ")"; ======= // Given three colinear points p, q, r, the function checks if // point q lies on line segment 'pr' static boolean onSegment(Point p, Point q, Point r) { if (q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y)) return true; return false; } // To find orientation of ordered triplet (p, q, r). // The function returns following values // 0 --> p, q and r are colinear // 1 --> Clockwise // 2 --> Counterclockwise static int orientation(Point p, Point q, Point r) { // See https://www.geeksforgeeks.org/orientation-3-ordered-points/ // for details of below formula. double val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); if (val == 0) return 0; // colinear return (val > 0)? 1: 2; // clock or counterclock wise } // The main function that returns true if line segment 'p1q1' // and 'p2q2' intersect. static boolean intersection(Point p1, Point q1, Point p2, Point q2) { // Find the four orientations needed for general and // special cases int o1 = orientation(p1, q1, p2); int o2 = orientation(p1, q1, q2); int o3 = orientation(p2, q2, p1); int o4 = orientation(p2, q2, q1); // General case if (o1 != o2 && o3 != o4) return true; // Special Cases // p1, q1 and p2 are colinear and p2 lies on segment p1q1 if (o1 == 0 && onSegment(p1, p2, q1)) return true; // p1, q1 and q2 are colinear and q2 lies on segment p1q1 if (o2 == 0 && onSegment(p1, q2, q1)) return true; // p2, q2 and p1 are colinear and p1 lies on segment p2q2 if (o3 == 0 && onSegment(p2, p1, q2)) return true; // p2, q2 and q1 are colinear and q1 lies on segment p2q2 if (o4 == 0 && onSegment(p2, q1, q2)) return true; return false; // Doesn't fall in any of the above cases } static Set<Pair<LineSegement,LineSegement>> notwice = new HashSet<Pair<LineSegement,LineSegement>>(); static boolean intersection(LineSegement a,LineSegement b) { if(notwice.contains(new Pair<LineSegement,LineSegement>(a,b))) { return false; }else { notwice.add(new Pair<LineSegement,LineSegement>(a,b)); } return intersection(a.a, a.b, b.a, b.b); } public String toString() { return "("+this.x + ","+ this.y + ","+this.index+")"; } boolean eq(Point q) { if(q.x == this.x && q.y == this.y) { return true; } return false; } @Override public int compareTo(Point arg0) { if(arg0.x == this.x) { return Double.compare(this.y, this.y); }else if(this.x > arg0.x) { return -1; }else { return 1; } //return Double.compare(this.x, arg0.x); //return 0; >>>>>>> parent of c6da410... Long Proofing } } class LineSegement { Point a,b; public LineSegement(Point a,Point b) { if(a.x > b.x) { this.a = b; this.b = a; }else { this.a = a; this.b = b; } } public double atX(double x) { if(this.a.y == this.b.y) { // Straight return this.a.y; }else { return this.a.y * (x/this.a.x); } } <<<<<<< HEAD <<<<<<< HEAD <<<<<<< HEAD <<<<<<< HEAD public Point atX_(double x) { return new Point(x,this.atX(x)); } ======= ======= >>>>>>> parent of c6da410... Long Proofing ======= >>>>>>> parent of c6da410... Long Proofing ======= >>>>>>> parent of c6da410... Long Proofing public Point atX_(double x) { return new Point(x,this.atX(x)); } /* >>>>>>> parent of c6da410... Long Proofing public String toString() { return this.a.toString() + " -- "+this.b.toString(); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |