Repository navigation
fix: stop convexHullRecursive returning interior points - #7784
Aditya2375 wants to merge 3 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
DenizAltunkapan
left a comment
There was a problem hiding this comment.
Thanks for fixing the farthest-point selection and adding regressions for interior and duplicate points. I confirmed locally that the recursive hull can still drop a vertex when the cross product overflows. Before merging, please make the arithmetic consistent across the recursive hull and add a regression test for large coordinates.
|
|
||
| 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()); |
There was a problem hiding this comment.
All four coordinate differences are evaluated as int before multiplication, so a cast after subtraction can preserve an already overflowed value. Please widen before subtracting. With unrestricted int coordinates, the determinant can also exceed long. Please use arithmetic that preserves its sign and magnitude across the supported range, or document and enforce a safe coordinate range.
| @@ -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) { | |||
There was a problem hiding this comment.
The initial upper/lower split and the final angle sort and collinearity filter still call Point.orientation(), which computes the cross product entirely in int. Please use the same overflow-safe orientation throughout the recursive hull. The squared-distance tie-breaker in the angle sort also needs to respect the supported coordinate range.
| assertTrue(isCounterClockwise(resultCollinear), "The result of the collinear test should be in counter-clockwise order"); | ||
| } | ||
| @Test | ||
| void testConvexHullRecursiveExcludesInteriorPoints() { |
There was a problem hiding this comment.
Please add a regression with large positive and negative coordinates. I compiled this revision and checked the triangle (-32768, -32768), (32768, -32768), (-32768, 32768). The initial split treats the three points as collinear after the cross product wraps to zero, and the result drops (32768, -32768).
Compute the cross product exactly (differences widened to long, BigInteger for the product) in the brute force check, the initial split, constructHull and the angle sort. Drop the collinear tie-break and filter in sortCounterClockwise, which cannot trigger because hull vertices are strictly convex. Add tests with large coordinates.
|
Thanks for the careful review. Pushed e6bae2c: the cross product is now computed exactly everywhere in ConvexHull (brute force check, initial split, constructHull, angle sort), with the differences widened to long first and BigInteger for the product, so it holds over the full int range. I removed the collinear tie-break in the angle sort since hull vertices are strictly convex and it can't be reached, which also removes the squared distance question. Added tests for your (-32768, -32768), (32768, -32768), (-32768, 32768) triangle and for points at Integer.MIN_VALUE / MAX_VALUE. |
Describe your change:
Fixed
ConvexHull.convexHullRecursivereturning points that lie strictly inside the convex hull.The quickhull step picks the "extreme" point (the farthest point from the current line segment, which is guaranteed to be a hull vertex) and recurses on it. But it compared values returned by
Point.orientation, which normalizes its result withInteger.compare(..., 0)and therefore only ever returns -1, 0 or 1. Every candidate point compared "equal", so the code simply recursed on the first candidate in list order. An arbitrary point beyond the segment is not guaranteed to be on the hull, so strictly interior points ended up in the result.Example with the current implementation:
The fix computes the actual cross product (as a
long, to avoid int overflow) when selecting the extreme point. I also deduplicated the input up front, since coincident input points previously made the< 3base case return duplicates (e.g.[P, P]->[P, P]).I verified the fixed implementation against an independent monotone-chain oracle on 5000 randomized point sets (including collinear and duplicate-heavy inputs): all match. Added regression tests for both cases; both fail on the current code and pass with the fix.
Checklist:
Fixes: #{$ISSUE_NO}.