Skip to content

Commit ccfeadc

Browse files
committed
Store input data in quadtree.
1 parent 510e0de commit ccfeadc

1 file changed

Lines changed: 22 additions & 20 deletions

File tree

src/geom/quadtree.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ d3.geom.quadtree = function(points, x1, y1, x2, y2) {
3333

3434
if (compat) points = data;
3535
else for (points = [], i = 0; i < n; ++i) {
36-
points.push({x: +fx.call(this, d = data[i], i), y: +fy.call(this, d, i)});
36+
points.push({x: +fx.call(this, d = data[i]), y: +fy.call(this, d)});
3737
}
3838

3939
if (x1 != null) {
@@ -59,38 +59,40 @@ d3.geom.quadtree = function(points, x1, y1, x2, y2) {
5959

6060
// Recursively inserts the specified point p at the node n or one of its
6161
// descendants. The bounds are defined by [x1, x2] and [y1, y2].
62-
function insert(n, p, x1, y1, x2, y2) {
63-
if (isNaN(p.x) || isNaN(p.y)) return; // ignore invalid points
62+
function insert(n, d, x, y, x1, y1, x2, y2) {
63+
if (isNaN(x) || isNaN(y)) return; // ignore invalid points
6464
if (n.leaf) {
65-
var v = n.point;
66-
if (v) {
65+
var nx = n.x,
66+
ny = n.y;
67+
if (nx != null) {
6768
// If the point at this leaf node is at the same position as the new
6869
// point we are adding, we leave the point associated with the
6970
// internal node while adding the new point to a child node. This
7071
// avoids infinite recursion.
71-
if ((Math.abs(v.x - p.x) + Math.abs(v.y - p.y)) < .01) {
72-
insertChild(n, p, x1, y1, x2, y2);
72+
if ((Math.abs(nx - x) + Math.abs(ny - y)) < .01) {
73+
insertChild(n, d, x, y, x1, y1, x2, y2);
7374
} else {
74-
n.point = null;
75-
insertChild(n, v, x1, y1, x2, y2);
76-
insertChild(n, p, x1, y1, x2, y2);
75+
var nPoint = n.point;
76+
n.x = n.y = n.point = null;
77+
insertChild(n, nPoint, nx, ny, x1, y1, x2, y2);
78+
insertChild(n, d, x, y, x1, y1, x2, y2);
7779
}
7880
} else {
79-
n.point = p;
81+
n.x = x, n.y = y, n.point = d;
8082
}
8183
} else {
82-
insertChild(n, p, x1, y1, x2, y2);
84+
insertChild(n, d, x, y, x1, y1, x2, y2);
8385
}
8486
}
8587

86-
// Recursively inserts the specified point p into a descendant of node n. The
87-
// bounds are defined by [x1, x2] and [y1, y2].
88-
function insertChild(n, p, x1, y1, x2, y2) {
88+
// Recursively inserts the specified point [x, y] into a descendant of node
89+
// n. The bounds are defined by [x1, x2] and [y1, y2].
90+
function insertChild(n, d, x, y, x1, y1, x2, y2) {
8991
// Compute the split point, and the quadrant in which to insert p.
9092
var sx = (x1 + x2) * .5,
9193
sy = (y1 + y2) * .5,
92-
right = p.x >= sx,
93-
bottom = p.y >= sy,
94+
right = x >= sx,
95+
bottom = y >= sy,
9496
i = (bottom << 1) + right;
9597

9698
// Recursively insert into the child node.
@@ -100,14 +102,14 @@ d3.geom.quadtree = function(points, x1, y1, x2, y2) {
100102
// Update the bounds as we recurse.
101103
if (right) x1 = sx; else x2 = sx;
102104
if (bottom) y1 = sy; else y2 = sy;
103-
insert(n, p, x1, y1, x2, y2);
105+
insert(n, d, x, y, x1, y1, x2, y2);
104106
}
105107

106108
// Create the root node.
107109
var root = d3_geom_quadtreeNode();
108110

109-
root.add = function(p) {
110-
insert(root, p, x1_, y1_, x2_, y2_);
111+
root.add = function(d) {
112+
insert(root, d, fx(d), fy(d), x1_, y1_, x2_, y2_);
111113
};
112114

113115
root.visit = function(f) {

0 commit comments

Comments
 (0)