Package Exports
- d3-quadtree
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (d3-quadtree) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
d3-quadtree
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.
Installing
If you use NPM, npm install d3-quadtree. Otherwise, download the latest release. You can also load directly from d3js.org, either as a standalone library or as part of D3 4.0 alpha. AMD, CommonJS, and vanilla environments are supported. In vanilla, a d3_quadtree global is exported:
<script src="https://d3js.org/d3-quadtree.v0.3.min.js"></script>
<script>
var quadtree = d3_quadtree.quadtree();
</script>Try d3-quadtree in your browser.
API Reference
# d3.quadtree()
Creates a new quadtree generator with the default x-accessor, y-accessor and extent. The returned generator can be used to create any number of quadtree roots from data.
# quadtree(data)
Constructs a new quadtree root node for the specified array of data. The x- and y-coordinates of each data point are determined using the current x- and y-accessors; each point in the quadtree is represented as a two-element array of numbers [x, y] with the following additional properties:
data- the datum associated with this node.index- the index of the datum associated with this node.
# quadtree.x([x])
If x is specified, sets the x-coordinate accessor and returns this quadtree generator. If x is not specified, returns the current x-coordinate accessor, which defaults to:
function x(d) {
return d[0];
}For each point added to the quadtree, the x-accessor is invoked, being passed the current datum d and index i. The x-accessor must then return a numeric value indicating the x-coordinate of the point corresponding to the given datum. The x-accessor may also be defined as a constant number rather than a function, if desired.
# quadtree.y([y])
If y is specified, sets the y-coordinate accessor and returns this quadtree generator. If y is not specified, returns the current y-coordinate accessor, which defaults to:
function y(d) {
return d[1];
}For each point added to the quadtree, the y-accessor is invoked, being passed the current datum d and index i. The y-accessor must then return a numeric value indicating the y-coordinate of the point corresponding to the given datum. The y-accessor may also be defined as a constant number rather than a function, if desired.
# quadtree.extent([extent])
If extent is specified, sets the current extent and returns this quadtree generator. The extent must be specified as a two-dimensional array [[x0, y0], [x1, y1]], where x0 and y0 are the inclusive lower bounds of the extent and x1 and y1 are the exclusive upper bounds, or null. If extent is not specified, returns the current extent, which defaults to null. When the extent is not null, any point outside the extent is ignored when constructing the quadtree.
# quadtree.size([size])
If size is specified, sets the current extent and returns this quadtree generator. The size must be specified as a two-element array of numbers [[x1, y1]], where x1 and y1 are the exclusive upper bounds, or null; the lower bounds of the extent are implicitly ⟨0,0⟩. If size is not specified, returns the current upper bounds of the extent, which defaults to null. When the extent is not null, any point outside the extent is ignored when constructing the quadtree.
This is a convenience method for setting the extent where the lower bounds of the extent are ⟨0,0⟩. For example, this:
quadtree.size([960, 500]);Is equivalent to this:
quadtree.extent([[0, 0], [960, 500]]);Quadtree Nodes
Internal nodes of the quadtree are represented as sparse four-element arrays in left-to-right, top-to-bottom order:
0- the top-left quadrant1- the top-right quadrant2- the bottom-left quadrant3- the bottom-right quadrant
Leaf nodes of the quadtree are represent as objects with the following property:
point- a two-element array of numbers [x, y]
If there are multiple coincident points in the quadtree, then point.next forms a linked list of coincident points.
# root.add(point)
Adds the specified new point to this quadtree and returns root. The point must be represented as a two-element array of numbers [x, y].
# root.find(x, y)
Given a point ⟨x,y⟩, returns the closest point in this quadtree.
# root.eachBefore(callback)
Visits each node in this quadtree in pre-order traversal, invoking the specified callback with arguments node, x0, y0, x1, y1 for each node, where node is the node being visited, ⟨x0, y0⟩ are the lower bounds of the node, and ⟨x1, y1⟩ are the upper bounds. (Assuming that positive x is right and positive y is down, as is typically the case in Canvas and SVG, ⟨x0, y0⟩ is the top-left corner and ⟨x1, y1⟩ is the lower-right corner; however, the coordinate system is arbitrary, so more formally x0 <= x1 and y0 <= y1.) Returns root. If the callback returns true for a given node, then the children of that node are not visited; otherwise, all child nodes are visited.
# root.eachAfter(callback)
Visits each node in this quadtree in post-order traversal, invoking the specified callback with arguments node, x0, y0, x1, y1 for each node, where node is the node being visited, ⟨x0, y0⟩ are the lower bounds of the node, and ⟨x1, y1⟩ are the upper bounds. (Assuming that positive x is right and positive y is down, as is typically the case in Canvas and SVG, ⟨x0, y0⟩ is the top-left corner and ⟨x1, y1⟩ is the lower-right corner; however, the coordinate system is arbitrary, so more formally x0 <= x1 and y0 <= y1.) Returns root.