From ee6c854ae0962f51503a1c75c48a516309ae9dff Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni <213192267+Aditya2375@users.noreply.github.com> Date: Wed, 25 Nov 2026 06:30:45 +0530 Subject: [PATCH 1/2] Fix convexHullRecursive returning interior points --- src/main/java/com/thealgorithms/geometry/ConvexHull.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/geometry/ConvexHull.java b/src/main/java/com/thealgorithms/geometry/ConvexHull.java index d44d20c68807..aff25aaeeceb 100644 --- a/src/main/java/com/thealgorithms/geometry/ConvexHull.java +++ b/src/main/java/com/thealgorithms/geometry/ConvexHull.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.TreeSet; @@ -69,6 +70,7 @@ public static List convexHullBruteForce(List points) { * @return the convex hull points in counter-clockwise order */ public static List convexHullRecursive(List points) { + points = new ArrayList<>(new LinkedHashSet<>(points)); if (points.size() < 3) { List result = new ArrayList<>(points); Collections.sort(result); @@ -105,11 +107,11 @@ public static List convexHullRecursive(List points) { private static void constructHull(Collection points, Point left, Point right, Set convexSet) { if (!points.isEmpty()) { Point extremePoint = null; - int extremePointDistance = Integer.MIN_VALUE; + long extremePointDistance = Long.MIN_VALUE; List candidatePoints = new ArrayList<>(); for (Point p : points) { - int det = Point.orientation(left, right, p); + long det = (long) (right.x() - left.x()) * (p.y() - left.y()) - (long) (right.y() - left.y()) * (p.x() - left.x()); if (det > 0) { candidatePoints.add(p); if (det > extremePointDistance) { From fa2fedb1c7583a9e25a90d3d71a5efa57f198700 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni <213192267+Aditya2375@users.noreply.github.com> Date: Wed, 25 Nov 2026 06:31:32 +0530 Subject: [PATCH 2/2] Add regression tests for convexHullRecursive interior-point bug --- .../geometry/ConvexHullTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/test/java/com/thealgorithms/geometry/ConvexHullTest.java b/src/test/java/com/thealgorithms/geometry/ConvexHullTest.java index d3ca0df65829..b857cc845f04 100644 --- a/src/test/java/com/thealgorithms/geometry/ConvexHullTest.java +++ b/src/test/java/com/thealgorithms/geometry/ConvexHullTest.java @@ -135,4 +135,23 @@ void testRecursiveHullForCoverage() { assertEquals(expectedCollinear, resultCollinear, "Should correctly handle collinear points on the hull edge"); assertTrue(isCounterClockwise(resultCollinear), "The result of the collinear test should be in counter-clockwise order"); } + @Test + void testConvexHullRecursiveExcludesInteriorPoints() { + // (1,8) is strictly inside the hull of the other four points. + List points = new ArrayList<>(List.of(new Point(1, 5), new Point(1, 8), new Point(2, 9), new Point(0, 10), new Point(4, 19))); + List expected = List.of(new Point(1, 5), new Point(2, 9), new Point(4, 19), new Point(0, 10)); + assertEquals(expected, ConvexHull.convexHullRecursive(points)); + + // (7,2) and (8,3) are strictly inside the triangle (4,0), (11,3), (3,10). + List points2 = new ArrayList<>(List.of(new Point(4, 0), new Point(7, 2), new Point(8, 3), new Point(11, 3), new Point(3, 10))); + List expected2 = List.of(new Point(4, 0), new Point(11, 3), new Point(3, 10)); + assertEquals(expected2, ConvexHull.convexHullRecursive(points2)); + } + + @Test + void testConvexHullRecursiveDuplicatePoints() { + List points = new ArrayList<>(List.of(new Point(5, 5), new Point(5, 5))); + List expected = List.of(new Point(5, 5)); + assertEquals(expected, ConvexHull.convexHullRecursive(points)); + } }