-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquadtree.js
More file actions
168 lines (140 loc) · 4.32 KB
/
quadtree.js
File metadata and controls
168 lines (140 loc) · 4.32 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
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
class Point {
constructor(x, y, userData) {
this.location = createVector(x, y);
this.userData = userData;
}
}
class Rectangle {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
contains(point) {
return point.location.x >= this.x - this.w &&
point.location.x < this.x + this.w &&
point.location.y <= this.y + this.h &&
point.location.y > this.y - this.h;
}
intersects(range) {
if (undefined !== range.w || undefined !== range.h) {
return !(range.x - range.w > this.x + this.w ||
range.x + range.w < this.x - this.w ||
range.y - range.h > this.y + this.h ||
range.y + range.h < this.y - this.w);
}
if (undefined !== range.r) {
// if any edge of rectangle inside circle, return true
let xdiff = abs(range.x - this.x) - this.w,
ydiff = abs(range.y - this.y) - this.h;
if (xdiff > range.r || ydiff > range.r) {
return false;
}
if (xdiff <= 0 || ydiff <= 0) {
return true;
}
return (xdiff * xdiff + ydiff * ydiff <= range.rSquared);
}
return undefined;
}
}
class Circle {
constructor(x, y, r) {
this.x = x;
this.y = y;
this.r = r;
this.rSquared = r * r;
}
contains(point) {
let xdiff = this.x - point.location.x;
let ydiff = this.y - point.location.y;
return (xdiff * xdiff + ydiff * ydiff) <= this.rSquared;
}
}
class QuadTree {
constructor(boundary, capacity) {
this.boundary = boundary;
this.capacity = capacity;
this.points = [];
this.divided = false;
}
insert(point) {
if (!this.boundary.contains(point)) {
return false;
}
if (this.divided) {
return this.insertIntoDivisions(point);
}
if (this.points.length < this.capacity) {
this.points.push(point);
return true;
}
this.subdivide();
// re-distribute points of this container to its subdivisions
let pts = this.points;
this.points = [];
for (let p of pts) {
this.insertIntoDivisions(p);
}
this.insertIntoDivisions(point);
}
insertIntoDivisions(point) {
return this.northWest.insert(point) ||
this.northEast.insert(point) ||
this.southEast.insert(point) ||
this.southWest.insert(point);
}
subdivide() {
let x = this.boundary.x;
let y = this.boundary.y;
let w = this.boundary.w;
let h = this.boundary.h;
let nw = new Rectangle(x - w/2, y - h/2, w/2, h/2);
this.northWest = new QuadTree(nw, this.capacity);
let ne = new Rectangle(x + w/2, y - h/2, w/2, h/2);
this.northEast = new QuadTree(ne, this.capacity);
let se = new Rectangle(x + w/2, y + h/2, w/2, h/2);
this.southEast = new QuadTree(se, this.capacity);
let sw = new Rectangle(x - w/2, y + h/2, w/2, h/2);
this.southWest = new QuadTree(sw, this.capacity);
this.divided = true;
}
queryRange(range, found) {
if (!found) {
found = [];
}
if (!this.boundary.intersects(range)) {
return found;
}
for (let p of this.points) {
if (range.contains(p)) {
found.push(p);
}
}
if (this.divided) {
this.northEast.queryRange(range, found);
this.northWest.queryRange(range, found);
this.southEast.queryRange(range, found);
this.southWest.queryRange(range, found);
}
return found;
}
show() {
stroke(255);
strokeWeight(1);
noFill();
rectMode(CENTER);
rect(this.boundary.x, this.boundary.y, this.boundary.w * 2, this.boundary.h * 2);
if (this.divided) {
this.northWest.show();
this.northEast.show();
this.southEast.show()
this.southWest.show()
}
for (let p of this.points) {
strokeWeight(2);
point(p.location.x, p.location.y);
}
}
}