|
3 | 3 | import java.util.Arrays;
|
4 | 4 | import java.util.Comparator;
|
5 | 5 | import java.util.Stack;
|
| 6 | +import java.util.ArrayList; |
6 | 7 |
|
7 | 8 | /**
|
8 | 9 | * A Java program that computes the convex hull using the Graham Scan algorithm.
|
|
16 | 17 | * https://algs4.cs.princeton.edu/99hull/GrahamScan.java.html
|
17 | 18 | */
|
18 | 19 | public class GrahamScan {
|
| 20 | + |
19 | 21 | private final Stack<Point> hull = new Stack<>();
|
| 22 | + |
20 | 23 | public GrahamScan(Point[] points) {
|
21 | 24 | // Pre-process points: sort by y-coordinate, then by polar order with respect to the first point
|
22 | 25 | Arrays.sort(points);
|
23 | 26 | Arrays.sort(points, 1, points.length, points[0].polarOrder());
|
| 27 | + |
24 | 28 | hull.push(points[0]);
|
25 |
| - // Find the first point not equal to points[0]/firstNonEqualIndex and the first point not collinear firstNonCollinearIndex with the previous points |
26 |
| - int firstNonEqualIndex; |
27 |
| - for (firstNonEqualIndex = 1; firstNonEqualIndex < points.length; firstNonEqualIndex++) { |
28 |
| - if (!points[0].equals(points[firstNonEqualIndex])) { |
29 |
| - break; |
| 29 | + |
| 30 | + // Find the first point not equal to points[0]/firstNonEqualIndex |
| 31 | + // and the first point not collinear firstNonCollinearIndex with the previous points |
| 32 | + int firstNonEqualIndex; |
| 33 | + for (firstNonEqualIndex = 1; firstNonEqualIndex < points.length; firstNonEqualIndex++) { |
| 34 | + if (!points[0].equals(points[firstNonEqualIndex])) { |
| 35 | + break; |
| 36 | + } |
30 | 37 | }
|
31 |
| - } |
32 |
| - if (firstNonEqualIndex == points.length) { |
33 |
| - return; |
34 |
| - } |
35 |
| - |
36 |
| - int firstNonCollinearIndex; |
37 |
| - for (firstNonCollinearIndex = firstNonEqualIndex + 1; firstNonCollinearIndex < points.length; firstNonCollinearIndex++) { |
38 |
| - if (Point.orientation(points[0], points[firstNonEqualIndex], points[firstNonCollinearIndex]) != 0) { |
39 |
| - break; |
| 38 | + |
| 39 | + if (firstNonEqualIndex == points.length) { |
| 40 | + return; |
40 | 41 | }
|
41 |
| - } |
42 |
| - hull.push(points[firstNonCollinearIndex - 1]); |
43 |
| - |
44 |
| - // Process the remaining points and update the hull |
45 |
| - for (int i = firstNonCollinearIndex; i < points.length; i++) { |
46 |
| - Point top = hull.pop(); |
47 |
| - while (Point.orientation(hull.peek(), top, points[i]) <= 0) { |
48 |
| - top = hull.pop(); |
| 42 | + |
| 43 | + int firstNonCollinearIndex; |
| 44 | + for (firstNonCollinearIndex = firstNonEqualIndex + 1; firstNonCollinearIndex < points.length; firstNonCollinearIndex++) { |
| 45 | + if (Point.orientation(points[0], points[firstNonEqualIndex], points[firstNonCollinearIndex]) != 0) { |
| 46 | + break; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + hull.push(points[firstNonCollinearIndex - 1]); |
| 51 | + |
| 52 | + // Process the remaining points and update the hull |
| 53 | + for (int i = firstNonCollinearIndex; i < points.length; i++) { |
| 54 | + Point top = hull.pop(); |
| 55 | + while (Point.orientation(hull.peek(), top, points[i]) <= 0) { |
| 56 | + top = hull.pop(); |
| 57 | + } |
| 58 | + hull.push(top); |
| 59 | + hull.push(points[i]); |
49 | 60 | }
|
50 |
| - hull.push(top); |
51 |
| - hull.push(points[i]); |
52 | 61 | }
|
53 |
| - |
| 62 | + |
54 | 63 | /**
|
55 | 64 | * @return An iterable collection of points representing the convex hull.
|
56 | 65 | */
|
|
0 commit comments