-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle.umd.js
524 lines (490 loc) · 18.3 KB
/
bundle.umd.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.Q43 = {})));
}(this, (function (exports) { 'use strict';
/**
* Clamping function
*/
let clamp = (a, b, c) => Math.min(Math.max(a, b), c);
/**
* Calculate whether x is within min and max
*/
let inRange = (min, x, max) => clamp(min, x, max) === x;
/**
* Checks if point is inside bounds
*/
let hasPoint = (px, py, ax, ay, bx, by) => inRange(ax, px, bx) && inRange(ay, py, by);
/**
* Checks if two boundaries intersects
*/
let intersects = (ax, ay, bx, by, cx, cy, dx, dy) => {
return hasPoint(cx, cy, ax, ay, bx, by) ||
hasPoint(cx, dy, ax, ay, bx, by) ||
hasPoint(dx, cy, ax, ay, bx, by) ||
hasPoint(dx, dy, ax, ay, bx, by);
};
/**
* Checks if point is within radius
*/
let inRadius = (ax, ay, bx, by, r) => (bx - ax)*(bx - ax) + (by - ay)*(by - ay) <= r*r;
/**
* The default amount of points a Q43 instance can store
* if there is no capacity value passed when using the Q43 constructor
*/
const Q43_CAP = 4;
class Q43{
/**
* Create a Q43 instance
* @param {Number} ax - minimum X of the Q43 boundary
* @param {Number} ay - minimum Y of the Q43 boundary
* @param {Number} bx - maximum X of the Q43 boundary
* @param {Number} by - maximum Y of the Q43 boundary
* @param {Number} cap - The amount of points a Q43 instance can store.
* @returns {Q43}
*/
constructor(ax, ay, bx, by, cap = Q43_CAP){
this.minX = ax;
this.minY = ay;
this.maxX = bx;
this.maxY = by;
this.capacity = cap;
this.points = [];
this.subdivided = false;
}
/**
* Check if x is within the Q43's x-axis
* @param {Number} px
* @returns {Boolean}
*/
containsX(px){
return inRange(this.minX, px, this.maxX);
}
/**
* Check if y is within the Q43's x-axis
* @param {Number} py
* @returns {Boolean}
*/
containsY(py){
return inRange(this.minY, py, this.maxY);
}
/**
* Check if point is within the Q43's x-axis
* @param {Number} px
* @param {Number} py
* @returns {Boolean}
*/
containsPoint(px, py){
return hasPoint(px, py, this.minX, this.minY, this.maxX, this.maxY);
}
/**
* Subdivide the Q43 into four equal parts.
*/
subdivide(){
/**
* Check if the Q43 instance
* has subdivided before
*/
if(!this.subdivided){
/**
* Prevent from subdividing again
*/
this.subdivided = true;
/**
* Calculate the midpoint of a the quadtree
*/
let ax = this.minX, ay = this.minY, bx = this.maxX, by = this.maxY;
let cx = ax + (bx - ax)*0.5, cy = ay + (by - ay)*0.5;
/**
* Create child Q43s from the equally divided boundaries
*/
this.SW = new Q43(ax, ay, cx, cy, this.capacity);
this.SE = new Q43(cx, ay, bx, cy, this.capacity);
this.NW = new Q43(ax, cy, cx, by, this.capacity);
this.NE = new Q43(cx, cy, bx, by, this.capacity);
}
}
/**
* Insert a point to the Q43
* @param {Array} pt
* @returns {Boolean}
*/
insert(pt){
if(typeof pt !== "undefined"){
/**
* Check if the coordinates are within the boundaries of the Q43
*/
if(this.containsPoint(pt[0], pt[1])){
/**
* Check if there is still space available
* for the Q43 to store points
*/
if(this.points.length < this.capacity){
/**
* store the points
*/
this.points.push(pt);
return true;
}
/**
* Because the Q43 has hit the limit,
* we need to subdivide the Q43
*/
this.subdivide();
/**
* Insert the point to all child (only one will be succesful)
*/
if(this.SW.insert(pt)) {return true;}
if(this.SE.insert(pt)) {return true;}
if(this.NW.insert(pt)) {return true;}
if(this.NE.insert(pt)) {return true;}
}
}
return undefined;
}
/**
* Remove a point from the Q43
* @param {Array} pt
* @returns {Boolean}
*/
remove(pt){
if(typeof pt !== "undefined"){
/**
* Iterate all points from this instance
*/
for(let i = 0; i < this.points.length; i++){
/**
* Compare point reference
*/
if(this.points[i] === pt){
/**
* Remove the point from the array
*/
this.points.splice(i, 1);
return true;
}
}
/**
* If the Q43 has subdivided, try removing from the child nodes.
*/
if(this.subdivided){
if(this.SW.remove(pt)){return true;}
if(this.SE.remove(pt)){return true;}
if(this.NW.remove(pt)){return true;}
if(this.NE.remove(pt)){return true;}
}
}
return false;
}
/**
* Get all points from the Q43 given a condition
* @param {Function} cond - the filter function
* @returns {Array} - array of points
*/
query(cond){
if(typeof cond === "function"){
let pt = [];
/**
* Iterate all points from this node
*/
for(let i = 0; i < this.points.length; i++){
let p = this.points[i];
/**
* Pass the point to the handler
*/
if(cond(p)){
/**
* if it passes the custom condition,
* add to the array of points
*/
pt.push(p);
}
}
/**
* Iterate to the children Q43s
*/
if(this.subdivided){
pt = pt.concat(this.SW.query(cond));
pt = pt.concat(this.SE.query(cond));
pt = pt.concat(this.NW.query(cond));
pt = pt.concat(this.NE.query(cond));
}
return pt;
}
return [];
}
/**
* Get all points from the Q43
* @returns {Array} - array of points
*/
all(){
let pt = [];
/**
* Push all points of this Q43
*/
for(let i = 0; i < this.points.length; i++){
pt.push(this.points[i]);
}
/**
* Push all points from the children Q43s
*/
if(this.subdivided){
pt = pt.concat(this.SW.all());
pt = pt.concat(this.SE.all());
pt = pt.concat(this.NW.all());
pt = pt.concat(this.NE.all());
}
return pt;
}
/**
* Get all points from a Q43 that is within a given boundary and passes the condition
* @param {Number} ax - minimum x of the given boundary
* @param {Number} ay - minimum y of the given boundary
* @param {Number} bx - maximum x of the given boundary
* @param {Number} by - maximum y of the given boundary
* @param {Function} cond - a filter function, optional
* @returns {Array} - array of points
*/
queryBounds(ax, ay, bx, by, cond){
if(typeof ax === "number" && typeof ay === "number" && typeof bx === "number" && typeof by === "number"){
let c = typeof cond === "function";
let pt = [];
/**
* Check if boundaries are within or intersecting the Q43
*/
if(intersects(this.minX, this.minY, this.maxX, this.maxY, ax, ay, bx, by)){
/**
* Iterate all points from this Q43
*/
for(let i = 0; i < this.points.length; i++){
let p = this.points[i];
let b = true;
/**
* if ever the user didn't pass a filter,
* just let this other compared value to true
*/
if(c){
/**
* Pass the point for filter
*/
b = cond(p);
}
if(containsPoint(p[0], p[1], ax, ay, bx, by) && b){
/**
* Push point if it is within bounds and passes the filter
*/
pt.push(p);
}
}
/**
* Iterate all points from the children Q43s
*/
if(this.subdivided){
pt = pt.concat(this.SW.queryBounds(ax, ay, bx, by, cond));
pt = pt.concat(this.SE.queryBounds(ax, ay, bx, by, cond));
pt = pt.concat(this.NW.queryBounds(ax, ay, bx, by, cond));
pt = pt.concat(this.NE.queryBounds(ax, ay, bx, by, cond));
}
}
return pt;
}
return [];
}
/**
* Gets all points within the radius of another point from the Q43
* @param {Number} px
* @param {Number} py
* @param {Number} radius
* @param {Function} cond - filter function, optional
* @returns {Array}
*/
queryRange(px, py, radius, cond){
if(typeof px === "number" && typeof py === "number" && typeof radius === "number"){
let c = typeof cond === "function";
let pt = [];
/**
* Calculate the Axis-align bounding box of the radius
* This is so that it can still get the points even if the source point
* of the radius is outside the boundaries as long as it intersects the
* Q43's boundary
*/
let ax = px - radius, ay = py - radius, bx = px + radius, by = py + radius;
if(intersects(this.minX, this.minY, this.maxX, this.maxY, ax, ay, bx, by)){
for(let i = 0; i < this.points.length; i++){
let p = this.points[i];
let b = true;
if(c){
b = cond(p);
}
if(inRadius(p[0], p[1], px, py, radius) && b){
pt.push(p);
}
}
if(this.subdivided){
pt = pt.concat(this.SW.queryRange(px, py, radius, cond));
pt = pt.concat(this.SE.queryRange(px, py, radius, cond));
pt = pt.concat(this.NW.queryRange(px, py, radius, cond));
pt = pt.concat(this.NE.queryRange(px, py, radius, cond));
}
}
return pt;
}
return [];
}
/**
* Iterates all points in the Q43 and passes them to a handler function
* @param {Function} handler
*/
for(handler){
if(typeof handler === "function"){
/**
* Pass all points of this Q43 to the handler
*/
for(let i = 0; i < this.points.length; i++){
let p = this.points[i];
handler(p);
}
/**
* Pass all points of the children Q43s to the handler
*/
if(this.subdivided){
this.SW.for(handler);
this.SE.for(handler);
this.NW.for(handler);
this.NE.for(handler);
}
}
}
/**
* Iterates all points in the Q43 that are within the bounds
* and passes them to a handler function
* @param {Number} ax
* @param {Number} ay
* @param {Number} bx
* @param {Number} by
* @param {Function} handler
*/
forBounds(ax, ay, bx, by, handler){
if(typeof ax === "number" && typeof ay === "number" && typeof bx === "number" && typeof by === "number" && typeof handler === "function"){
if(intersects(this.minX, this.minY, this.maxX, this.maxY, ax, ay, bx, by)){
for(let i = 0; i < this.points.length; i++){
let p = this.points[i];
if(containsPoint(p[0], p[1], ax, ay, bx, by)){
handler(p);
}
}
if(this.subdivided){
this.SW.forBounds(ax, ay, bx, by, handler);
this.SE.forBounds(ax, ay, bx, by, handler);
this.NW.forBounds(ax, ay, bx, by, handler);
this.NE.forBounds(ax, ay, bx, by, handler);
}
}
}
}
/**
* Iterates all points within the radius of another point in the Q43
* and passes them to a handler.
* @param {Number} px
* @param {Number} py
* @param {Number} radius
* @param {Function} handler
*/
forRange(px, py, radius, handler){
if(typeof px === "number" && typeof py === "number" && typeof radius === "number" && typeof handler === "function"){
let ax = px - radius, ay = py - radius, bx = px + radius, by = py + radius;
if(intersects(this.minX, this.minY, this.maxX, this.maxY, ax, ay, bx, by)){
for(let i = 0; i < this.points.length; i++){
let p = this.points[i];
if(inRadius(p[0], p[1], px, py, radius)){
handler(p);
}
}
if(this.subdivided){
this.SW.forRange(px, py, radius, handler);
this.SE.forRange(px, py, radius, handler);
this.NW.forRange(px, py, radius, handler);
this.NE.forRange(px, py, radius, handler);
}
}
}
}
/**
* Creates a new Q43 with the points from another Q43.
* This is useful for rebuilding a quadtree whose points have updated coordinates.
* @returns {Q43}
*/
rebuild(){
/**
* Create an empty copy of the Q43
*/
let q = new Q43(this.minX, this.minY, this.maxX, this.maxY, this.capacity);
/**
* Start passing points from this Q43 to the copy
*/
this.for(p => q.insert(p));
/**
* return the copy
*/
return q;
}
/**
* Updates the points of the Q43.
* Unlike the rebuild method, the update method does not restore subdivided empty Q43s.
*/
update(){
/**
* To update the Q43, the Q43 must remove first all of
* the points it has then insert it again.
*
* This will not restore empty subidivided Q43s
*/
this.for(p => this.remove(p) && this.insert(p));
}
/**
* Calculates the amount of points a Q43 has.
* @returns {Number}
*/
size(){
let size = this.points.length;
if(this.subdivided){
size += this.NW.size();
size += this.NE.size();
size += this.SW.size();
size += this.SE.size();
}
return size;
}
/**
* For debugging purposes
* @param {CanvasRenderingContext2D} ctx
* @param {Boolean} tree
* @param {Boolean} point
*/
draw(ctx, tree, point){
let mx = this.minX,
my = this.minY;
let dx = this.maxX - mx,
dy = this.maxY - my;
if(tree){
ctx.strokeStyle = "#0ff";
ctx.strokeRect(mx, my, dx, dy);
}
if(this.subdivided){
this.NW.draw(ctx, tree, point);
this.NE.draw(ctx, tree, point);
this.SW.draw(ctx, tree, point);
this.SE.draw(ctx, tree, point);
}
if(point){
for(let i = 0; i < this.points.length; i++){
ctx.strokeStyle = "#f00";
ctx.strokeRect(this.points[i][0], this.points[i][1], 1, 1);
}
}
}
}
exports.Q43_CAP = Q43_CAP;
exports.default = Q43;
Object.defineProperty(exports, '__esModule', { value: true });
})));