-
Notifications
You must be signed in to change notification settings - Fork 0
/
quadTree.js
273 lines (202 loc) · 5.41 KB
/
quadTree.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
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
class QuadTree {
constructor(x, y, w, h, cap) {
this.r = new Rect(x, y, w, h);
this.points = [];
this.root = new Quadrant(this.r.x, this.r.y, this.r.w, this.r.h, this);
this.c = max(1, cap);
}
getC() {return this.c;}
getPoints() {return this.points;}
getLeaves() {
return this.root.getLeaves();
}
clear() {
this.points = [];
this.root = new Quadrant(this.r.x, this.r.y, this.r.w, this.r.h, this);
return true;
}
build() {
let points = this.points;
this.clear();
for (let p of points) {
this.ins(p);
}
return true;
}
ins(p) {
//Insert the point if it's a Pnt and is contained in the QuadTree
if (p instanceof Pnt && this.r.contains(p)) {
for (let p2 of this.points) {
if (p2.x == p.x && p2.y == p.y) {return false;}
}
//Add it to the log and the quadtree
if (this.root.ins(p)) {this.points.push(p); return true;}
}
return false;
}
query(zone) {
let nearLeaves = [];
let leaves = this.getLeaves();
if (!(zone instanceof Circ) && !(zone instanceof Rect)) {return "ERROR query(): Zone is not a valid shape";}
for (let i = 0; i < leaves.length; i++) {
if (zone.intersects(leaves[i].r)) {nearLeaves.push(leaves[i]);}
}
return nearLeaves;
}
queryPoints(zone) {
let nearPoints = [];
let leaves = this.getLeaves();
if (!(zone instanceof Circ) && !(zone instanceof Rect)) {return "ERROR query(): Zone is not a valid shape";}
for (let quad of leaves) {
if (zone.intersects(quad.r)) {
for (let dot of quad.points) {
if (zone.intersects(new Circ(dot.x, dot.y, dot.data[2]))) {
nearPoints.push(dot);
}
}
}
}
return nearPoints;
}
}
class Pnt {
constructor(x, y, data = null) {
this.x = x;
this.y = y;
this.data = data;
}
}
class Quadrant {
constructor(x, y, w, h, qt) {
this.r = new Rect(x, y, w, h);
this.qt = qt;
this.points = [];
this.divided = false;
}
ins(p) {
if (!this.r.contains(p)) {return false;}
if (this.divided) {
//Insert the point into only one subquadrant
if (this.ne.ins(p)) {return true;}
if (this.nw.ins(p)) {return true;}
if (this.sw.ins(p)) {return true;}
if (this.se.ins(p)) {return true;}
return false;
} else if (qt.getC() > this.points.length) {
this.points.push(p);
return true;
} else if (this.divide()){
//Divide and push all points into the subQuadrants
this.points.push(p);
let points = this.points;
this.points = [];
for (let p of points) {
//Insert the point only into the first Quadrant that accepts it
if (this.ne.ins(p)) {continue;}
if (this.nw.ins(p)) {continue;}
if (this.sw.ins(p)) {continue;}
if (this.se.ins(p)) {continue;}
return false;
}
return true;
} else {return false;}
}
divide() {
//Create all the subdivision Quadrants
this.ne = new Quadrant(this.r.x + this.r.w/2, this.r.y, this.r.w/2, this.r.h/2, this.qt);
this.nw = new Quadrant(this.r.x, this.r.y, this.r.w/2, this.r.h/2, this.qt);
this.sw = new Quadrant(this.r.x, this.r.y + this.r.h/2, this.r.w/2, this.r.h/2, this.qt);
this.se = new Quadrant(this.r.x + this.r.w/2, this.r.y + this.r.h/2, this.r.w/2, this.r.h/2, this.qt);
this.divided = true;
return true;
}
getLeaves() {
if (this.divided) {
let leaves = [];
Array.prototype.push.apply(leaves, this.ne.getLeaves());
Array.prototype.push.apply(leaves, this.nw.getLeaves());
Array.prototype.push.apply(leaves, this.sw.getLeaves());
Array.prototype.push.apply(leaves, this.se.getLeaves());
return leaves;
}
return [this];
}
}
class Rect {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
contains(p) {
if (p instanceof Pnt) {
return !(this.x > p.x
|| this.x + this.w < p.x
|| this.y > p.y
|| this.y + this.h < p.y
);
}
return false;
}
intersects(r) {
if (r instanceof Circ) {
//Get the min distance between the circle's center and the rect edges
let dX = r.x - max(this.x, min(r.x, this.x + this.w));
let dY = r.y - max(this.y, min(r.y, this.y + this.h));
//If the closest point of the Rect is inside the circle
return (dX * dX + dY * dY) < (r.rr);
}
if (r instanceof Rect) {
return !(this.x + this.w < r.x
|| this.x > r.x + r.w
|| this.y + this.h < r.y
|| this.y > r.y + r.h
);
}
//If the range is not a Rect or a Circ
return false;
}
}
class Circ {
constructor(x, y, r) {
this.x = x;
this.y = y;
this.r = r;
this.r2 = r + r;
this.rr = r * r;
}
radius(radius) {
if (isNaN(radius)) {return this.r;}
this.r = radius;
this.r2 = radius + radius;
this.rr = radius * radius;
return this.r;
}
contains(p) {
if (p instanceof Pnt) {
let dX = this.x - p.x;
let dY = this.y - p.y;
return (this.rr) > (dX * dX + dY * dY);
}
return false;
}
intersects(r) {
if (r instanceof Circ) {
let dX = r.x - this.x;
let dY = r.y - this.y;
let dR = this.r + r.r;
//If the distance between centers is less than the sum of the radius
return (dR * dR) > (dX * dX) + (dY * dY);
}
if (r instanceof Rect) {
//Get the min distance between the circle's center and the rect
let dX = this.x - max(r.x, min(this.x, r.x + r.w));
let dY = this.y - max(r.y, min(this.y, r.y + r.h));
//If the closest point of the Rect is inside the circle
return (dX * dX + dY * dY) < (this.rr);
}
//If the range is not a Rect or a Circ
return false;
}
}