Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
bhautikrathod9 wants to merge 2 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from bhautikrathod9:fix-convex-hull

Conversation

@bhautikrathod9
Copy link

@bhautikrathod9 bhautikrathod9 commented Oct 3, 2025

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)

Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ 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 

☔ View full report in Codecov by Sentry.
📢 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@raklaptudirm raklaptudirm Awaiting requested review from raklaptudirm raklaptudirm is a code owner

@appgurueu appgurueu Awaiting requested review from appgurueu appgurueu is a code owner

At least 2 approving reviews are required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /