-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquadtree.js
More file actions
94 lines (88 loc) · 2.75 KB
/
quadtree.js
File metadata and controls
94 lines (88 loc) · 2.75 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
export class Quadtree {
constructor(bounds, capacity) {
this.bounds = bounds;
this.capacity = capacity;
this.points = [];
this.divided = false;
}
subdivide() {
const x = this.bounds.x;
const y = this.bounds.y;
const w = this.bounds.width / 2;
const h = this.bounds.height / 2;
this.northeast = new Quadtree({ x: x + w, y: y, width: w, height: h }, this.capacity);
this.northwest = new Quadtree({ x, y, width: w, height: h }, this.capacity);
this.southeast = new Quadtree({ x: x + w, y: y + h, width: w, height: h }, this.capacity);
this.southwest = new Quadtree({ x, y: y + h, width: w, height: h }, this.capacity);
this.divided = true;
}
insert(point) {
if(point.x < this.bounds.x || point.x > this.bounds.x + this.bounds.width || point.y < this.bounds.y || point.y > this.bounds.y + this.bounds.height) {
return false;
}
if (this.points.length < this.capacity) {
this.points.push(point);
return true;
}
if (!this.divided) {
this.subdivide();
}
if (this.northeast.insert(point) || this.northwest.insert(point) ||
this.southeast.insert(point) || this.southwest.insert(point)) {
return true;
}
}
query(range, found) {
if (!found) {
found = [];
}
if (!range.intersects(this.bounds)) {
return found;
}
for (const point of this.points) {
if (range.contains(point)) {
found.push(point);
}
}
if (this.divided) {
this.northwest.query(range, found);
this.northeast.query(range, found);
this.southwest.query(range, found);
this.southeast.query(range, found);
}
return found;
}
clear() {
this.points = [];
this.divided = false;
this.northeast = null;
this.northwest = null;
this.southeast = null;
this.southwest = null;
}
retrieve(range) {
let found = [];
if (!this.intersects(range)) {
return found;
}
for (const point of this.points) {
if (this.contains(range, point)) {
found.push(point);
}
}
if (this.divided) {
found = found.concat(this.northeast.retrieve(range));
found = found.concat(this.northwest.retrieve(range));
found = found.concat(this.southeast.retrieve(range));
found = found.concat(this.southwest.retrieve(range));
}
return found;
}
intersects (range) {
return !(range.x > this.bounds.x + this.bounds.width || range.x + range.width < this.bounds.x || range.y > this.bounds.y + this.bounds.height || range.y + range.height < this.bounds.y);
}
contains(range, point) {
return point.x >= this.bounds.x && point.x <= this.bounds.x + this.bounds.width &&
point.y >= this.bounds.y && point.y <= this.bounds.y + this.bounds.height;
}
}