Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

d3-quadtree

a quadtree recursively partitions two-dimensional space into squares, dividing each square into four equally-sized squares. each distinct point exists in a unique leaf node; coincident points are represented by a linked list. quadtrees can accelerate various spatial operations, such as the barnes–hut approximation for computing many-body forces, collision detection, and searching for nearby points.

687474703a2f2f626c2e6f636b732e6f72672f6d626f73746f636b2f7261772f393037383639302f7468756d626e61696c2e706e67

687474703a2f2f626c2e6f636b732e6f72672f6d626f73746f636b2f7261772f343334333231342f7468756d626e61696c2e706e67

installing

if you use npm, npm install d3-quadtree. you can also download the latest release on github. for vanilla html in modern browsers, import d3-quadtree from skypak:

<script type="module">

import {quadtree} from "https://cdn.skypack.dev/[email protected]";

const tree = quadtree();

</script>

for legacy environments, you can load d3-quadtree’s umd bundle from an npm-based cdn such as jsdelivr; a d3 global is exported:

<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script>

const tree = d3.quadtree();

</script>

api reference

# d3.quadtree([data[, x, y]]) <>

creates a new, empty quadtree with an empty extent and the default x and yaccessors. if data is specified, adds the specified array of data to the quadtree. this is equivalent to:

const tree = d3.quadtree()
    .addall(data);

if x and y are also specified, sets the x and y accessors to the specified functions before adding the specified array of data to the quadtree, equivalent to:

const tree = d3.quadtree()
    .x(x)
    .y(y)
    .addall(data);

# quadtree.x([x]) <>

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

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

the x-acccessor is used to derive the x-coordinate of data when adding to and removing from the tree. it is also used when finding to re-access the coordinates of data previously added to the tree; therefore, the x– and y-accessors must be consistent, returning the same value given the same input.

# quadtree.y([y]) <>

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

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

the y-acccessor is used to derive the y-coordinate of data when adding to and removing from the tree. it is also used when finding to re-access the coordinates of data previously added to the tree; therefore, the x– and y-accessors must be consistent, returning the same value given the same input.

# quadtree.extent([extent]) <>

if extent is specified, expands the quadtree to cover the specified points [[x0, y0], [x1, y1]] and returns the quadtree. if extent is not specified, returns the quadtree’s current extent [[x0, y0], [x1, y1]], where x0 and y0 are the inclusive lower bounds and x1 and y1 are the inclusive upper bounds, or undefined if the quadtree has no extent. the extent may also be expanded by calling quadtree.cover or quadtree.add.

# quadtree.cover(x, y) <>

expands the quadtree to cover the specified point ⟨x,y⟩, and returns the quadtree. if the quadtree’s extent already covers the specified point, this method does nothing. if the quadtree has an extent, the extent is repeatedly doubled to cover the specified point, wrapping the root node as necessary; if the quadtree is empty, the extent is initialized to the extent [[⌊x⌋, ⌊y⌋], [⌈x⌉, ⌈y⌉]]. (rounding is necessary such that if the extent is later doubled, the boundaries of existing quadrants do not change due to floating point error.)

# quadtree.add(datum) <>

adds the specified datum to the quadtree, deriving its coordinates ⟨x,y⟩ using the current x and yaccessors, and returns the quadtree. if the new point is outside the current extent of the quadtree, the quadtree is automatically expanded to cover the new point.

# quadtree.addall(data) <>

adds the specified array of data to the quadtree, deriving each element’s coordinates ⟨x,y⟩ using the current x and yaccessors, and return this quadtree. this is approximately equivalent to calling quadtree.add repeatedly:

for (let i = 0, n = data.length; i < n; ++i) {
  quadtree.add(data[i]);
}

however, this method results in a more compact quadtree because the extent of the data is computed first before adding the data.

# quadtree.remove(datum) <>

removes the specified datum from the quadtree, deriving its coordinates ⟨x,y⟩ using the current x and yaccessors, and returns the quadtree. if the specified datum does not exist in this quadtree, this method does nothing.

# quadtree.removeall(data) <>

removes the specified data from the quadtree, deriving their coordinates ⟨x,y⟩ using the current x and yaccessors, and returns the quadtree. if a specified datum does not exist in this quadtree, it is ignored.

# quadtree.copy()

returns a copy of the quadtree. all nodes in the returned quadtree are identical copies of the corresponding node in the quadtree; however, any data in the quadtree is shared by reference and not copied.

# quadtree.root() <>

returns the root node of the quadtree.

# quadtree.data() <>

returns an array of all data in the quadtree.

# quadtree.size() <>

returns the total number of data in the quadtree.

# quadtree.find(x, y[, radius]) <>

returns the datum closest to the position ⟨x,y⟩ with the given search radius. if radius is not specified, it defaults to infinity. if there is no datum within the search area, returns undefined.

# quadtree.visit(callback) <>

visits each node in the 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, and returns the quadtree. (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.)

if the callback returns true for a given node, then the children of that node are not visited; otherwise, all child nodes are visited. this can be used to quickly visit only parts of the tree, for example when using the barnes–hut approximation. note, however, that child quadrants are always visited in sibling order: top-left, top-right, bottom-left, bottom-right. in cases such as search, visiting siblings in a specific order may be faster.

as an example, the following visits the quadtree and returns all the nodes within a rectangular extent [xmin, ymin, xmax, ymax], ignoring quads that cannot possibly contain any such node:

function search(quadtree, xmin, ymin, xmax, ymax) {
  const results = [];
  quadtree.visit((node, x1, y1, x2, y2) => {
    if (!node.length) {
      do {
        let d = node.data;
        if (d[0] >= xmin && d[0] < xmax && d[1] >= ymin && d[1] < ymax) {
          results.push(d);
        }
      } while (node = node.next);
    }
    return x1 >= xmax || y1 >= ymax || x2 < xmin || y2 < ymin;
  });
  return results;
}

# quadtree.visitafter(callback) <>

visits each node in the 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, and returns the quadtree. (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.

nodes

internal nodes of the quadtree are represented as four-element arrays in left-to-right, top-to-bottom order:

  • 0 – the top-left quadrant, if any.
  • 1 – the top-right quadrant, if any.
  • 2 – the bottom-left quadrant, if any.
  • 3 – the bottom-right quadrant, if any.

a child quadrant may be undefined if it is empty.

leaf nodes are represented as objects with the following properties:

  • data – the data associated with this point, as passed to quadtree.add.
  • next – the next datum in this leaf, if any.

the length property may be used to distinguish leaf nodes from internal nodes: it is undefined for leaf nodes, and 4 for internal nodes. for example, to iterate over all data in a leaf node:

if (!node.length) do console.log(node.data); while (node = node.next);

the point’s x– and y-coordinates must not be modified while the point is in the quadtree. to update a point’s position, remove the point and then re-add it to the quadtree at the new position. alternatively, you may discard the existing quadtree entirely and create a new one from scratch; this may be more efficient if many of the points have moved.


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.