JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 16473927
  • Score
    100M100P100Q252491F
  • License BSD-3-Clause

Two-dimensional recursive spatial subdivision.

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. The released bundle supports AMD, CommonJS, and vanilla environments. Create a custom build using Rollup or your preferred bundler. You can also load directly from d3js.org:

<script src="https://d3js.org/d3-quadtree.v0.2.min.js"></script>

In a vanilla environment, a d3_quadtree global is exported. Try d3-quadtree in your browser.

API Reference

# d3.quadtree()

Creates a new quadtree factory with the default x-accessor, y-accessor and extent. The returned factory function can be used to create any number of quadtrees from data.

# quadtree([data])

Constructs a new quadtree for the specified array of data, returning the root node of a new quadtree. The x- and y-coordinates of each data point are determined using the current x- and y-accessor functions. To build a quadtree by adding points incrementally, the specified data array can be empty or omitted and then points can be later added to the returned root node; in this case, you must explicitly specify the extent of the quadtree.

Each node in the quadtree has several properties:

  • nodes - a sparse array of four child nodes: top-left, top-right, bottom-left, bottom-right.
  • leaf - a boolean indicating whether this is an internal node or a leaf node.
  • data - the datum associated with this node, if any; may apply to either internal or leaf nodes.
  • x - the x-coordinate of the associated point, if any.
  • y - the y-coordinate of the associated point, if any.

The returned root node also defines add and visit methods.

# root.add(datum)

Adds the specified new datum to this quadtree and returns root.

# root.find(x, y)

Given a point ⟨x,y⟩, returns the closest datum in this quadtree.

# root.visit(callback)

Visits each node in this quadtree, invoking the specified callback with arguments {node, x1, y1, x2, y2} for each node, where node is the node being visited, ⟨x1, y1⟩ is the top-left corner, and ⟨x2, y2⟩ is the bottom-right corner. Returns root. Nodes are traversed in pre-order. If the callback returns true for a given node, then the children of that node are not visited; otherwise, all child nodes are visited.

Note that the coordinate system used by the quadtree is arbitrary, so a more precise definition is that x1 <= x2 and y1 <= y2. In the typical coordinate system used by SVG and Canvas, the origin ⟨0,0⟩ is in the top-left corner, and thus ⟨x1, y1⟩ is also the top-left corner of the current node.

# quadtree.x([x])

If x is specified, sets the x-coordinate accessor and returns this quadtree factory. 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, either during initial construction or lazily added, 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 factory. 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, either during initial construction or lazily added, 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 factory. If extent is not specified, returns the current extent, which defaults to null. When the extent is null, an extent will be computed automatically by scanning the array of input data points passed to the quadtree constructor. Otherwise, the extent must be specified as a two-dimensional array [​[x0, y0], [​x1, y1]​], where x0 and y0 are the lower bounds of the extent, and x1 and y1 are the upper bounds of the extent. Setting an extent is required when constructing a quadtree lazily from an initially-empty set of nodes.

# quadtree.size([size])

An alias for quadtree.extent where the minimum x and y of the extent are ⟨0,0⟩. Given a quadtree factory q, this is equivalent to q.extent([[0, 0], size]).