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

[BUG]: ConvexHull algorithm crashes with division by zero on vertical lines #1815

Open
Labels
bugSomething isn't working
@bhautikrathod9

Description

Description

The current ConvexHull implementation has a critical bug in the orientation function that causes division by zero when processing points that share the same x-coordinate (vertical lines).

Location

File: Geometry/ConvexHull.js
Function: orientation(a, b, c)

Expected Behavior

The algorithm should handle vertical lines correctly and return the proper convex hull vertices.

Actual Behavior

Current Implementation (Buggy):

function orientation(a, b, c) {
 const alpha = (b.y - a.y) / (b.x - a.x) // ❌ Division by zero!
 const beta = (c.y - b.y) / (c.x - b.x) // ❌ Division by zero!
 
 if (alpha > beta) return 1
 else if (beta > alpha) return -1
 return 0
}

Problem:

When b.x === a.x or c.x === b.x, the division results in Infinity or NaN, causing incorrect results or crashes.
Steps to Reproduce

import { convexHull } from './Geometry/ConvexHull.js'
// Points with same x-coordinate (vertical line)
const points = [
 { x: 2, y: 0 },
 { x: 2, y: 1 }, // Same x as above
 { x: 2, y: 2 }, // Same x as above
 { x: 0, y: 1 },
 { x: 4, y: 1 }
]
convexHull(points) // ❌ Returns incorrect result or crashes

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

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