Skip to content

Commit 2a7f582

Browse files
committed
Add quadtree.find.
1 parent db8d305 commit 2a7f582

3 files changed

Lines changed: 79 additions & 5 deletions

File tree

d3.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5533,6 +5533,9 @@
55335533
root.visit = function(f) {
55345534
d3_geom_quadtreeVisit(f, root, x1_, y1_, x2_, y2_);
55355535
};
5536+
root.find = function(x, y) {
5537+
return d3_geom_quadtreeFind(root, x, y, x1_, y1_, x2_, y2_);
5538+
};
55365539
i = -1;
55375540
if (x1 == null) {
55385541
while (++i < n) {
@@ -5586,6 +5589,28 @@
55865589
if (children[3]) d3_geom_quadtreeVisit(f, children[3], sx, sy, x2, y2);
55875590
}
55885591
}
5592+
function d3_geom_quadtreeFind(root, x, y, x0, y0, x3, y3) {
5593+
var minDistance2 = Infinity, closestPoint;
5594+
(function find(node, x1, y1, x2, y2) {
5595+
var point;
5596+
if (x1 > x3 || y1 > y3 || x2 < x0 || y2 < y0) return;
5597+
if (point = node.point) {
5598+
var dx = x - point[0], dy = y - point[1], distance2 = dx * dx + dy * dy;
5599+
if (distance2 < minDistance2) {
5600+
var distance = Math.sqrt(minDistance2 = distance2);
5601+
x0 = x - distance, y0 = y - distance;
5602+
x3 = x + distance, y3 = y + distance;
5603+
closestPoint = point;
5604+
}
5605+
}
5606+
var children = node.nodes, xm = (x1 + x2) * .5, ym = (y1 + y2) * .5, right = x > xm, below = y > ym;
5607+
if (node = children[below << 1 | right]) find(node, right ? xm : x1, below ? ym : y1, right ? x2 : xm, below ? y2 : ym);
5608+
if (node = children[below << 1 | !right]) find(node, right ? x1 : xm, below ? ym : y1, right ? xm : x2, below ? y2 : ym);
5609+
if (node = children[!below << 1 | right]) find(node, right ? xm : x1, below ? y1 : ym, right ? x2 : xm, below ? ym : y2);
5610+
if (node = children[!below << 1 | !right]) find(node, right ? x1 : xm, below ? y1 : ym, right ? xm : x2, below ? ym : y2);
5611+
})(root, x0, y0, x3, y3);
5612+
return closestPoint;
5613+
}
55895614
d3.interpolateRgb = d3_interpolateRgb;
55905615
function d3_interpolateRgb(a, b) {
55915616
a = d3.rgb(a);

0 commit comments

Comments
 (0)