-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (47 loc) · 1.76 KB
/
index.js
File metadata and controls
54 lines (47 loc) · 1.76 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
var selfIntersectLines = [];
exports.findSelfIntersections = function(points, findAllLines) {
var lineIntersect = require('line-intersect'),
equalPoint = function(point1, point2) {
return(point1[0] === point2[0] && point1[1] === point2[1]);
},
line1p1, line1p2, line2p1, line2p2, result, found = false;
for(var i = 0; i < points.length; i++) {
line1p1 = points[i];
if((i+1) < points.length) {
line1p2 = points[i+1];
} else {
line1p2 = points[0];
if(equalPoint(line1p1, line1p2)) {
continue;
}
}
for(var j = i+1; j < points.length; j++) {
line2p1 = points[j];
if((j+1) < points.length) {
line2p2 = points[j+1];
} else {
line2p2 = points[0];
if(equalPoint(line2p1, line2p2)) {
continue;
}
}
result = lineIntersect.checkIntersection(
line1p1[0], line1p1[1], line1p2[0], line1p2[1],
line2p1[0], line2p1[1], line2p2[0], line2p2[1]
);
if(result['type'] === 'intersecting') {
if(equalPoint(line1p2, line2p1) && equalPoint(line1p2, [result.point.x, result.point.y]) || equalPoint(line1p1, line2p2)) {
continue;
}
selfIntersectLines.push([[line1p1, line1p2], [line2p1, line2p2]]);
if(findAllLines !== true) {
return true;
}
}
}
}
return found;
};
exports.getSelfIntersectLines = function() {
return selfIntersectLines;
};