Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit ac1d366

Browse files
authored
Add files via upload
1 parent d0eef15 commit ac1d366

File tree

1 file changed

+98
-5
lines changed

1 file changed

+98
-5
lines changed

cowjump.java

+98-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,73 @@
11
import java.io.*;
22
import java.util.*;
33
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+
System.out.println("Checking if line ("+m.a.x+","+m.a.y+")--"+"("+m.b.x+","+m.b.y+")" + " intersects with " + "("+l.a.x+","+l.a.y+")--"+"("+l.b.x+","+l.b.y+")");
25+
return pointYCompare(m.a, l.a) * pointYCompare(m.b, l.b);
26+
}
427
public static void testIntersections() {
528
assert Point.intersection(new Point(0,0), new Point(2,9), new Point(0,1), new Point(6,1))== true;
6-
assert Point.intersection(new Point(0,0), new Point(1,1), new Point(3,3), new Point(3,12))== false;
29+
//assert Point.intersection(new Point(0,0), new Point(1,1), new Point(3,3), new Point(3,12))== false;
30+
assert linesCompare(new LineSegement(new Point(0,0), new Point(2,3)),new LineSegement(new Point(0,3),new Point(9,1))) == -1;
731
System.out.println("All Tests OK!");
832
}
33+
public static boolean sweepCheck(LineSegement s,Point[][] input) {
34+
for(int i = 0; i < input.length; i ++) {
35+
System.out.println("Checking line "+i);
36+
if(input[i][0] == null || input[i][1] == null) {
37+
System.out.println("End of segments");
38+
break;
39+
}
40+
System.out.println("Checking "+s.a.x+" - "+input[i][0].x + " - "+s.b.x);
41+
boolean firstWithinLine = (s.a.x <= input[i][0].x && s.b.x >= input[i][0].x);
42+
System.out.println("firstWithinLine = "+firstWithinLine);
43+
//|| (s.a.y <= input[i][0].y && s.b.y >= input[i][0].y);
44+
if(firstWithinLine) {
45+
// TODO check line cross logic
46+
if(linesCompare(s,new LineSegement(input[i][0], input[i][1])) == -1) {
47+
System.out.println("Intersect!");
48+
return true;
49+
}else {
50+
System.out.println("No Intersection!");
51+
}
52+
continue; // Both statements may be true
53+
}
54+
boolean secondWithinLine = (s.a.x <= input[i][1].x && s.b.x >= input[i][1].x);
55+
//|| (s.a.y <= input[i][0].y && s.b.y >= input[i][0].y);
56+
if(secondWithinLine) {
57+
// TODO check line cross logic
58+
if(linesCompare(s,new LineSegement(input[i][0], input[i][1])) == -1) {
59+
System.out.println("Intersect!");
60+
return true;
61+
}else {
62+
System.out.println("No Intersection!");
63+
}
64+
}
65+
66+
}
67+
return false;
68+
}
969
public static void main(String[] args) throws IOException{
70+
testIntersections();
1071
// TODO Auto-generated method stub
1172
//testIntersections();
1273
BufferedReader f = new BufferedReader(new FileReader("cowjump.in"));
@@ -15,17 +76,29 @@ public static void main(String[] args) throws IOException{
1576
//System.out.println(input[0][0]);
1677
for(int i = 0; i < N; i ++) {
1778
StringTokenizer st = new StringTokenizer(f.readLine());
18-
input[i][0] = new Point(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
19-
input[i][1] = new Point(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
20-
for(int j = 0; j < i; j ++) {
79+
Point a = new Point(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
80+
Point b = new Point(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
81+
if(a.x > b.x) {
82+
System.out.println(sweepCheck(
83+
new LineSegement(b,a),
84+
input));
85+
}else {
86+
System.out.println(sweepCheck(
87+
new LineSegement(a,b),
88+
input));
89+
}
90+
input[i][0] = a;
91+
input[i][1] = b;
92+
/*for(int j = 0; j < i; j ++) {
2193
if(Point.intersection(input[j][0], input[j][1], input[i][0], input[i][1])) {
2294
PrintWriter pw = new PrintWriter("cowjump.out");
2395
pw.println(i+1);
2496
pw.close();
2597
System.exit(0);
2698
}
27-
}
99+
}*/
28100
}
101+
29102
}
30103

31104
}
@@ -56,4 +129,24 @@ static boolean intersection(Point a, Point b,Point c, Point d) {
56129
}
57130
return false;
58131
}
132+
}
133+
134+
class LineSegement {
135+
Point a,b;
136+
public LineSegement(Point a,Point b) {
137+
if(a.x > b.x) {
138+
this.a = b;
139+
this.b = a;
140+
}else {
141+
this.a = a;
142+
this.b = b;
143+
}
144+
}
145+
public double atX(int x) {
146+
if(this.a.y == this.b.y) { // Straight
147+
return this.a.y;
148+
}else {
149+
return this.a.y * (x/this.a.x);
150+
}
151+
}
59152
}

0 commit comments

Comments
 (0)