JavaScript icon indicating copy to clipboard operation
JavaScript copied to clipboard

fix: correct division by zero bug in ConvexHull orientation function fixes: #1815

Open bhautikrathod9 opened this issue 3 months ago • 1 comments

Description

This PR fixes a critical bug in the ConvexHull algorithm and improves the implementation.

Changes Made

  1. Fixed Division by Zero Bug: Replaced slope-based orientation calculation with cross product method
  2. Improved Algorithm: Implemented standard Monotone Chain algorithm
  3. Better Error Handling: Changed Error to RangeError for invalid inputs
  4. Input Protection: Algorithm no longer mutates original input array
  5. Enhanced Documentation: Added comprehensive JSDoc comments
  6. 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)

bhautikrathod9 avatar Oct 03 '25 08:10 bhautikrathod9

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.

codecov-commenter avatar Oct 03 '25 08:10 codecov-commenter