1
+ import java .io .*;
2
+ import java .util .*;
3
+ public class cowjump {
4
+ public static int pointYCompare (Point p1 , Point p2 ) {
5
+ // Compare points
6
+ // Check if p2 is above, below, or next to p1
7
+ System .out .println ("pointycompare((" +p1 .x +"," +p1 .y +"),(" +p2 .x +"," +p2 .y +")" );
8
+ if (p1 .y == p2 .y ) {
9
+ System .out .println ("output: 0" ); // Same Y
10
+ return 0 ;
11
+ }
12
+ if (p2 .y < p1 .y ) {
13
+ System .out .println ("output: -1" ); // below
14
+ return -1 ;
15
+ }
16
+ if (p2 .y > p1 .y ) {
17
+ System .out .println ("output: 1" ); // above
18
+ return 1 ;
19
+ }
20
+ System .out .println ("NONE OF THE ABOVE" ); // This code should never run, but just keeping this to prevent syntax errors
21
+ return -9999999 ;
22
+ }
23
+ public static int linesCompare (LineSegement m , LineSegement l ) {
24
+ int output = pointYCompare (m .a , l .a ) * pointYCompare (m .b , l .b );;
25
+ System .out .println ("Intersection of line " +m +" and " +l + " is " +output + " == -1" );
26
+ return output ;
27
+ }
28
+ public static void testIntersections () {
29
+ assert Point .intersection (new Point (0 ,0 ), new Point (2 ,9 ), new Point (0 ,1 ), new Point (6 ,1 ))== true ;
30
+ //assert Point.intersection(new Point(0,0), new Point(1,1), new Point(3,3), new Point(3,12))== false;
31
+ assert linesCompare (new LineSegement (new Point (0 ,0 ), new Point (2 ,3 )),new LineSegement (new Point (0 ,3 ),new Point (9 ,1 ))) == -1 ;
32
+ System .out .println ("All Tests OK!" );
33
+ }
34
+
35
+ public static boolean sweepCheck (LineSegement s ,Point [][] input ) {
36
+ for (int i = 0 ; i < input .length ; i ++) {
37
+ System .out .println ("Checking line " +i );
38
+ if (input [i ][0 ] == null && input [i ][1 ] == null ) {
39
+ System .out .println ("End of segments" );
40
+ break ;
41
+ }
42
+ System .out .println ("Checking " +s .a .x +" - " +input [i ][0 ].x + " - " +s .b .x );
43
+ boolean firstWithinLine = (s .a .x <= input [i ][0 ].x && s .b .x >= input [i ][0 ].x );
44
+ System .out .println ("(s.a.x <= input[i][0].x && s.b.x >= input[i][0].x)" );
45
+ System .out .println ((s .a .x <= input [i ][0 ].x )+" && " + (s .b .x >= input [i ][0 ].x ));
46
+ System .out .println ("firstWithinLine = " +firstWithinLine );
47
+ //|| (s.a.y <= input[i][0].y && s.b.y >= input[i][0].y);
48
+ if (firstWithinLine ) {
49
+ // TODO check line cross logic
50
+ LineSegement full = new LineSegement (input [i ][0 ], input [i ][1 ]);
51
+ if (linesCompare (s ,new LineSegement (full .atX_ (s .a .x ),full .atX_ (s .a .y ))) == -1 ) {
52
+ System .out .println ("Intersect!" );
53
+ return true ;
54
+ }else {
55
+ System .out .println ("No Intersection!" );
56
+ }
57
+ //continue; // Both statements may be true
58
+ }
59
+ boolean secondWithinLine = (s .a .x <= input [i ][1 ].x && s .b .x >= input [i ][1 ].x );
60
+ //|| (s.a.y <= input[i][0].y && s.b.y >= input[i][0].y);
61
+ if (secondWithinLine && !firstWithinLine ) {
62
+ // TODO check line cross logic
63
+ LineSegement full = new LineSegement (input [i ][0 ], input [i ][1 ]);
64
+ if (linesCompare (s ,new LineSegement (full .atX_ (s .a .x ), full .atX_ (s .b .x ))) == -1 ) {
65
+ System .out .println ("Intersect!" );
66
+ return true ;
67
+ }else {
68
+ System .out .println ("No Intersection!" );
69
+ }
70
+ }
71
+
72
+ }
73
+ return false ;
74
+ }
75
+ public static void main (String [] args ) throws IOException {
76
+ //testIntersections();
77
+ //testIntersections();
78
+ BufferedReader f = new BufferedReader (new FileReader ("cowjump2.in" ));
79
+ int N = Integer .parseInt (f .readLine ());
80
+ Point [][] input = new Point [N ][2 ];
81
+ //System.out.println(input[0][0]);
82
+ int output = -1 ;
83
+ for (int i = 0 ; i < N ; i ++) {
84
+ StringTokenizer st = new StringTokenizer (f .readLine ());
85
+ Point a = new Point (Integer .parseInt (st .nextToken ()), Integer .parseInt (st .nextToken ()));
86
+ Point b = new Point (Integer .parseInt (st .nextToken ()), Integer .parseInt (st .nextToken ()));
87
+ boolean status ;
88
+ if (a .x > b .x ) {
89
+ status = sweepCheck (
90
+ new LineSegement (b ,a ),
91
+ input );
92
+ }else {
93
+ status = sweepCheck (
94
+ new LineSegement (a ,b ),
95
+ input );
96
+ }
97
+ System .out .println ("That was line " +i );
98
+ output = i ;
99
+ if (status ) {
100
+ break ;
101
+ }
102
+
103
+ input [i ][0 ] = a ;
104
+ input [i ][1 ] = b ;
105
+ /*for(int j = 0; j < i; j ++) {
106
+ if(Point.intersection(input[j][0], input[j][1], input[i][0], input[i][1])) {
107
+ PrintWriter pw = new PrintWriter("cowjump.out");
108
+ pw.println(i+1);
109
+ pw.close();
110
+ System.exit(0);
111
+ }
112
+ }*/
113
+ }
114
+ f .close ();
115
+ PrintWriter pw = new PrintWriter ("cowjump.out" );
116
+ pw .println (output +1 );
117
+ pw .close ();
118
+ System .exit (0 );
119
+
120
+ }
121
+
122
+ }
123
+ class Point {
124
+ double x ,y ;
125
+ public Point (double x ,double y ) {
126
+ this .x = x ;
127
+ this .y = y ;
128
+ }
129
+ public Point (int x ,int y ) {
130
+ this .x = x ;
131
+ this .y = y ;
132
+ }
133
+ public Point () {
134
+ this .x = 0 ;
135
+ this .y = 0 ;
136
+ }
137
+ static boolean intersection (Point a , Point b ,Point c , Point d ) {
138
+ // OLD CALCULATION CODE
139
+ Point E = new Point (b .x - a .x , b .y - a .y );
140
+ Point F = new Point (d .x - c .x , d .y - c .y );
141
+ Point P = new Point (-E .y , E .x );
142
+ Point Q = new Point (a .x - c .x , a .y - c .y );
143
+ double k = F .x * P .x + F .y * P .y ;
144
+ if (k == 0 ) {
145
+ // Parallel
146
+ return false ;
147
+ }
148
+ double h = (Q .x * P .x + Q .y * P .y )/(k );
149
+ if (0 <= h && h <= 1 ) {
150
+ return true ;
151
+ }
152
+ return false ;
153
+ }
154
+ public String toString () {
155
+ return "(" +this .x + "," + this .y + ")" ;
156
+ }
157
+ }
158
+
159
+ class LineSegement {
160
+ Point a ,b ;
161
+ public LineSegement (Point a ,Point b ) {
162
+ if (a .x > b .x ) {
163
+ this .a = b ;
164
+ this .b = a ;
165
+ }else {
166
+ this .a = a ;
167
+ this .b = b ;
168
+ }
169
+ }
170
+ public double atX (double x ) {
171
+ if (this .a .y == this .b .y ) { // Straight
172
+ return this .a .y ;
173
+ }else {
174
+ return this .a .y * (x /this .a .x );
175
+ }
176
+ }
177
+ public Point atX_ (double x ) {
178
+ return new Point (x ,this .atX (x ));
179
+ }
180
+ public String toString () {
181
+ return this .a .toString () + " -- " +this .b .toString ();
182
+ }
183
+ }
0 commit comments