forked from owenashurst/agar.io-clone
-
Notifications
You must be signed in to change notification settings - Fork 2
/
quadtree.js
239 lines (205 loc) · 8.36 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
/*
* QuadTree Implementation in JavaScript
* @author: silflow <https://github.com/silflow>
*
* Usage:
* To create a new empty Quadtree, do this:
* var tree = QUAD.init(args)
*
* args = {
* // mandatory fields
* x : x coordinate
* y : y coordinate
* w : width
* h : height
*
* // optional fields
* maxChildren : max children per node
* maxDepth : max depth of the tree
*}
*
* API:
* tree.insert() takes arrays or single items
* every item must have a .x and .y property. if not, the tree will break.
*
* tree.retrieve(item, callback) calls the callback for all objects that are in
* the same region or overlapping.
*
* tree.clear() removes all items from the quadtree.
*/
(function(window) {
var QUAD = {}; // global var for the quadtree
QUAD.init = function(args) {
var node;
var TOP_LEFT = 0;
var TOP_RIGHT = 1;
var BOTTOM_LEFT = 2;
var BOTTOM_RIGHT = 3;
var PARENT = 4;
// assign default values
args.maxChildren = args.maxChildren || 2;
args.maxDepth = args.maxDepth || 4;
/**
* Node creator. You should never create a node manually. the algorithm takes
* care of that for you.
*/
node = function (x, y, w, h, depth, maxChildren, maxDepth) {
var items = [], // holds all items
nodes = []; // holds all child nodes
// returns a fresh node object
return {
x : x, // top left point
y : y, // top right point
w : w, // width
h : h, // height
depth : depth, // depth level of the node
/**
* iterates all items that match the selector and invokes the supplied callback on them.
*/
retrieve : function (item, callback) {
for (var i = 0; i < items.length; ++i) {
callback(items[i]);
}
// check if node has subnodes
if (nodes.length) {
// call retrieve on all matching subnodes
this.findOverlappingNodes(item, function(dir) {
nodes[dir].retrieve(item, callback);
});
}
},
/**
* Adds a new Item to the node.
*
* If the node already has subnodes, the item gets pushed down one level.
* If the item does not fit into the subnodes, it gets saved in the
* "children"-array.
*
* If the maxChildren limit is exceeded after inserting the item,
* the node gets divided and all items inside the "children"-array get
* pushed down to the new subnodes.
*/
insert : function (item) {
var i;
if (nodes.length) {
// get the node in which the item fits best
i = this.findInsertNode(item);
if (i === PARENT) {
// if the item does not fit, push it into the
// children array
items.push(item);
} else {
nodes[i].insert(item);
}
} else {
items.push(item);
//divide the node if maxChildren is exceeded and maxDepth is not reached
if (items.length > maxChildren && this.depth < maxDepth) {
this.divide();
}
}
},
/**
* Find a node the item should be inserted in.
*/
findInsertNode : function (item) {
// left
if (item.x + item.w < x + (w / 2)) {
if (item.y + item.h < y + (h / 2)) return TOP_LEFT;
if (item.y >= y + (h / 2)) return BOTTOM_LEFT;
return PARENT;
}
// right
if (item.x >= x + (w / 2)) {
if (item.y + item.h < y + (h / 2)) return TOP_RIGHT;
if (item.y >= y + (h / 2)) return BOTTOM_RIGHT;
return PARENT;
}
return PARENT;
},
/**
* Finds the regions the item overlaps with. See constants defined
* above. The callback is called for every region the item overlaps.
*/
findOverlappingNodes : function (item, callback) {
// left
if (item.x < x + (w / 2)) {
if (item.y < y + (h / 2)) callback(TOP_LEFT);
if (item.y + item.h >= y + h/2) callback(BOTTOM_LEFT);
}
// right
if (item.x + item.w >= x + (w / 2)) {
if (item.y < y + (h / 2)) callback(TOP_RIGHT);
if (item.y + item.h >= y + h/2) callback(BOTTOM_RIGHT);
}
},
/**
* Divides the current node into four subnodes and adds them
* to the nodes array of the current node. Then reinserts all
* children.
*/
divide : function () {
var width, height, i, oldChildren;
var childrenDepth = this.depth + 1;
// set dimensions of the new nodes
width = (w / 2);
height = (h / 2);
// create top left node
nodes.push(node(this.x, this.y, width, height, childrenDepth, maxChildren, maxDepth));
// create top right node
nodes.push(node(this.x + width, this.y, width, height, childrenDepth, maxChildren, maxDepth));
// create bottom left node
nodes.push(node(this.x, this.y + height, width, height, childrenDepth, maxChildren, maxDepth));
// create bottom right node
nodes.push(node(this.x + width, this.y + height, width, height, childrenDepth, maxChildren, maxDepth));
oldChildren = items;
items = [];
for (i = 0; i < oldChildren.length; i++) {
this.insert(oldChildren[i]);
}
},
/**
* Clears the node and all its subnodes.
*/
clear : function () {
for (var i = 0; i < nodes.length; i++) nodes[i].clear();
items.length = 0;
nodes.length = 0;
},
/*
* convenience method: is not used in the core algorithm.
* ---------------------------------------------------------
* returns this nodes subnodes. this is usful if we want to do stuff
* with the nodes, i.e. accessing the bounds of the nodes to draw them
* on a canvas for debugging etc...
*/
getNodes : function () {
return nodes.length ? nodes : false;
}
};
};
return {
root : (function () {
return node(args.x, args.y, args.w, args.h, 0, args.maxChildren, args.maxDepth);
}()),
insert : function (item) {
var len, i;
if (item instanceof Array) {
len = item.length;
for (i = 0; i < len; i++) {
this.root.insert(item[i]);
}
} else {
this.root.insert(item);
}
},
retrieve : function (selector, callback) {
return this.root.retrieve(selector, callback);
},
clear : function () {
this.root.clear();
}
};
};
window.QUAD = QUAD;
}(this));