-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadtree.js
363 lines (321 loc) · 10.7 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Implements a quad tree for region querying.
//
// last edited: 2018-06-02
/**
* @typedef IPoint
* @property {number} x
* @property {number} y
*/
/**
* @typedef IRect
* @property {Rect} rect
*/
/**
* A rectangle defined by its lower (in magnitude) and higher (in magnitude)
* corners. Also implements point and rectangle collision.
*/
class Rect {
/**
* Constructs a Rect.
* @param {number} x1 the x coord of the lower corner
* @param {number} y1 the y coord of the lower corner
* @param {number} x2 the x coord of the higher corner
* @param {number} y2 the y coord of the higher corner
*/
constructor(x1, y1, x2, y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
/**
* The center of the Rect.
* @returns a 2 item array of the center point, `[x, y]`
*/
get Center() { return [this.x1 + (0.5 * this.x2 - this.x1), this.y1 + (0.5 * this.y2 - this.y1)]; }
/** The width of the Rect */
get Width() { return this.x2 - this.x1; }
/** The height of the Rect. */
get Height() { return this.y2 - this.y1; }
/** The Area of the Rect. */
get Area() { return this.Width * this.Height; }
/**
* Returns true if this rect contains the point at (x,y).
* @param {number} x the x coord to check
* @param {number} y the y coord to check
*/
containsPoint(x, y) {
return this.x1 <= x && x < this.x2 && this.y1 <= y && y < this.y2;
}
/**
* True if other is completely contained within this Rect.
* @param {Rect} other the other rectangle
*/
containsRect(other) {
return this.x1 <= other.x1 && this.y1 <= other.y1 && this.x2 >= other.x2 && this.y2 >= other.y2;
}
/**
* Returns true if the 'other' rect intersects with this one.
* @param {Rect} other the other Rect
*/
intersects(other) {
return !(this.x1 > other.x2 || this.x2 < other.x1) &&
!(this.y1 > other.y2 || this.y2 < other.y1);
}
/** Draws a rect to the screen. */
draw() {
rect(this.x1, this.y1, this.Width, this.Height);
}
}
/** A very simple 2D point. */
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
/**
* A quad-tree whose nodes can contain an arbitrary number of points. If 'toLeaves'
* is set to 'true', only the leaves of the tree will contain points (intermediate
* nodes will have a capacity of 0).
*/
class PointQuadTree {
/**
* Constructs a new QuadTree.
* @param {Rect} range the area that is covered by this quadtree(node)
* @param {number} capacity the interger number of how many items each node
* can contain before splitting
* @param {boolean} toLeaves if true, items are pushed to leaves when the
* node splits.
*/
constructor(range, capacity = 1, toLeaves = false) {
this.range = range;
this.capacity = capacity;
if (this.capacity < 1) { this.capacity = 1; }
this.points = [];
// 4 quadrants L = lower side, H = higher side.
this.LL = null;
this.LH = null;
this.HL = null;
this.HH = null;
this._isSplit = false;
this._pushToLeaves = toLeaves;
}
/**
* Inserts a Point into the tree. Returns true/false if the point was inserted.
* @param {IPoint} point any object that has numeric properties 'x' and 'y'
*/
insert(point) {
if (!this.range.containsPoint(point.x, point.y)) {
return false;
}
if (this.points.length < this.capacity) {
this.points.push(point);
} else {
if (!this.isSplit()) {
this.split();
}
this.LL.insert(point);
this.LH.insert(point);
this.HL.insert(point);
this.HH.insert(point);
}
return true;
}
/**
* Returns true if this node has been split, false otherwise.
*/
isSplit() { return this._isSplit; }
/**
* Splits this node into 4 children. pushes points from this node down to
* children if the tree is configured to do so.
*/
split() {
let w = this.range.Width / 2;
let h = this.range.Height / 2;
this.LL = new PointQuadTree(
new Rect(this.range.x1, this.range.y1, this.range.x1 + w, this.range.y1 + h),
this.capacity,
this._pushToLeaves);
this.LH = new PointQuadTree(
new Rect(this.range.x1 + w, this.range.y1, this.range.x2, this.range.y1 + h),
this.capacity,
this._pushToLeaves);
this.HL = new PointQuadTree(
new Rect(this.range.x1, this.range.y1 + h, this.range.x1 + w, this.range.y2),
this.capacity,
this._pushToLeaves);
this.HH = new PointQuadTree(
new Rect(this.range.x1 + w, this.range.y1 + h, this.range.x2, this.range.y2),
this.capacity,
this._pushToLeaves);
this._isSplit = true;
// if the quadtree is set to push all points down to the leaves,
// then this will re-insert the points currently contained in this node.
// because capacity is set to 0, the re-inserted points will go to the
// new children, as will any newly inserted future points.
if (this._pushToLeaves) {
this.capacity = 0;
while (this.points.length > 0) {
let p = this.points.pop();
this.insert(p);
}
}
}
/**
* Returns all points contained in the 'range' Rect. If 'found' is specified,
* the points will be appended to that array. Returns a reference to the
* found array.
* @param {Rect} range rectangular area to search
* @param {IPoint[]} found a list of already found items
* @returns {IPoint[]} a list of found Points
*/
query(range, found = []) {
if (!this.range.intersects(range)) {
return found;
}
for (const p of this.points) {
if (range.containsPoint(p.x, p.y)) {
found.push(p);
}
}
if (this.isSplit()) {
this.LL.query(range, found);
this.LH.query(range, found);
this.HL.query(range, found);
this.HH.query(range, found);
}
return found;
}
/**
* draws the quad tree (its regions) to the screen.
*/
draw() {
this.range.draw();
if (this.isSplit()) {
this.LL.draw();
this.LH.draw();
this.HL.draw();
this.HH.draw();
}
}
}
/**
* A quad-tree whose nodes can contain an arbitrary number of rectangles.
*/
class RectQuadTree {
/**
* Constructs a new RectQuadTree.
* @param {Rect} range the area that is covered by this quadtree(node)
*/
constructor(range) {
this.range = range;
this.objs = [];
// 4 quadrants L = lower side, H = higher side.
this.LL = null;
this.LH = null;
this.HL = null;
this.HH = null;
this._isSplit = false;
}
/**
* Inserts a Rect into the tree. Returns true/false if the point was inserted.
* @param {IRect} obj any object that has the property 'rect' which gets a Rect
*/
insert(obj) {
// 1. check if this contains the incoming rect
if (!this.range.containsRect(obj.rect)) {
return false;
}
// 2. if so, check against children and attempt insertion in them.
// 2.1 split node if not already done.
if (!this.isSplit()) {
this.split();
}
let inserted =
this.LL.insert(obj) ||
this.LH.insert(obj) ||
this.HL.insert(obj) ||
this.HH.insert(obj);
// 3. if child insertion fails, keep incoming rect in current node's list of items
if (!inserted) {
this.objs.push(obj);
}
return true;
}
/**
* Returns true if this node has been split, false otherwise.
*/
isSplit() { return this._isSplit; }
/**
* Splits this node into 4 children.
*/
split() {
let w = this.range.Width / 2;
let h = this.range.Height / 2;
this.LL = new RectQuadTree(
new Rect(this.range.x1, this.range.y1, this.range.x1 + w, this.range.y1 + h));
this.LH = new RectQuadTree(
new Rect(this.range.x1 + w, this.range.y1, this.range.x2, this.range.y1 + h));
this.HL = new RectQuadTree(
new Rect(this.range.x1, this.range.y1 + h, this.range.x1 + w, this.range.y2));
this.HH = new RectQuadTree(
new Rect(this.range.x1 + w, this.range.y1 + h, this.range.x2, this.range.y2));
this._isSplit = true;
}
/**
* Returns all objects that intersect with the 'range' Rect. If 'found' is specified,
* the objects will be appended to that array. Returns a reference to the
* found array.
* @param {Rect} range rectangular area to search
* @param {IRect[]} found a list of already found items
* @returns {IRect[]} a list of found objects
*/
query(range, found = []) {
// TODO: add parameter to this method switch between
// test for intersection and test for containment
// 1. check if this node intersects with the query range
if (!this.range.intersects(range)) {
return found;
}
// 2. Two posibilities:
// 1) the current node is completely contained within the query range,
// which means that ALL of its objects and all of its children's objs
// are added to found
// 2) the current node is not completely contained, so each object in
// the node must be checked against the query range
//
// this means that the tree will generally run with less performance
// when most of the nodes are larger than the query range.
if (range.containsRect(this.range)) {
found.push(...this.objs);
} else {
// test for intersection, not containment
for (const o of this.objs) {
if (range.intersects(o.rect)) {
found.push(o);
}
}
}
// 3. check children
if (this.isSplit()) {
this.LL.query(range, found);
this.LH.query(range, found);
this.HL.query(range, found);
this.HH.query(range, found);
}
return found;
}
/**
* draws the quad tree (its regions) to the screen.
*/
draw() {
this.range.draw();
if (this.isSplit()) {
this.LL.draw();
this.LH.draw();
this.HL.draw();
this.HH.draw();
}
}
}