Quadtree Constructor
bounds and properties of the node
depth level (internal use only, required for subnodes)
ReadonlyboundsThe numeric boundaries of this node.
ReadonlylevelThe level of this node.
ReadonlymaxTotal max nesting levels of the root Quadtree node.
ReadonlymaxMax objects this node can hold before it splits.
ReadonlyminMinimum nesting levels of the root Quadtree node.
ReadonlynodesSubnodes of this node
ReadonlyobjectsArray of objects in this node.
Get the quadrant (subnode indexes) an object belongs to.
Array containing indexes of intersecting subnodes (0-3 = top-right, top-left, bottom-left, bottom-right).
Insert an object into the node. If the node exceeds the capacity, it will split and add all objects to their corresponding subnodes.
Object to be added.
const tree = new Quadtree({ width: 100, height: 100 });
tree.insert(new Rectangle({ x: 25, y: 25, width: 10, height: 10, data: 'data' }));
tree.insert(new Circle({ x: 25, y: 25, r: 10, data: 512 }));
tree.insert(new Line({ x1: 25, y1: 25, x2: 60, y2: 40, data: { custom: 'property'} }));
The opposite of a split: merge and dissolve subnodes (when total object count doesn't exceed maxObjects).
The objects from this node and all subnodes combined.
BetaRemove an object from the tree.
If you have to remove many objects, consider clearing the entire tree and rebuilding it or use the fast flag to cleanup after the last removal.
Object to be removed.
Set to true to increase performance temporarily by preventing cleanup of empty subnodes (optional, default: false).
Weather or not the object was removed from THIS node (no recursive check).
const tree = new Quadtree({ width: 100, height: 100 });
const circle = new Circle({ x: 25, y: 25, r: 10, data: 512 });
tree.insert(circle);
tree.remove(circle);
const tree = new Quadtree({ width: 100, height: 100 });
const rects = [];
for(let i=0; i<20; i++) {
rects[i] = new Rectangle({ x: 25, y: 25, width: 50, height: 50 });
tree.insert(rects[i]);
}
for(let i=rects.length-1; i>0; i--) {
//fast=true – just remove the object (may leaves vacant subnodes)
//fast=false – cleanup empty subnodes (default)
const fast = (i !== 0);
tree.remove(rects[i], fast);
}
Return all objects that could collide with the given geometry.
Array containing all detected objects.
tree.retrieve(new Rectangle({ x: 25, y: 25, width: 10, height: 10, data: 'data' }));
tree.retrieve(new Circle({ x: 25, y: 25, r: 10, data: 512 }));
tree.retrieve(new Line({ x1: 25, y1: 25, x2: 60, y2: 40, data: { custom: 'property'} }));
BetaUpdate an object already in the tree (shorthand for remove and insert).
If you have to update many objects, consider clearing and rebuilding the
entire tree or use the fast flag to cleanup after the last update.
Object to be updated.
Set to true to increase performance temporarily by preventing cleanup of empty subnodes (optional, default: false).
const tree = new Quadtree({ width: 100, height: 100, maxObjects: 1 });
const rect1 = new Rectangle({ x: 25, y: 25, width: 10, height: 10 });
const rect2 = new Rectangle({ x: 25, y: 25, width: 10, height: 10 });
tree.insert(rect1);
tree.insert(rect2);
rect1.x = 75;
rect1.y = 75;
tree.update(rect1);
const tree = new Quadtree({ width: 100, height: 100 });
const rects = [];
for(let i=0; i<20; i++) {
rects[i] = new Rectangle({ x: 20, y: 20, width: 20, height: 20 });
tree.insert(rects[i]);
}
for(let i=rects.length-1; i>0; i--) {
rects[i].x = 20 + Math.random()*60;
rects[i].y = 20 + Math.random()*60;
//fast=true – just re-insert the object (may leaves vacant subnodes)
//fast=false – cleanup empty subnodes (default)
const fast = (i !== 0);
tree.update(rects[i], fast);
}
Class representing a Quadtree node.
Example
Example: Typescript: If you like to be explicit, you optionally can pass in a generic type for objects to be stored in the Quadtree: