-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.java
109 lines (79 loc) · 1.85 KB
/
Test.java
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class A {
public int a = 1;
}
class B extends A {
}
class Point1 {
public int x, y;
public Point1(int a, int b) {
x = a;
y = b;
}
}
public class Test {
/**
* @param args
*/
public int direction(Point1 a, Point1 b , Point1 k) {
int res = (a.x - k.x) * (b.y - k.y) -( a.y - k.y ) * (b.x - k.x);
if (res < 0)
return -1;
if (res >0)
return 1;
return 0;
}
public boolean onSegment(Point1 a, Point1 b, Point1 k) {
int minx, miny, maxx, maxy;
if (a.x > b.x) {
minx = b.x;
maxx = a.x;
}
else {
minx = a.x;
maxx = b.x;
}
if (a.y > b.y) {
miny = b.y;
maxy = a.y;
}
else {
miny = a.y;
maxy = b.y;
}
if (k.x >= minx && k.x <= maxx && k.y >= miny && k.y <= maxy)
return true;
return false;
}
public boolean segmentIntersect(Point1 p1, Point1 p2, Point1 p3, Point1 p4) {
int d1 = direction(p1, p2, p3);
int d2 = direction(p1, p2, p4);
int d3 = direction(p3, p4, p1);
int d4 = direction(p3, p4, p2);
if (d1*d2 < 0 && d3*d4 < 0)
return true;
if (d1 == 0 && onSegment(p1, p2, p3))
return true;
if(d2 == 0 && onSegment(p1, p2, p4))
return true;
if(d3 ==0 && onSegment(p3, p4, p1))
return true;
if(d4 ==0 && onSegment(p3, p4, p2))
return true;
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// System.out.println(t.segmentIntersect(new Point(0,0), new Point(1,1), new Point(0,1), new Point(1,0)));
//
// System.out.println(t.segmentIntersect(new Point(2,2), new Point(1,1), new Point(3,0), new Point(3,8)));
//
// System.out.println(t.segmentIntersect(new Point(0,0), new Point(1,1), new Point(0,0), new Point(5,7)));
}
}
class C extends A {
}