diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index 1a373cf315a2..4ed4a5ddba5c 100644 --- a/src/main/java/com/thealgorithms/geometry/GrahamScan.java +++ b/src/main/java/com/thealgorithms/geometry/GrahamScan.java @@ -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++) { + 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++) { @@ -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. */ diff --git a/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java b/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java index 622273881a27..c71721cfcd1a 100644 --- a/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java +++ b/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java @@ -13,4 +13,41 @@ 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()); + } + + @Test + void testGrahamScanKeepsFarthestPointOnDiagonalRay() { + // (1,1), (4,4), (8,8) share the anchor's first ray at 45 degrees. Non-horizontal + // ties keep their sorted order, so the farthest point is reached only by + // walking the whole run: (8,8) must replace (1,1) and then (4,4). + Point[] points = {new Point(0, 0), new Point(1, 1), new Point(4, 4), new Point(8, 8), new Point(0, 9)}; + String expectedResult = "[(0, 0), (8, 8), (0, 9)]"; + GrahamScan graham = new GrahamScan(points); + assertEquals(expectedResult, graham.hull().toString()); + } }