-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvexHull.js
64 lines (51 loc) · 1.37 KB
/
convexHull.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class ConvexHull {
constructor() {
this.hull = [];
}
// Find left most point for gift wrapping algorithm
findLeftMostPoint(points) {
let minX = Number.POSITIVE_INFINITY;
let minY = 0;
let minIdx = 0;
// O(n) where n is the number of points to find leftmost point
points.forEach((point, idx) => {
if (point.x <= minX) {
minY = point.y;
minX = point.x;
minIdx = idx;
}
});
return { x: minX, y: minY, idx: minIdx }
}
orient(p, q, r) {
const val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
if (val == 0) return 0; // colinear
return (val > 0) ? 1 : 2; // clock or counterclock wise
}
jarvisWalk(points) {
// Convex hull
this.hull = [];
// Number of points
const n = points.length;
// Only work with more than 2 points
if (n < 3) { return; }
// Leftmost point using the cartesian plane relative to the genesis point
const leftmostPoint = this.findLeftMostPoint(points);
let p = leftmostPoint.idx, q;
do {
this.hull.push(points[p]);
q = (p + 1) % n;
for (let i = 0; i < n; i++) {
if (this.orient(points[p], points[i], points[q]) === 2) {
q = i;
}
}
// Connect the points
points[p].connect(points[q]);
p = q;
} while (p !== leftmostPoint.idx);
}
removePointsInHull(points) {
return points.filter(value => this.hull.includes(value));
}
}