Skip to content
robinhouston edited this page Apr 23, 2013 · 26 revisions

WikiGeometryQuadtree Geom

A quadtree is a two-dimensional recursive spatial subdivision. This implementation uses square partitions, dividing each square into four equally-sized squares. Each point exists in a unique node; if multiple points are in the same position, some points may be stored on internal nodes rather than leaf nodes. Quadtrees can be used to accelerate various spatial operations, such as the Barnes-Hut approximation for computing n-body forces, or collision detection.

# d3.geom.quadtree([points, [x1, y1, x2, y2 | width, height]])

Deprecated: If points is specified, constructs a new quadtree for the specified array of points. Bounds are optional and can be specified explicitly as x1, y1, x2, y2, or as width, height, which is equivalent to 0, width, 0, height. Points are assumed to be of the form {x: …, y: …}.

# quadtree(points)

Constructs a new quadtree for the specified array of points.

# quadtree.add(point)

Adds a new point to the quadtree.

# quadtree.visit(callback)

The specified callback is invoked with the arguments (node, x1, y1, x2, y2) for each quadtree node pre-order, provided the callback returns false. If callback returns true for a node, then the children of that node are not visited.

# quadtree.x([x])

If x is specified, sets the x-coordinate accessor. If x is not specified, returns the current x-coordinate accessor, which defaults to:

function(d) { return d[0]; }

# quadtree.y([y])

If y is specified, sets the y-coordinate accessor. If y is not specified, returns the current y-coordinate accessor, which defaults to:

function(d) { return d[1]; }

# quadtree.size([size])

If the specified size is null, this causes the quadtree size to be automatically computed for the initial array of points. If the specified size is a two-element array, [width, height], this sets the quadtree's size.

If size is not specified, returns the current size, which defaults to null.

Clone this wiki locally