Skip to content

Commit 36e2b16

Browse files
committed
Changed implementation of slope calculation
1 parent 08d8c6b commit 36e2b16

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

Geometry/ConvexHullGraham.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,10 @@ function compare(a, b) {
1313
return 1
1414
}
1515
function orientation(a, b, c) {
16-
// Check orientation of Line(a, b) and Line(b, c)
17-
const alpha = (b.y - a.y) / (b.x - a.x)
18-
const beta = (c.y - b.y) / (c.x - b.x)
16+
const crossProduct = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y);
1917

20-
// Clockwise
21-
if (alpha > beta) return 1
22-
// Anticlockwise
23-
else if (beta > alpha) return -1
24-
// Colinear
25-
return 0
18+
if(crossProduct === 0) return 0;
19+
return (crossProduct > 0) ? 1 : -1;
2620
}
2721

2822
function convexHull(points) {

0 commit comments

Comments
 (0)