Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
d3-quadtree
Advanced tools
The d3-quadtree package is a part of the D3.js library and provides a data structure known as a quadtree. A quadtree is a tree data structure in which each internal node has exactly four children. Quadtrees are often used to partition a two-dimensional space by recursively subdividing it into four quadrants or regions. This package is particularly useful for spatial indexing, collision detection, and efficient neighbor searching.
Creating a Quadtree
This feature allows you to create a quadtree from a set of points. The quadtree can then be used for efficient spatial queries.
const d3 = require('d3-quadtree');
const points = [[0, 0], [1, 1], [2, 2], [3, 3]];
const quadtree = d3.quadtree().addAll(points);
Adding Points
This feature allows you to add individual points to an existing quadtree. This is useful for dynamically updating the quadtree as new data points are introduced.
const d3 = require('d3-quadtree');
const quadtree = d3.quadtree();
quadtree.add([0, 0]);
quadtree.add([1, 1]);
Finding Nearest Neighbor
This feature allows you to find the nearest neighbor to a given point. This is useful for applications like collision detection or finding the closest data point.
const d3 = require('d3-quadtree');
const points = [[0, 0], [1, 1], [2, 2], [3, 3]];
const quadtree = d3.quadtree().addAll(points);
const nearest = quadtree.find(1.5, 1.5);
Bounding Box Search
This feature allows you to search for points within a specific bounding box. This is useful for spatial queries where you need to find all points within a certain area.
const d3 = require('d3-quadtree');
const points = [[0, 0], [1, 1], [2, 2], [3, 3]];
const quadtree = d3.quadtree().addAll(points);
const found = [];
quadtree.visit((node, x0, y0, x1, y1) => {
if (!node.length) {
do {
const d = node.data;
if (d[0] >= 1 && d[0] <= 2 && d[1] >= 1 && d[1] <= 2) {
found.push(d);
}
} while (node = node.next);
}
return x0 >= 2 || y0 >= 2 || x1 < 1 || y1 < 1;
});
rbush is a high-performance JavaScript library for 2D spatial indexing of points and rectangles. It is similar to d3-quadtree in that it provides efficient spatial queries, but it uses an R-tree data structure instead of a quadtree. R-trees are generally better for dynamic datasets where insertions and deletions are frequent.
kdbush is a very fast static spatial index for 2D points based on a flat KD-tree. It is similar to d3-quadtree in that it provides efficient nearest neighbor searches, but it is optimized for static datasets where the points do not change after the initial build.
geokdbush is a geospatial extension of kdbush, providing fast nearest neighbor searches for geographic coordinates. It is similar to d3-quadtree in that it provides efficient spatial queries, but it is specifically optimized for geographic data and uses a flat KD-tree structure.
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.
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 Skypack:
<script type="module">
import {quadtree} from "https://cdn.skypack.dev/d3-quadtree@3";
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/d3-quadtree@3"></script>
<script>
const tree = d3.quadtree();
</script>
# d3.quadtree([data[, x, y]]) <>
Creates a new, empty quadtree with an empty extent and the default x- and y-accessors. 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);
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.
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.
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.)
Adds the specified datum to the quadtree, deriving its coordinates ⟨x,y⟩ using the current x- and y-accessors, 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.
Adds the specified array of data to the quadtree, deriving each element’s coordinates ⟨x,y⟩ using the current x- and y-accessors, 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.
Removes the specified datum from the quadtree, deriving its coordinates ⟨x,y⟩ using the current x- and y-accessors, and returns the quadtree. If the specified datum does not exist in this quadtree, this method does nothing.
Removes the specified data from the quadtree, deriving their coordinates ⟨x,y⟩ using the current x- and y-accessors, 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.
Returns the root node of the quadtree.
Returns an array of all data in the quadtree.
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.
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.
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.
FAQs
Two-dimensional recursive spatial subdivision.
The npm package d3-quadtree receives a total of 2,896,676 weekly downloads. As such, d3-quadtree popularity was classified as popular.
We found that d3-quadtree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.