-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvex_hull.java
More file actions
188 lines (161 loc) · 5.65 KB
/
convex_hull.java
File metadata and controls
188 lines (161 loc) · 5.65 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
/*
Andrew's monotone chain algorithm for computing the convex hull of 2D points.
Computes the convex hull (smallest convex polygon containing all points) using
a simple and robust algorithm.
Key operations:
- convexHull(points): Returns convex hull vertices in counter-clockwise order
Time complexity: O(n log n) dominated by sorting
Space complexity: O(n)
*/
import java.util.*;
class convex_hull {
static class Point implements Comparable<Point> {
double x, y;
Point(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Point other) {
if (this.x != other.x) {
return Double.compare(this.x, other.x);
}
return Double.compare(this.y, other.y);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Point)) return false;
Point p = (Point) obj;
return this.x == p.x && this.y == p.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
static double cross(Point o, Point a, Point b) {
return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
}
static List<Point> convexHull(List<Point> points) {
if (points.size() <= 1) {
return new ArrayList<>(points);
}
List<Point> sorted = new ArrayList<>(points);
Collections.sort(sorted);
List<Point> lower = new ArrayList<>();
for (Point p : sorted) {
while (lower.size() >= 2
&& cross(lower.get(lower.size() - 2), lower.get(lower.size() - 1), p) <= 0) {
lower.remove(lower.size() - 1);
}
lower.add(p);
}
List<Point> upper = new ArrayList<>();
for (int i = sorted.size() - 1; i >= 0; i--) {
Point p = sorted.get(i);
while (upper.size() >= 2
&& cross(upper.get(upper.size() - 2), upper.get(upper.size() - 1), p) <= 0) {
upper.remove(upper.size() - 1);
}
upper.add(p);
}
lower.remove(lower.size() - 1);
upper.remove(upper.size() - 1);
lower.addAll(upper);
return lower;
}
static void testMain() {
List<Point> pts =
Arrays.asList(
new Point(0, 0),
new Point(1, 0),
new Point(1, 1),
new Point(0, 1),
new Point(0.5, 0.5));
List<Point> hull = convexHull(pts);
assert hull.size() == 4;
assert hull.contains(new Point(0, 0));
assert hull.contains(new Point(1, 0));
assert hull.contains(new Point(1, 1));
assert hull.contains(new Point(0, 1));
assert !hull.contains(new Point(0.5, 0.5));
}
// Don't write tests below during competition.
static void testEmpty() {
assert convexHull(new ArrayList<>()).isEmpty();
}
static void testSinglePoint() {
List<Point> pts = Arrays.asList(new Point(1, 2));
List<Point> hull = convexHull(pts);
assert hull.size() == 1;
}
static void testTwoPoints() {
List<Point> pts = Arrays.asList(new Point(0, 0), new Point(1, 1));
List<Point> hull = convexHull(pts);
assert hull.size() == 2;
}
static void testCollinearPoints() {
List<Point> pts =
Arrays.asList(
new Point(0, 0), new Point(1, 1),
new Point(2, 2), new Point(3, 3));
List<Point> hull = convexHull(pts);
assert hull.size() == 2;
assert hull.contains(new Point(0, 0));
assert hull.contains(new Point(3, 3));
}
static void testTriangle() {
List<Point> pts = Arrays.asList(new Point(0, 0), new Point(2, 0), new Point(1, 2));
List<Point> hull = convexHull(pts);
assert hull.size() == 3;
}
static void testWithInteriorPoints() {
List<Point> pts =
Arrays.asList(
new Point(0, 0),
new Point(4, 0),
new Point(4, 3),
new Point(2, 4),
new Point(0, 3),
new Point(2, 2),
new Point(2, 1));
List<Point> hull = convexHull(pts);
assert hull.size() == 5;
assert !hull.contains(new Point(2, 2));
assert !hull.contains(new Point(2, 1));
}
static void testNegativeCoordinates() {
List<Point> pts =
Arrays.asList(
new Point(-1, -1), new Point(1, -1),
new Point(1, 1), new Point(-1, 1));
List<Point> hull = convexHull(pts);
assert hull.size() == 4;
}
static void testLargeSquare() {
List<Point> pts = new ArrayList<>();
for (int i = 0; i <= 10; i++) {
for (int j = 0; j <= 10; j++) {
pts.add(new Point(i, j));
}
}
List<Point> hull = convexHull(pts);
assert hull.size() == 4;
assert hull.contains(new Point(0, 0));
assert hull.contains(new Point(10, 0));
assert hull.contains(new Point(10, 10));
assert hull.contains(new Point(0, 10));
}
public static void main(String[] args) {
testMain();
testEmpty();
testSinglePoint();
testTwoPoints();
testCollinearPoints();
testTriangle();
testWithInteriorPoints();
testNegativeCoordinates();
testLargeSquare();
System.out.println("All tests passed!");
}
}