JavaScript
JavaScript copied to clipboard
fix: correct division by zero bug in ConvexHull orientation function fixes: #1815
Description
This PR fixes a critical bug in the ConvexHull algorithm and improves the implementation.
Changes Made
- Fixed Division by Zero Bug: Replaced slope-based orientation calculation with cross product method
- Improved Algorithm: Implemented standard Monotone Chain algorithm
-
Better Error Handling: Changed
ErrortoRangeErrorfor invalid inputs - Input Protection: Algorithm no longer mutates original input array
- Enhanced Documentation: Added comprehensive JSDoc comments
-
Comprehensive Tests: Added 13 test cases covering:
- Edge cases (collinear points, duplicates, vertical/horizontal lines)
- Various geometric configurations
- Negative and floating-point coordinates
- Large datasets
- Input immutability
Bug Details
Before: The orientation function used division which caused crashes:
const alpha = (b.y - a.y) / (b.x - a.x) // Division by zero!
After: Using robust cross product:
const crossProduct = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y)
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 85.94%. Comparing base (08d8c6b) to head (6035c65).
Additional details and impacted files
@@ Coverage Diff @@
## master #1817 +/- ##
==========================================
+ Coverage 85.91% 85.94% +0.02%
==========================================
Files 379 379
Lines 19778 19795 +17
Branches 3015 3018 +3
==========================================
+ Hits 16993 17012 +19
+ Misses 2785 2783 -2
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.