Repository navigation
fix: stop GrahamScan dropping hull vertices on horizontal collinear runs - #7785
Open
Aditya2375 wants to merge 2 commits into
Open
Aditya2375 wants to merge 2 commits into
Aditya2375 wants to merge 2 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7785 +/- ##
=========================================
Coverage 82.45% 82.46%
- Complexity 8512 8519 +7
=========================================
Files 847 847
Lines 26291 26300 +9
Branches 5134 5136 +2
=========================================
+ Hits 21677 21687 +10
Misses 3825 3825
+ Partials 789 788 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Aditya2375
marked this pull request as ready for review
November 25, 2026 06:54
Aditya2375
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
November 25, 2026 06:54
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change:
Fixed
GrahamScandropping hull vertices when several input points are collinear with the anchor point on a horizontal ray.The constructor sorts the points by polar angle around the lowest point and then assumes the last point of the first collinear run (
points[firstNonCollinearIndex - 1]) is the farthest one from the anchor - only that point can be a hull vertex, the rest are interior to the edge. That assumption holds for the reference implementation (Princeton algs4), whosePolarOrdercompares horizontal collinear ties as equal so the stable sort keeps them in ascending distance order. HerePolarOrderbreaks horizontal ties by descending distance (Integer.compare(dx2, dx1), pinned byPointTest), so the point atfirstNonCollinearIndex - 1is the nearest point of the run and the actual hull vertex is never looked at again.Examples with the current implementation:
The fix selects the farthest point of the collinear run explicitly (by squared distance) instead of relying on the sort order, so it is correct regardless of how
PolarOrderbreaks ties. No changes toPoint/PolarOrderwere needed and all existing tests pass unmodified.I verified the fix against an independent monotone-chain oracle on 5000 randomized point sets (including horizontal/diagonal collinear and duplicate-heavy inputs): all hulls match. Added regression tests for the cases above; they fail on the current code and pass with the fix.
Checklist:
Fixes: #{$ISSUE_NO}.