|
5533 | 5533 | root.visit = function(f) { |
5534 | 5534 | d3_geom_quadtreeVisit(f, root, x1_, y1_, x2_, y2_); |
5535 | 5535 | }; |
| 5536 | + root.find = function(x, y) { |
| 5537 | + return d3_geom_quadtreeFind(root, x, y, x1_, y1_, x2_, y2_); |
| 5538 | + }; |
5536 | 5539 | i = -1; |
5537 | 5540 | if (x1 == null) { |
5538 | 5541 | while (++i < n) { |
|
5586 | 5589 | if (children[3]) d3_geom_quadtreeVisit(f, children[3], sx, sy, x2, y2); |
5587 | 5590 | } |
5588 | 5591 | } |
| 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 | + } |
5589 | 5614 | d3.interpolateRgb = d3_interpolateRgb; |
5590 | 5615 | function d3_interpolateRgb(a, b) { |
5591 | 5616 | a = d3.rgb(a); |
|
0 commit comments