-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
277 lines (238 loc) · 8.44 KB
/
solution.js
File metadata and controls
277 lines (238 loc) · 8.44 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//algorithm http://davis.wpi.edu/~matt/courses/clipping/
function intersect(fig1, fig2) { // return arr with poly's
'use strict'
function getCoordinatesOfCrossing(a1, a2, b1, b2) { //{x:y:},{x:y:},{x:y:},{x:y:} return {x:y:}||true||false
var denom, a, b, num1, num2, x, y;
denom = ((b2.y - b1.y) * (a2.x - a1.x)) - ((b2.x - b1.x) * (a2.y - a1.y));
if (denom == 0) return true;
a = a1.y - b1.y;
b = a1.x - b1.x;
num1 = ((b2.x - b1.x) * a) - ((b2.y - b1.y) * b);
num2 = ((a2.x - a1.x) * a) - ((a2.y - a1.y) * b);
a = num1 / denom;
b = num2 / denom;
x = a1.x + (a * (a2.x - a1.x));
y = a1.y + (a * (a2.y - a1.y));
if ((a >= 0 && a < 1) && (b >= 0 && b < 1)) {
return {
x: x,
y: y,
};
} else {
return false;
}
};
function inPolygon(dot, polygon) { //return true false
var npol = polygon.length,
j = npol - 1,
c = false;
for (var i = 0; i < npol; i++) {
if ((((polygon[i].y <= dot.y) && (dot.y < polygon[j].y)) || ((polygon[j].y <= dot.y) && (dot.y < polygon[i].y))) &&
(dot.x > (polygon[j].x - polygon[i].x) * (dot.y - polygon[i].y) / (polygon[j].y - polygon[i].y) + polygon[i].x)) {
c = !c
}
j = i;
}
return c;
};
function figInOthFig(fig) {
return fig.every(isTrue) && fig
function isTrue(n) {
return n.inPoly
}
};
function Node(vec, delta, intersection) {
this.vec = vec;
this.delta = delta || 0;
this.intersect = !!intersection;
};
Node.prototype = {
vec: null,
next: null,
next: null,
prev: null,
nextPoly: null,
neighbor: null,
intersect: null,
entry: null,
visited: false,
delta: 0,
inPoly: true,
nextNonIntersection: function() {
var self = this;
while (self && self.intersect) {
self = self.next;
}
return self;
},
last: function() {
var self = this;
while (self.next && self.next !== this) {
self = self.next;
}
return self;
},
createLoop: function() {
var last = this.last();
last.prev.next = this;
this.prev = last.prev;
},
firstNodeOfInterest: function() {
var self = this;
if (self) {
do {
self = self.next;
} while (self !== this && (!self.intersect || self.intersect && self.visited));
}
return self;
},
insertBetween: function(first, last) {
var a = first;
while (a !== last && a.delta < this.delta) {
a = a.next;
}
this.next = a;
this.prev = a.prev;
if (this.prev) {
this.prev.next = this;
}
this.next.prev = this;
}
};
Node.prototype.toString = function() {
var str = '';
var self = this;
while (self.next && self.next !== this) {
str += '(x:' + self.vec.x + '; y:' + self.vec.y + ') intersect:' + self.intersect + '\n';
self = self.next;
}
return str;
};
function createLinkedList(vecs, vecs2) {
var l = vecs.length;
var ret, where;
for (var i = 0; i < l; i++) {
var current = vecs[i];
if (!ret) {
where = ret = new Node(current);
where.inPoly = ret.inPoly = inPolygon(vecs[i], vecs2);
} else {
where.next = new Node(current);
where.next.prev = where;
where = where.next;
where.inPoly = inPolygon(vecs[i], vecs2);
}
}
return ret;
};
function distance(a, b) {
return Math.sqrt(Math.pow((b.x - a.x), 2) + Math.pow((b.y - a.y), 2));
};
function clean(arr) {
var cur = arr.length - 1;
while (cur--) {
var c = arr[cur];
var p = arr[cur + 1];
if (c.x === p.x && c.y === p.y) {
arr.splice(cur, 1);
}
}
return arr;
};
function identifyIntersections(subjectList, clipList) {
var subject, clip;
var auxs = subjectList.last();
auxs.next = new Node(subjectList.vec, auxs);
auxs.next.prev = auxs;
var auxc = clipList.last();
auxc.next = new Node(clipList.vec, auxc);
auxc.next.prev = auxc;
var found = false;
for (subject = subjectList; subject.next; subject = subject.next) {
if (!subject.intersect) {
for (clip = clipList; clip.next; clip = clip.next) {
if (!clip.intersect) {
var a = subject.vec,
b = subject.next.nextNonIntersection().vec,
c = clip.vec,
d = clip.next.nextNonIntersection().vec;
var i = getCoordinatesOfCrossing(a, b, c, d);
if (i && i !== true) {
found = true;
var iSubject = new Node(i, distance(a, i) / distance(a, b), true);
var iClip = new Node(i, distance(c, i) / distance(c, d), true);
iSubject.neighbor = iClip;
iClip.neighbor = iSubject;
iSubject.insertBetween(subject, subject.next.nextNonIntersection());
iClip.insertBetween(clip, clip.next.nextNonIntersection());
}
}
}
}
}
return found;
};
function entryNotEntry(subjectList, clipList) {
var entry = true;
var subject;
for (subject = subjectList; subject.next; subject = subject.next) {
if (subject.intersect) {
subject.entry = subject.neighbor.entry = entry;
entry = !entry;
}
}
};
function collectClipResults(subjectList, clipList) {
subjectList.createLoop();
clipList.createLoop();
var crt, results = [],
result;
while ((crt = subjectList.firstNodeOfInterest()) !== subjectList) {
result = [];
for (; !crt.visited; crt = crt.neighbor) {
result.push(crt.vec);
var forward = crt.entry
while (true) {
crt.visited = true;
/* crt = crt.next.inPoly ? crt.next :
crt.prev.inPoly ? crt.prev :
forward ? crt.next : crt.prev;*/
crt = forward ? crt.next : crt.prev;
if (crt.intersect) {
crt.visited = true;
break;
} else {
result.push(crt.vec);
}
}
}
results.push(clean(result));
}
return results;
};
function polygonBoolean(subjectPoly, clipPoly) {
var subjectList = createLinkedList(subjectPoly, clipPoly),
clipList = createLinkedList(clipPoly, subjectPoly),
response;
// Phase one.
var isects = identifyIntersections(subjectList, clipList);
if (isects) {
// Phase two.
entryNotEntry(subjectList, clipList);
// Phase three. collect resulting polygons
response = collectClipResults(subjectList, clipList);
} else {
// No intersections
var inner = subjectList.inPoly;
var outer = clipList.inPoly;
if (inner) {
return [subjectPoly]
} else if (outer) {
return [clipPoly]
}
}
console.log(subjectList.toString())
return response;
};
return polygonBoolean(fig1, fig2)
}