You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.6 KiB
58 lines
1.6 KiB
export default function(x, y) { |
|
if (isNaN(x = +x) || isNaN(y = +y)) return this; // ignore invalid points |
|
|
|
var x0 = this._x0, |
|
y0 = this._y0, |
|
x1 = this._x1, |
|
y1 = this._y1; |
|
|
|
// If the quadtree has no extent, initialize them. |
|
// Integer extent are necessary so that if we later double the extent, |
|
// the existing quadrant boundaries don’t change due to floating point error! |
|
if (isNaN(x0)) { |
|
x1 = (x0 = Math.floor(x)) + 1; |
|
y1 = (y0 = Math.floor(y)) + 1; |
|
} |
|
|
|
// Otherwise, double repeatedly to cover. |
|
else if (x0 > x || x > x1 || y0 > y || y > y1) { |
|
var z = x1 - x0, |
|
node = this._root, |
|
parent, |
|
i; |
|
|
|
switch (i = (y < (y0 + y1) / 2) << 1 | (x < (x0 + x1) / 2)) { |
|
case 0: { |
|
do parent = new Array(4), parent[i] = node, node = parent; |
|
while (z *= 2, x1 = x0 + z, y1 = y0 + z, x > x1 || y > y1); |
|
break; |
|
} |
|
case 1: { |
|
do parent = new Array(4), parent[i] = node, node = parent; |
|
while (z *= 2, x0 = x1 - z, y1 = y0 + z, x0 > x || y > y1); |
|
break; |
|
} |
|
case 2: { |
|
do parent = new Array(4), parent[i] = node, node = parent; |
|
while (z *= 2, x1 = x0 + z, y0 = y1 - z, x > x1 || y0 > y); |
|
break; |
|
} |
|
case 3: { |
|
do parent = new Array(4), parent[i] = node, node = parent; |
|
while (z *= 2, x0 = x1 - z, y0 = y1 - z, x0 > x || y0 > y); |
|
break; |
|
} |
|
} |
|
|
|
if (this._root && this._root.length) this._root = node; |
|
} |
|
|
|
// If the quadtree covers the point already, just return. |
|
else return this; |
|
|
|
this._x0 = x0; |
|
this._y0 = y0; |
|
this._x1 = x1; |
|
this._y1 = y1; |
|
return this; |
|
}
|
|
|