Skip to content

Commit

Permalink
Tiny optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Apr 23, 2016
1 parent fa42ac0 commit 4b1a8c5
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/cover.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
export default function(x, y) {
if (isNaN(x = +x) || isNaN(y = +y)) return this; // ignore invalid points

var node = this._root,
parent,
i,
x0 = this._x0,
var x0 = this._x0,
y0 = this._y0,
x1 = this._x1,
y1 = this._y1,
z = x1 - x0;
y1 = this._y1;

// If the quadtree has no extent, initialize them.
// Integer extent are necessary so that if we later double the extent,
Expand All @@ -20,6 +16,11 @@ export default function(x, y) {

// 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;
Expand All @@ -46,6 +47,9 @@ export default function(x, y) {
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;
Expand Down

0 comments on commit 4b1a8c5

Please sign in to comment.