-
-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathjarvis-march.js
45 lines (39 loc) · 1.05 KB
/
jarvis-march.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function jarvisMarch(points) {
const hull = [];
let pointOnHull = points.reduce((leftmost, current) => leftmost.x < current.x ? leftmost : current);
do {
hull.push(pointOnHull);
pointOnHull = points.reduce(chooseNextPointOnHull(pointOnHull));
} while (pointOnHull !== hull[0]);
return hull;
}
function chooseNextPointOnHull(currentPoint) {
return function (nextPoint, candidate) {
if (nextPoint === currentPoint || isLeftOf({ a: currentPoint, b: nextPoint }, candidate)) {
return candidate;
}
return nextPoint;
}
}
function isLeftOf({ a, b }, p) {
return (b.x - a.x) * (p.y - a.y) > (p.x - a.x) * (b.y - a.y);
}
const points = [
{ x: -5, y: 2 },
{ x: 5, y: 7 },
{ x: -6, y: -12 },
{ x: -14, y: -14 },
{ x: 9, y: 9 },
{ x: -1, y: -1 },
{ x: -10, y: 11 },
{ x: -6, y: 15 },
{ x: -6, y: -8 },
{ x: 15, y: -9 },
{ x: 7, y: -7 },
{ x: -2, y: -9 },
{ x: 6, y: -5 },
{ x: 0, y: 14 },
{ x: 2, y: 8 }
];
const convexHull = jarvisMarch(points);
convexHull.forEach(p => console.log(`(${p.x}, ${p.y})`));