Repository navigation
fix: stop GrahamScan dropping hull vertices on horizontal collinear runs - #7785
Merged
DenizAltunkapan merged 5 commits intoNov 29, 2026
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7785 +/- ##
============================================
+ Coverage 82.50% 82.52% +0.01%
- Complexity 8550 8556 +6
============================================
Files 847 847
Lines 26349 26356 +7
Branches 5150 5152 +2
============================================
+ Hits 21740 21749 +9
Misses 3823 3823
+ Partials 786 784 -2 ☔ 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
DenizAltunkapan
approved these changes
Nov 29, 2026
DenizAltunkapan
enabled auto-merge (squash)
November 29, 2026 16:39
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}.