-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
213 lines (174 loc) · 6.08 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
'use strict';
const Polygon = require('./lib/polygon');
const PolygonArray = require('./lib/polygonArray');
const Edge = require('./lib/edge').Edge;
const pointState = require('./lib/point').pointState;
function intersection(poly1, poly2) {
try {
testInputValues(poly1, poly2);
} catch (e) {
return e;
}
poly1 = new Polygon(poly1);
poly2 = new Polygon(poly2);
let result = isOnePolyInOther(poly1, poly2);
if (result) {
return result;
}
let intersectPolies = new PolygonArray();
intersectPolies.add(new Polygon());
let point;
let elem;
let edges = poly1.getEdges();
for (let edge of edges) {
point = edge.getStartPoint();
findPointInPoly(point, poly2);
findEdgeIntersection(edge, poly2.getEdges());
if (!edge.getIntersectCount()) {
continue;
}
elem = getFirstIntersectElem(edge, point);
if (!elem) {
continue;
}
addIntersectPoint(elem.point, poly2);
findNextIntersectPoint(edge);
}
return intersectPolies.getResult();
function findNextIntersectPoint(edge) {
let poly = poly1.isEdgeExist(elem.edge) ? poly2 : poly1;
let ownPoly = (poly === poly1) ? poly2 : poly1;
let point1 = elem.edge.getStartPoint();
let point2 = elem.edge.getEndPoint();
poly.isPointInPoly(point1);
poly.isPointInPoly(point2);
let edgePart1 = new Edge(elem.point, point1);
let edgePart2 = new Edge(elem.point, point2);
let edges = [].slice.call(poly.getEdges());
reduceEdges(edges, edge);
findEdgeIntersection(edgePart1, edges);
findEdgeIntersection(edgePart2, edges);
edgePart1.setState(point1.getState());
edgePart2.setState(point2.getState());
let nextStartPoint;
let nextPart;
if (point1.getState() === pointState.outPoly && (edgePart1.getIntersectCount() % 2) ||
point1.getState() === (pointState.inPoly || pointState.onEdge) &&
!(edgePart1.getIntersectCount() % 2)) {
nextStartPoint = point1;
nextPart = edgePart1;
} else {
nextStartPoint = point2;
nextPart = edgePart2;
}
if (nextPart.getIntersectCount()) {
let element = getFirstIntersectElem(nextPart, elem.point);
if (element) {
edge = elem.edge;
elem = element;
addIntersectPoint(element.point, poly);
return findNextIntersectPoint(edge);
}
}
edges = [].slice.call(ownPoly.getEdges());
reduceEdges(edges, elem.edge);
let nextEdge;
for (let edge of edges) {
if (edge.isPointExist(nextStartPoint)) {
nextEdge = edge;
break;
}
}
if (!nextEdge.getStartPoint().isCoordEqual(nextStartPoint)) {
nextEdge.changePoints();
}
ownPoly.setDirection(elem.edge, nextEdge);
for (var i = 0; i < ownPoly.getEdges().length; i++) {
if (i !== 0) {
nextEdge = ownPoly.getNextEdge();
}
point = nextEdge.getStartPoint();
findPointInPoly(point, poly);
findEdgeIntersection(nextEdge, poly.getEdges());
if (!nextEdge.getIntersectCount()) {
continue;
}
elem = getFirstIntersectElem(nextEdge, point);
if (!elem) {
return;
}
addIntersectPoint(elem.point, poly);
return findNextIntersectPoint(nextEdge);
}
}
function addIntersectPoint(point, poly) {
if (point.getState() === pointState.undefined) {
poly.isPointInPoly(point);
}
let intersectPoly = intersectPolies.getLast();
if (intersectPoly.isIntersectionEnd()) {
intersectPolies.add(new Polygon());
}
intersectPoly.addPoint(point);
}
function findPointInPoly(point, poly) {
if (point.compare(intersectPolies.getPoints()) && poly.isPointInPoly(point)) {
addIntersectPoint(point, poly);
}
}
function getFirstIntersectElem(edge, point) {
let intersections = edge.getIntersectElements();
intersections = intersections.filter(intersect =>
intersect.point.compare(intersectPolies.getPoints()));
if (!intersections.length) {
intersectPolies.getLast().endIntersection();
return false;
}
edge.setState(point.getState());
if (intersections.length > 1) {
intersections.forEach(intersect => intersect.point.calcDistance(point));
intersections.sort((a, b) => a.point.distance - b.point.distance);
}
return intersections[0];
}
}
function testInputValues(poly1, poly2) {
if (!Array.isArray(poly1) || !Array.isArray(poly2)) {
throw new TypeError('Both of input values must be an array');
} else if (poly1.length < 3 || poly2.length < 3) {
throw new RangeError('Lengths of input values must be greater than two');
}
}
function isOnePolyInOther(poly1, poly2) {
let countPointsIn;
for (let poly of [poly1, poly2]) {
let secondPoly = (poly === poly1) ? poly2 : poly1;
countPointsIn = poly.calcPointsInPoly(secondPoly);
if (countPointsIn === poly.getPoints().length) {
return poly.getPointsResult();
} else if (countPointsIn) {
break;
}
}
return false;
}
function findEdgeIntersection(edge, edges) {
if (edge.getIntersectElements().length) {
return;
}
let intersectPoint;
edges.forEach(intersectEdge => {
intersectPoint = edge.findIntersectingPoint(intersectEdge);
if (intersectPoint.toString() === 'Point') {
edge.addIntersectElement(intersectEdge, intersectPoint);
}
});
}
function reduceEdges(edges, edge) {
let index = edges.indexOf(edge);
if (index + 1) {
edges.splice(index, 1);
}
return edges;
}
module.exports = intersection;