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/4] 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/4] 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()); + } } From 3887ae97e43e2a0b046da113cfc57dee7cfb5c6b Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni <213192267+Aditya2375@users.noreply.github.com> Date: Sun, 29 Nov 2026 14:33:35 +0530 Subject: [PATCH 3/4] test: cover the diagonal first-ray tie in GrahamScan --- .../com/thealgorithms/geometry/GrahamScanTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java b/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java index 48fbd841a7a5..c71721cfcd1a 100644 --- a/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java +++ b/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java @@ -39,4 +39,15 @@ void testGrahamScanKeepsFarthestPointOnHorizontalRay() { 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()); + } } From d8d00f1205bacb977c8ed142acb135409cff8490 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni <213192267+Aditya2375@users.noreply.github.com> Date: Sun, 29 Nov 2026 14:52:25 +0530 Subject: [PATCH 4/4] Simplify loop bound in collinear tie-break (firstNonCollinearIndex <= points.length always) --- src/main/java/com/thealgorithms/geometry/GrahamScan.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index d556ad64732d..4ed4a5ddba5c 100644 --- a/src/main/java/com/thealgorithms/geometry/GrahamScan.java +++ b/src/main/java/com/thealgorithms/geometry/GrahamScan.java @@ -50,7 +50,7 @@ public GrahamScan(Point[] points) { // 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++) { + for (int i = firstNonEqualIndex + 1; i < firstNonCollinearIndex; i++) { if (distanceSquared(points[0], points[i]) > distanceSquared(points[0], farthestCollinear)) { farthestCollinear = points[i]; }