-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathquadtree.js
251 lines (201 loc) · 7.83 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
/**
* quadtree-js
* @version 1.2.6
* @license MIT
* @author Timo Hausmann
*/
/* https://github.com/timohausmann/quadtree-js.git v1.2.6 */
/*
Copyright © 2012-2023 Timo Hausmann
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
;(function() {
/**
* The Quadtree uses rectangle objects for all areas ("Rect").
* All rectangles require the properties x, y, width, height
* @typedef {Object} Rect
* @property {number} x X-Position
* @property {number} y Y-Position
* @property {number} width Width
* @property {number} height Height
*/
/**
* Quadtree Constructor
* @class Quadtree
* @param {Rect} bounds bounds of the node ({ x, y, width, height })
* @param {number} [max_objects=10] (optional) max objects a node can hold before splitting into 4 subnodes (default: 10)
* @param {number} [max_levels=4] (optional) total max levels inside root Quadtree (default: 4)
* @param {number} [level=0] (optional) depth level, required for subnodes (default: 0)
*/
function Quadtree(bounds, max_objects, max_levels, level) {
this.max_objects = max_objects || 10;
this.max_levels = max_levels || 4;
this.level = level || 0;
this.bounds = bounds;
this.objects = [];
this.nodes = [];
};
/**
* Split the node into 4 subnodes
* @memberof Quadtree
*/
Quadtree.prototype.split = function() {
var nextLevel = this.level + 1,
subWidth = this.bounds.width/2,
subHeight = this.bounds.height/2,
x = this.bounds.x,
y = this.bounds.y;
//top right node
this.nodes[0] = new Quadtree({
x : x + subWidth,
y : y,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);
//top left node
this.nodes[1] = new Quadtree({
x : x,
y : y,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);
//bottom left node
this.nodes[2] = new Quadtree({
x : x,
y : y + subHeight,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);
//bottom right node
this.nodes[3] = new Quadtree({
x : x + subWidth,
y : y + subHeight,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);
};
/**
* Determine which node the object belongs to
* @param {Rect} pRect bounds of the area to be checked ({ x, y, width, height })
* @return {number[]} an array of indexes of the intersecting subnodes (0-3 = top-right, top-left, bottom-left, bottom-right / ne, nw, sw, se)
* @memberof Quadtree
*/
Quadtree.prototype.getIndex = function(pRect) {
var indexes = [],
verticalMidpoint = this.bounds.x + (this.bounds.width/2),
horizontalMidpoint = this.bounds.y + (this.bounds.height/2);
var startIsNorth = pRect.y < horizontalMidpoint,
startIsWest = pRect.x < verticalMidpoint,
endIsEast = pRect.x + pRect.width > verticalMidpoint,
endIsSouth = pRect.y + pRect.height > horizontalMidpoint;
//top-right quad
if(startIsNorth && endIsEast) {
indexes.push(0);
}
//top-left quad
if(startIsWest && startIsNorth) {
indexes.push(1);
}
//bottom-left quad
if(startIsWest && endIsSouth) {
indexes.push(2);
}
//bottom-right quad
if(endIsEast && endIsSouth) {
indexes.push(3);
}
return indexes;
};
/**
* Insert the object into the node. If the node
* exceeds the capacity, it will split and add all
* objects to their corresponding subnodes.
* @param {Rect} pRect bounds of the object to be added ({ x, y, width, height })
* @memberof Quadtree
*/
Quadtree.prototype.insert = function(pRect) {
var i = 0,
indexes;
//if we have subnodes, call insert on matching subnodes
if(this.nodes.length) {
indexes = this.getIndex(pRect);
for(i=0; i<indexes.length; i++) {
this.nodes[indexes[i]].insert(pRect);
}
return;
}
//otherwise, store object here
this.objects.push(pRect);
//max_objects reached
if(this.objects.length > this.max_objects && this.level < this.max_levels) {
//split if we don't already have subnodes
if(!this.nodes.length) {
this.split();
}
//add all objects to their corresponding subnode
for(i=0; i<this.objects.length; i++) {
indexes = this.getIndex(this.objects[i]);
for(var k=0; k<indexes.length; k++) {
this.nodes[indexes[k]].insert(this.objects[i]);
}
}
//clean up this node
this.objects = [];
}
};
/**
* Return all objects that could collide with the given object
* @param {Rect} pRect bounds of the object to be checked ({ x, y, width, height })
* @return {Rect[]} array with all detected objects
* @memberof Quadtree
*/
Quadtree.prototype.retrieve = function(pRect) {
var indexes = this.getIndex(pRect),
returnObjects = this.objects;
//if we have subnodes, retrieve their objects
if(this.nodes.length) {
for(var i=0; i<indexes.length; i++) {
returnObjects = returnObjects.concat(this.nodes[indexes[i]].retrieve(pRect));
}
}
//remove duplicates
if(this.level === 0) {
return Array.from(new Set(returnObjects));
}
return returnObjects;
};
/**
* Clear the quadtree
* @memberof Quadtree
*/
Quadtree.prototype.clear = function() {
this.objects = [];
for(var i=0; i < this.nodes.length; i++) {
if(this.nodes.length) {
this.nodes[i].clear();
}
}
this.nodes = [];
};
//export for commonJS or browser
if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = Quadtree;
} else {
window.Quadtree = Quadtree;
}
})();