Skip to content

Commit ee6c854

Browse files
authored
Fix convexHullRecursive returning interior points
1 parent 9e216aa commit ee6c854

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

‎src/main/java/com/thealgorithms/geometry/ConvexHull.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Collections;
66
import java.util.Comparator;
77
import java.util.HashSet;
8+
import java.util.LinkedHashSet;
89
import java.util.List;
910
import java.util.Set;
1011
import java.util.TreeSet;
@@ -69,6 +70,7 @@ public static List<Point> convexHullBruteForce(List<Point> points) {
6970
* @return the convex hull points in counter-clockwise order
7071
*/
7172
public static List<Point> convexHullRecursive(List<Point> points) {
73+
points = new ArrayList<>(new LinkedHashSet<>(points));
7274
if (points.size() < 3) {
7375
List<Point> result = new ArrayList<>(points);
7476
Collections.sort(result);
@@ -105,11 +107,11 @@ public static List<Point> convexHullRecursive(List<Point> points) {
105107
private static void constructHull(Collection<Point> points, Point left, Point right, Set<Point> convexSet) {
106108
if (!points.isEmpty()) {
107109
Point extremePoint = null;
108-
int extremePointDistance = Integer.MIN_VALUE;
110+
long extremePointDistance = Long.MIN_VALUE;
109111
List<Point> candidatePoints = new ArrayList<>();
110112

111113
for (Point p : points) {
112-
int det = Point.orientation(left, right, p);
114+
long det = (long) (right.x() - left.x()) * (p.y() - left.y()) - (long) (right.y() - left.y()) * (p.x() - left.x());
113115
if (det > 0) {
114116
candidatePoints.add(p);
115117
if (det > extremePointDistance) {

0 commit comments

Comments
 (0)