forked from CodingTrain/QuadTree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadtree.js
282 lines (244 loc) · 7.2 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
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// QuadTree
class Point {
constructor(x, y, data) {
this.x = x;
this.y = y;
this.userData = data;
}
}
class Rectangle {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
contains(point) {
return (point.x >= this.x - this.w &&
point.x <= this.x + this.w &&
point.y >= this.y - this.h &&
point.y <= this.y + this.h);
}
intersects(range) {
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.h);
}
}
// circle class for a circle shaped query
class Circle {
constructor(x, y, r) {
this.x = x;
this.y = y;
this.r = r;
this.rSquared = this.r * this.r;
}
contains(point) {
// check if the point is in the circle by checking if the euclidean distance of
// the point and the center of the circle if smaller or equal to the radius of
// the circle
let d = Math.pow((point.x - this.x), 2) + Math.pow((point.y - this.y), 2);
return d <= this.rSquared;
}
intersects(range) {
let xDist = Math.abs(range.x - this.x);
let yDist = Math.abs(range.y - this.y);
// radius of the circle
let r = this.r;
let w = range.w;
let h = range.h;
let edges = Math.pow((xDist - w), 2) + Math.pow((yDist - h), 2);
// no intersection
if (xDist > (r + w) || yDist > (r + h))
return false;
// intersection within the circle
if (xDist <= w || yDist <= h)
return true;
// intersection on the edge of the circle
return edges <= this.rSquared;
}
}
class QuadTree {
constructor(boundary, capacity) {
if (!boundary) {
throw TypeError('boundary is null or undefined');
}
if (!(boundary instanceof Rectangle)) {
throw TypeError('boundary should be a Rectangle');
}
if (typeof capacity !== 'number') {
throw TypeError(`capacity should be a number but is a ${typeof capacity}`);
}
if (capacity < 1) {
throw RangeError('capacity must be greater than 0');
}
this.boundary = boundary;
this.capacity = capacity;
this.points = [];
this.divided = false;
}
toJSON(isChild) {
let obj = { points: this.points };
if (this.divided) {
if (this.northeast.points.length > 0) {
obj.ne = this.northeast.toJSON(true);
}
if (this.northwest.points.length > 0) {
obj.nw = this.northwest.toJSON(true);
}
if (this.southeast.points.length > 0) {
obj.se = this.southeast.toJSON(true);
}
if (this.southwest.points.length > 0) {
obj.sw = this.southwest.toJSON(true);
}
}
if (!isChild) {
obj.capacity = this.capacity;
obj.x = this.boundary.x;
obj.y = this.boundary.y;
obj.w = this.boundary.w;
obj.h = this.boundary.h;
}
return obj;
}
static fromJSON(obj, x, y, w, h, capacity) {
if (typeof x === "undefined") {
if ("x" in obj) {
x = obj.x;
y = obj.y;
w = obj.w;
h = obj.h;
capacity = obj.capacity;
} else {
throw TypeError("JSON missing boundary information");
}
}
let qt = new QuadTree(new Rectangle(x, y, w, h), capacity);
qt.points = obj.points;
if (
"ne" in obj ||
"nw" in obj ||
"se" in obj ||
"sw" in obj
) {
let x = qt.boundary.x;
let y = qt.boundary.y;
let w = qt.boundary.w / 2;
let h = qt.boundary.h / 2;
if ("ne" in obj) {
qt.northeast = QuadTree.fromJSON(obj.ne, x + w, y - h, w, h, capacity);
} else {
qt.northeast = new QuadTree(new Rectangle(x + w, y - h, w, h), capacity);
}
if ("nw" in obj) {
qt.northwest = QuadTree.fromJSON(obj.nw, x - w, y - h, w, h, capacity);
} else {
qt.northwest = new QuadTree(new Rectangle(x - w, y - h, w, h), capacity);
}
if ("se" in obj) {
qt.southeast = QuadTree.fromJSON(obj.se, x + w, y + h, w, h, capacity);
} else {
qt.southeast = new QuadTree(new Rectangle(x + w, y + h, w, h), capacity);
}
if ("sw" in obj) {
qt.southwest = QuadTree.fromJSON(obj.sw, x - w, y + h, w, h, capacity);
} else {
qt.southwest = new QuadTree(new Rectangle(x - w, y + h, w, h), capacity);
}
qt.divided = true;
}
return qt;
}
subdivide() {
let x = this.boundary.x;
let y = this.boundary.y;
let w = this.boundary.w / 2;
let h = this.boundary.h / 2;
let ne = new Rectangle(x + w, y - h, w, h);
this.northeast = new QuadTree(ne, this.capacity);
let nw = new Rectangle(x - w, y - h, w, h);
this.northwest = new QuadTree(nw, this.capacity);
let se = new Rectangle(x + w, y + h, w, h);
this.southeast = new QuadTree(se, this.capacity);
let sw = new Rectangle(x - w, y + h, w, h);
this.southwest = new QuadTree(sw, this.capacity);
this.divided = true;
}
insert(point) {
if (!this.boundary.contains(point)) {
return false;
}
if (this.points.length < this.capacity) {
this.points.push(point);
return true;
}
if (!this.divided) {
this.subdivide();
}
return (this.northeast.insert(point) || this.northwest.insert(point) ||
this.southeast.insert(point) || this.southwest.insert(point));
}
query(range, found) {
if (!found) {
found = [];
}
if (!range.intersects(this.boundary)) {
return found;
}
for (let p of this.points) {
if (range.contains(p)) {
found.push(p);
}
}
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;
}
closest(point, count, startingSize) {
if (typeof count === "undefined") {
count = 1;
}
if (typeof startingSize === "undefined") {
startingSize = 1;
}
if (!this.divided) {
// Empty
if (this.points.length == 0) {
return [];
}
// Limit to number of points in this QuadTree
if (this.points.length < count) {
count = this.points.length;
}
}
// optimized, expanding binary search
// start with a small circle, rapidly expand, slowly shrink
let radius = startingSize;
let limit = 16;
while (true) {
const range = new Circle(point.x, point.y, radius);
const points = this.query(range);
if (points.length === count) {
return points; // Return when we hit the right size
} else if (points.length < count) {
radius *= 2;
} else if (limit-- <= 0) {
return points.slice(0, count); // Slice to return correct count (breaks ties)
} else {
radius /= 3;
}
}
}
}
if (typeof module !== "undefined") {
module.exports = { Point, Rectangle, QuadTree, Circle };
}