Skip to content

fix: stop convexHullRecursive returning interior points - #7784

Open
Aditya2375 wants to merge 2 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-convex-hull-recursive
Open

Aditya2375 wants to merge 2 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-convex-hull-recursive

Conversation

@Aditya2375

Copy link
Copy Markdown
Contributor

Describe your change:

Fixed ConvexHull.convexHullRecursive returning 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 with Integer.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:

convexHullRecursive([(1,5), (1,8), (2,9), (0,10), (4,19)])
// returns [(1,5), (2,9), (4,19), (1,8), (0,10)]
// (1,8) lies strictly inside the hull of the other four points

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 < 3 base 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.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Java files are placed inside an existing directory.
  • All filenames are in all uppercase characters with no spaces or dashes.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.42%. Comparing base (9e216aa) to head (fa2fedb).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7784      +/-   ##
============================================
- Coverage     82.45%   82.42%   -0.03%     
+ Complexity     8512     8511       -1     
============================================
  Files           847      847              
  Lines         26291    26294       +3     
  Branches       5134     5135       +1     
============================================
- Hits          21677    21674       -3     
- Misses         3825     3830       +5     
- Partials        789      790       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Aditya2375
Aditya2375 marked this pull request as ready for review November 25, 2026 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants