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
6 changes: 4 additions & 2 deletions src/main/java/com/thealgorithms/geometry/ConvexHull.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,6 +70,7 @@ public static List<Point> convexHullBruteForce(List<Point> points) {
* @return the convex hull points in counter-clockwise order
*/
public static List<Point> convexHullRecursive(List<Point> points) {
points = new ArrayList<>(new LinkedHashSet<>(points));
if (points.size() < 3) {
List<Point> result = new ArrayList<>(points);
Collections.sort(result);
Expand Down Expand Up @@ -105,11 +107,11 @@ public static List<Point> convexHullRecursive(List<Point> points) {
private static void constructHull(Collection<Point> points, Point left, Point right, Set<Point> convexSet) {
if (!points.isEmpty()) {
Point extremePoint = null;
int extremePointDistance = Integer.MIN_VALUE;
long extremePointDistance = Long.MIN_VALUE;
List<Point> 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) {
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/com/thealgorithms/geometry/ConvexHullTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Point> 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<Point> 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<Point> 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<Point> expected2 = List.of(new Point(4, 0), new Point(11, 3), new Point(3, 10));
assertEquals(expected2, ConvexHull.convexHullRecursive(points2));
}

@Test
void testConvexHullRecursiveDuplicatePoints() {
List<Point> points = new ArrayList<>(List.of(new Point(5, 5), new Point(5, 5)));
List<Point> expected = List.of(new Point(5, 5));
assertEquals(expected, ConvexHull.convexHullRecursive(points));
}
}
Loading