-
Notifications
You must be signed in to change notification settings - Fork 2
/
quadtree.js
216 lines (189 loc) · 5.55 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
'use strict';
var Quadtree = function(box, max){
this.box = box;
this.children = null;
this.value = [];
this.max = max || 10; //max points per node
};
Quadtree.prototype.insert = function(point, object){
//check if should contain point
if (!this.box.contains(point)){
return this;
}
//if is a leaf node and not full, then insert
//need to check if it already exists though
var i;
if (this.children === null && this.value.length < this.max){
for( i = 0; i < this.value.length; i++ ){
if(this.value[i].point.equals(point)){
this.value[i].value = object;
return;
}
}
this.value.push({point: point, value:object});
return this;
}
//if is a leaf node but full, call subdivide
if(this.children === null){
this.subdivide();
}
// if is not a leaf node, call insert on child nodes
for( i = 0; i < this.children.length; i++ ){
this.children[i].insert(point, object);
}
this.value = [];
return this;
};
Quadtree.prototype.subdivide = function(){
//use box quadrant method to create 4 new equal child quadrants
this.children = this.box.split();
for(var i = 0; i < this.children.length; i++){
this.children[i] = new Quadtree(this.children[i], this.max);
}
//try inserting each value into the new child nodes
for(i = 0; i < this.value.length; i++){
for(var k = 0; k < this.children.length; k++){
this.children[k].insert(this.value[i].point, this.value[i].value);
}
}
};
Quadtree.prototype.queryRange = function(box){
//return all point/value pairs contained in range
var result = [];
this._queryRangeRec(box, result);
return result;
};
Quadtree.prototype._queryRangeRec = function(box, result){
//if query area doesn't overlap this box then return
if (!this.box.overlaps(box)){
return;
}
//if leaf node with contained value(s), then check against contained objects
var i;
if(this.value.length > 0){
for( i = 0; i < this.value.length; i++ ){
if(box.contains(this.value[i].point)){
result.push(this.value[i]);
}
}
return;
}
//if has children, then make recursive call on children
if(this.children !== null){
for( i = 0; i < this.children.length; i++ ){
this.children[i]._queryRangeRec(box, result);
}
return;
}
};
Quadtree.prototype.queryPoint = function(point){
//return value if tree contains point
if(!this.box.contains(point)){
return null;
}
if (this.value.length > 0){
for (var i = 0; i < this.value.length; i++){
if (this.value[i].point.equals(point)){
return this.value[i].value;
}
}
}
if (this.children !== null){
var val = null;
for(var i = 0; i < this.children.length; i++){
val = val || this.children[i].queryPoint(point);
}
return val;
}
return null;
};
Quadtree.prototype.removePoint = function(point){
//return if tree doesn't contain point
if(!this.box.contains(point)){
return;
}
var i;
if (this.value.length > 0){
for ( i = 0; i < this.value.length; i++ ){
if (this.value[i].point.equals(point)){
this.value.splice(i,1);
return;
}
}
return; // didn't contain point and is leaf node
}
if (this.children !== null){
for( i = 0; i < this.children.length; i++ ){
this.children[i].removePoint(point);
}
}
return;
};
Quadtree.prototype.clear = function(){
this.children = null;
this.value = [];
};
//generalized box class, defined by two points with lessThan (lte) and greaterThan (gte) functions
var Box = function(least, greatest){
this.low = least;
this.high = greatest;
};
//return true if box contains point
Box.prototype.contains = function(point){
if(this.low.lte(point) && this.high.gte(point)){
return true;
}
return false;
};
//return true if overlap of boxes
Box.prototype.overlaps = function(box){
if (this.high.x < box.low.x) return false; // a is left of b
if (this.low.x > box.high.x) return false; // a is right of b
if (this.high.y < box.low.y) return false; // a is above b
if (this.low.y > box.high.y) return false; // a is below b
return true;
};
// return true if the box contains the box provided as argument.
Box.prototype.containsBox = function(box){
return this.contains(box.low) && this.contains(box.high);
};
//return array of children
Box.prototype.split = function(){
var result = [];
result.push(new Box(this.low, new Point((this.low.x+this.high.x)/2, (this.low.y+this.high.y)/2)));
result.push(new Box(new Point((this.low.x+this.high.x)/2, this.low.y),
new Point(this.high.x, (this.low.y+this.high.y)/2)));
result.push(new Box(new Point((this.low.x+this.high.x)/2, (this.low.y+this.high.y)/2), this.high));
result.push(new Box(new Point(this.low.x, (this.low.y+this.high.y)/2),
new Point((this.low.x+this.high.x)/2, this.high.y)));
return result;
};
//two dimensional point
var Point = function(x, y){
this.x = x;
this.y = y;
};
//less than or equal to in both dimensions
Point.prototype.lte = function(point){
if(this.x <= point.x && this.y <= point.y){
return true;
}
return false;
};
//greater than or equal to in both dimensions
Point.prototype.gte = function(point){
if (this.x >= point.x && this.y >= point.y){
return true;
}
return false;
};
//return true if points are equal in both dimensions
Point.prototype.equals = function(point){
return (this.x === point.x && this.y === point.y);
};
//make compatible with use in browser
if (typeof module !== 'undefined') {
module.exports.Quadtree = Quadtree;
module.exports.Box = Box;
module.exports.Point = Point;
}