From 0688e839036d50af2d2fadf1409056df20a3777a Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni <213192267+Aditya2375@users.noreply.github.com> Date: Wed, 25 Nov 2026 11:57:29 +0530 Subject: [PATCH 1/2] Fix GrahamScan dropping hull vertices on horizontal collinear runs --- .../com/thealgorithms/geometry/GrahamScan.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index 1a373cf315a2..d556ad64732d 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 < 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++) { @@ -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. */ From ea2364432150a69caa8717a1280a87c70b3a22bd Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni <213192267+Aditya2375@users.noreply.github.com> Date: Wed, 25 Nov 2026 11:58:04 +0530 Subject: [PATCH 2/2] Add regression tests for GrahamScan collinear runs --- .../geometry/GrahamScanTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java b/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java index 622273881a27..48fbd841a7a5 100644 --- a/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java +++ b/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java @@ -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()); + } }