-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
70 lines (56 loc) · 1.26 KB
/
index.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
65
66
67
68
69
70
var simplify = require('simplify-base');
/**
* square distance between 2 points
* @param {Point} p1
* @param {Point} p2
* @return {Number}
*/
function getSquareDistance(p1, p2) { // square distance between 2 points
var dx = p1.x - p2.x,
dz = p1.z - p2.z,
dy = p1.y - p2.y;
return dx * dx +
dz * dz +
dy * dy;
}
/**
* square distance from a point to a segment
*
* @param {Point} p The point
* @param {Point} p1 The start of the segment
* @param {Point} p2 The end of the segment
* @return {Number}
*/
function getSquareSegmentDistance(p, p1, p2) { // square distance from a point to a segment
var x = p1.x,
y = p1.y,
z = p1.z,
dx = p2.x - x,
dy = p2.y - y,
dz = p2.z - z,
t;
if (dx !== 0 || dy !== 0) {
t = ((p.x - x) * dx +
(p.z - z) * dz +
(p.y - y) * dy) /
(dx * dx +
dz * dz +
dy * dy);
if (t > 1) {
x = p2.x;
y = p2.y;
z = p2.z;
} else if (t > 0) {
x += dx * t;
y += dy * t;
z += dz * t;
}
}
dx = p.x - x;
dy = p.y - y;
dz = p.z - z;
return dx * dx +
dz * dz +
dy * dy;
}
module.exports = simplify(getSquareDistance, getSquareSegmentDistance);