Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/main/java/com/thealgorithms/geometry/GrahamScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ public GrahamScan(Point[] points) {
}
}

hull.push(points[firstNonCollinearIndex - 1]);
// Among the points collinear with points[0] on the first ray, only the farthest one
// can be a hull vertex. The polar-order tie-break does not guarantee it sorts last
// (horizontal ties sort by descending distance), so select it explicitly.
Point farthestCollinear = points[firstNonEqualIndex];
for (int i = firstNonEqualIndex + 1; i < firstNonCollinearIndex && i < points.length; i++) {
if (distanceSquared(points[0], points[i]) > distanceSquared(points[0], farthestCollinear)) {
farthestCollinear = points[i];
}
}
hull.push(farthestCollinear);

// Process the remaining points and update the hull
for (int i = firstNonCollinearIndex; i < points.length; i++) {
Expand All @@ -59,6 +68,12 @@ public GrahamScan(Point[] points) {
}
}

private static long distanceSquared(Point a, Point b) {
long dx = a.x() - b.x();
long dy = a.y() - b.y();
return dx * dx + dy * dy;
}

/**
* @return An iterable collection of points representing the convex hull.
*/
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/com/thealgorithms/geometry/GrahamScanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,30 @@ void testGrahamScan() {
GrahamScan graham = new GrahamScan(points);
assertEquals(expectedResult, graham.hull().toString());
}
@Test
void testGrahamScanAllCollinearHorizontal() {
// All points on a horizontal line: the hull is just the two extremes.
Point[] points = {new Point(1, 3), new Point(6, 3), new Point(8, 3), new Point(5, 3)};
String expectedResult = "[(1, 3), (8, 3)]";
GrahamScan graham = new GrahamScan(points);
assertEquals(expectedResult, graham.hull().toString());
}

@Test
void testGrahamScanAllCollinearHorizontalWithDuplicates() {
Point[] points = {new Point(3, 3), new Point(11, 3), new Point(5, 3), new Point(0, 3), new Point(2, 3), new Point(2, 3), new Point(4, 3), new Point(0, 3), new Point(5, 3)};
String expectedResult = "[(0, 3), (11, 3)]";
GrahamScan graham = new GrahamScan(points);
assertEquals(expectedResult, graham.hull().toString());
}

@Test
void testGrahamScanKeepsFarthestPointOnHorizontalRay() {
// (6,0) and (9,0) are collinear with the anchor (1,0) on a horizontal ray;
// (9,0) is a hull vertex and must not be dropped.
Point[] points = {new Point(10, 6), new Point(10, 8), new Point(4, 1), new Point(0, 9), new Point(8, 1), new Point(1, 0), new Point(7, 1), new Point(9, 0), new Point(4, 11), new Point(2, 5), new Point(7, 9), new Point(6, 0)};
String expectedResult = "[(1, 0), (9, 0), (10, 6), (10, 8), (4, 11), (0, 9)]";
GrahamScan graham = new GrahamScan(points);
assertEquals(expectedResult, graham.hull().toString());
}
}
Loading