-
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathjarvis-march.js
More file actions
39 lines (32 loc) · 931 Bytes
/
jarvis-march.js
File metadata and controls
39 lines (32 loc) · 931 Bytes
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
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: 1, y: 3 },
{ x: 2, y: 4 },
{ x: 4, y: 0 },
{ x: 1, y: 0 },
{ x: 0, y: 2 },
{ x: 2, y: 2 },
{ x: 3, y: 4 },
{ x: 3, y: 1 },
];
const convexHull = jarvisMarch(points);
convexHull.forEach(p => console.log(`(${p.x}, ${p.y})`));