This repository has been archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sweep.js
227 lines (166 loc) · 5.1 KB
/
sweep.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
214
215
216
217
218
219
220
221
222
223
224
225
226
if (typeof module !== 'undefined') module.exports = sweep;
var VERTEX_START = 0,
VERTEX_SPLIT = 1,
VERTEX_END = 2,
VERTEX_MERGE = 3,
VERTEX_REGULAR = 4;
function sweep(points) {
var n = points.length,
queue = [],
i, first, last, v, prev, next;
// put vertices in a circular doubly linked list
first = last = makeNode(points[0], 0);
queue.push(first);
for (i = 1; i < n; i++) {
v = makeNode(points[i], i);
v.prev = last;
last.next = v;
last.dy = v.y - last.y;
last = v;
queue.push(v);
}
first.prev = last;
last.next = first;
last.dy = first.y - last.y;
// init edges list
var edges = [];
for (i = 0; i < n; i++) {
v = queue[i];
edges.push(new Edge(v, v.next, i));
}
// we'll iterate through vertices from top to bottom (sweep line)
queue.sort(compareVertices);
// determine vertex type
for (i = 0; i < n; i++) {
v = queue[i];
prev = v.prev;
next = v.next;
v.type =
below(prev, v) && below(next, v) ? (isConvex(next, prev, v) ? VERTEX_START : VERTEX_SPLIT) :
below(v, prev) && below(v, next) ? (isConvex(next, prev, v) ? VERTEX_END : VERTEX_MERGE) : VERTEX_REGULAR;
}
var helpers = new Array(n),
currentEdges = [],
k, j, m;
for (k = 0; k < n; k++) {
v = queue[k];
i = v.index;
if (v.type === VERTEX_START) {
insertEdge(currentEdges, edges[i]);
helpers[i] = v;
} else if (v.type === VERTEX_END) {
m = v.prev.index;
if (helpers[m].type === VERTEX_MERGE) addDiagonal(v, helpers[m]);
removeEdge(currentEdges, edges[m]);
} else if (v.type === VERTEX_SPLIT) {
j = lowerEdge(currentEdges, v).index;
addDiagonal(v, helpers[j]);
helpers[j] = v;
insertEdge(currentEdges, edges[i]);
helpers[i] = v;
} else if (v.type === VERTEX_MERGE) {
m = v.prev.index;
if (helpers[m].type === VERTEX_MERGE) addDiagonal(v, helpers[m]);
removeEdge(currentEdges, edges[m]);
j = lowerEdge(currentEdges, v).index;
if (helpers[j].type === VERTEX_MERGE) addDiagonal(v, helpers[j]);
helpers[j] = v;
} else {
if (below(v, v.prev)) {
m = v.prev.index;
if (helpers[m].type === VERTEX_MERGE) addDiagonal(v, helpers[m]);
removeEdge(currentEdges, edges[m]);
insertEdge(currentEdges, edges[i]);
helpers[i] = v;
} else {
j = lowerEdge(currentEdges, v).index;
if (helpers[j].type === VERTEX_MERGE) addDiagonal(v, helpers[j]);
helpers[j] = v;
}
}
}
}
function addDiagonal(v1, v2) {
// debugger;
// console.log('add!');
drawPoly([[v1.x, v1.y], [v2.x, v2.y]], 'red');
}
function compareVertices(p1, p2) {
return (p1.y - p2.y) || (p1.x - p2.x);
}
function below(p1, p2) {
return ((p1.y - p2.y) || (p1.x - p2.x)) > 0;
}
function isConvex(p1, p2, p3) {
return (p3.y - p1.y) * (p2.x - p1.x) - (p3.x - p1.x) * (p2.y - p1.y) > 0;
}
function makeNode(p, index) {
var node = {
x: p[0],
y: p[1],
index: index,
prev: null,
next: null,
type: null
};
return node;
}
function insertEdge(edges, edge) {
var i = 0,
j = edges.length - 1;
while (i <= j) {
var mid = Math.floor((i + j) / 2);
if (compareEdges(edge, edges[mid])) j = mid - 1;
else i = mid + 1;
}
edges.splice(i, 0, edge);
}
function lowerEdge(edges, p) {
var i = 0,
j = edges.length - 1,
edge = new Edge(p, p);
while (i <= j) {
var mid = Math.floor((i + j) / 2);
if (compareEdges(edge, edges[mid])) j = mid - 1;
else i = mid + 1;
}
return edges[i - 1];
}
function removeEdge(edges, edge) {
var i = 0,
j = edges.length - 1;
while (i <= j) {
var mid = Math.floor((i + j) / 2);
if (edge === edges[mid]) {
edges.splice(mid, 1);
return;
}
if (compareEdges(edge, edges[mid])) j = mid - 1;
else i = mid + 1;
}
}
function compareEdges(e1, e2) {
return e2.p1.y === e2.p2.y ? (e1.p1.y === e1.p2.y ? e1.p1.y < e2.p1.y : isConvex(e1.p1, e1.p2, e2.p1)) :
e1.p1.y <= e1.p2.y ? !isConvex(e2.p1, e2.p2, e1.p1) : isConvex(e1.p1, e1.p2, e2.p1);
}
function Edge(p1, p2, index) {
this.p1 = p1;
this.p2 = p2;
this.index = index;
}
// function findEdge(items, edge) {
// var i = 0,
// j = items.length - 1;
// while (i <= j) {
// var mid = Math.floor((i + j) / 2);
// var c = compareEdges(edge, items[mid]);
// if (c === 0) return mid;
// if (c < 0) j = mid - 1;
// else i = mid + 1;
// }
// return null;
// }
// function removeEdge(items, edge) {
// var index = findEdge(items, edge);
// if (index !== null) items.splice(index, 1);
// }