-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConvexHull.java
More file actions
91 lines (80 loc) · 2.21 KB
/
ConvexHull.java
File metadata and controls
91 lines (80 loc) · 2.21 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
package own;
import java.util.Arrays;
/*
* Computing the convex hull of a set of points using
* the Graham scan algorithm.
* Application: find the two points with biggest euclidean distance between
* them given a set of points.
* http://en.wikipedia.org/wiki/Graham_scan
*/
public class ConvexHull {
private int n;
private Point[] points;
public ConvexHull(Point[] points) {
this.n = points.length;
this.points = points;
}
private Point getPointWithLowestYCoord() {
Point lowest_point = points[0];
for (int i = 1; i < n; i++) {
if (points[i].getY() < lowest_point.getY()) {
lowest_point = points[i];
} else if (points[i].getY() == lowest_point.getY()) {
if (points[i].getX() > lowest_point.getX())
lowest_point = points[i];
}
}
return lowest_point;
}
private int computeConvexHull() {
if (n <= 2)
return n;
Point point_with_lowest_y_coord = getPointWithLowestYCoord();
for (int i = 0; i < n; i++) {
points[i].setComparatorPoint(point_with_lowest_y_coord);
}
Arrays.sort(points);
Point[] mod_points = new Point[n + 1];
for (int i = 0; i < n; i++) {
mod_points[i] = points[i];
}
mod_points[n] = points[0];
points = mod_points;
int convex_hull_index = 1;
int i = 2;
while (i <= n) {
// decide if an angle is counterclockwise or not
// if it is not counterclockwise, do not include it in the
// convex hull
while (!Point.isCounterclockwise(points[convex_hull_index - 1],
points[convex_hull_index], points[i])) {
if (convex_hull_index > 1)
convex_hull_index--;
else if (i == n)
// all points are collinear
break;
else
i++;
}
convex_hull_index++;
swap(points, convex_hull_index, i);
i++;
}
return convex_hull_index;
}
public Point[] getConvexHull() {
int convex_hull_index = computeConvexHull();
Point[] convex_hull_points = new Point[convex_hull_index];
for (int i = 0; i < convex_hull_index; i++) {
convex_hull_points[i] = points[i];
}
return convex_hull_points;
}
private void swap(Point[] points, int index1, int index2) {
assert (index1 < points.length);
assert (index2 < points.length);
Point aux = points[index1];
points[index1] = points[index2];
points[index2] = aux;
}
}