-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadTree.js
178 lines (154 loc) · 3.9 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
class Point {
constructor(x, y, userData) {
this.x = x;
this.y = 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.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) {
var xDist = Math.abs(range.x - this.x);
var yDist = Math.abs(range.y - this.y);
// radius of the circle
var r = this.r;
var w = range.w;
var h = range.h;
var 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, n) {
this.boundary = boundary;
this.capacity = n;
this.points = [];
this.divided = false;
}
clear() {
this.divided = false;
this.points = [];
}
subdivide() {
let x = this.boundary.x;
let y = this.boundary.y;
let w = this.boundary.w;
let h = this.boundary.h;
let ne = new Rectangle(x + w / 2, y - h / 2, w / 2, h / 2);
this.northEast = new QuadTree(ne, this.capacity);
let nw = new Rectangle(x - w / 2, y - h / 2, w / 2, h / 2);
this.northWest = new QuadTree(nw, 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;
}
insert(point) {
if (!this.boundary.contains(point)) {
return;
}
if (this.points.length < this.capacity) {
this.points.push(point);
return true;
} else {
if (!this.divided) {
this.subdivide();
}
if (this.northEast.insert(point)) {
return true;
} else if (this.northWest.insert(point)) {
return true;
} else if (this.southeast.insert(point)) {
return true;
} else if (this.southWest.insert(point)) {
return true;
}
}
}
query(range, found) {
if (!found) {
found = [];
}
if (!this.boundary.intersects(range)) {
return;
} else {
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;
}
show() {
stroke(80);
strokeWeight(0.5);
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.southWest.show();
this.southeast.show();
}
// for (let p of this.points) {
// strokeWeight(2);
// point(p.x, p.y);
// }
}
}