Skip to content

Commit 42b866e

Browse files
Spc.java
1 parent 545945f commit 42b866e

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

src/main/java/com/thealgorithms/geometry/GrahamScan.java

+34-25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Arrays;
44
import java.util.Comparator;
55
import java.util.Stack;
6+
import java.util.ArrayList;
67

78
/**
89
* A Java program that computes the convex hull using the Graham Scan algorithm.
@@ -16,41 +17,49 @@
1617
* https://algs4.cs.princeton.edu/99hull/GrahamScan.java.html
1718
*/
1819
public class GrahamScan {
20+
1921
private final Stack<Point> hull = new Stack<>();
22+
2023
public GrahamScan(Point[] points) {
2124
// Pre-process points: sort by y-coordinate, then by polar order with respect to the first point
2225
Arrays.sort(points);
2326
Arrays.sort(points, 1, points.length, points[0].polarOrder());
27+
2428
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+
}
3037
}
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;
4041
}
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]);
4960
}
50-
hull.push(top);
51-
hull.push(points[i]);
5261
}
53-
62+
5463
/**
5564
* @return An iterable collection of points representing the convex hull.
5665
*/

0 commit comments

Comments
 (0)