Skip to content

fix: stop GrahamScan dropping hull vertices on horizontal collinear runs - #7785

Open
Aditya2375 wants to merge 2 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-graham-scan-collinear
Open

Aditya2375 wants to merge 2 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-graham-scan-collinear

Conversation

@Aditya2375

Copy link
Copy Markdown
Contributor

Describe your change:

Fixed GrahamScan dropping 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), whose PolarOrder compares horizontal collinear ties as equal so the stable sort keeps them in ascending distance order. Here PolarOrder breaks horizontal ties by descending distance (Integer.compare(dx2, dx1), pinned by PointTest), so the point at firstNonCollinearIndex - 1 is the nearest point of the run and the actual hull vertex is never looked at again.

Examples with the current implementation:

new GrahamScan(new Point[] {(1,3), (6,3), (8,3), (5,3)}).hull()
// returns [(1,3), (5,3)] - (8,3) is a hull vertex but missing, (5,3) is interior

new GrahamScan(new Point[] {(3,3),(11,3),(5,3),(0,3),(2,3),(2,3),(4,3),(0,3),(5,3)}).hull()
// returns [(0,3), (0,3)] - the anchor point duplicated, (11,3) missing

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 PolarOrder breaks ties. No changes to Point/PolarOrder were 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.

  • 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

❌ Patch coverage is 62.50000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.46%. Comparing base (9e216aa) to head (ea23644).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
...in/java/com/thealgorithms/geometry/GrahamScan.java 62.50% 1 Missing and 2 partials ⚠️
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.
📢 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 06:54
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