-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaths.js
More file actions
66 lines (53 loc) · 1.52 KB
/
maths.js
File metadata and controls
66 lines (53 loc) · 1.52 KB
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
const combinations = require("iter-tools").combinations;
exports.collinear = (points, n) => {
let round = points.length;
let results = [];
// sort the points by y and then x
points.sort((a, b) => {
return a.y === b.y ? a.x - b.x : a.y - b.y;
});
// from the bigger points combination, I try to get the longest collinear line segment
while (round >= n) {
for (let c of combinations(points, round)) {
if (angles(c).reduce((acc, val) => acc + val) === 0) {
results.push(c);
}
}
round -= 1;
if (results.length) break; // the first results are the longest
}
return results;
};
// get the angles between each group of three points
const angles = points => {
let angles = [];
if (points.length === 2) {
// two points drawn always a line :-)
angles.push(0);
}
for (let c of combinations(points, 3)) {
angles.push(angle(...c));
}
return angles;
};
// get the length of the segment between two points
const segmentLength = (exports.segmentLength = (p1, p2) => {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
});
// angle between three points
const angle = (p1, p2, p3) => {
var p12 = segmentLength(p1, p2);
var p13 = segmentLength(p1, p3);
var p23 = segmentLength(p2, p3);
// for values less than -1 or greater than 1, Math.acos() returns NaN.
return Math.acos(
Math.min(
Math.max(
(Math.pow(p12, 2) + Math.pow(p13, 2) - Math.pow(p23, 2)) /
(2 * p12 * p13),
-1.0
),
1.0
)
);
};