Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions quadtree.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class QuadTree {
this.southeast = new QuadTree(se, this.capacity);
let sw = new Rectangle(x - w, y + h, w, h);
this.southwest = new QuadTree(sw, this.capacity);

this.divided = true;
}

Expand All @@ -243,13 +243,16 @@ class QuadTree {
return false;
}

if (this.points.length < this.capacity) {
this.points.push(point);
return true;
}

if (!this.divided) {
if (this.points.length < this.capacity) {
this.points.push(point);
return true;
}

this.subdivide();

let p;
while (p = this.points.shift()) this.insert(p);
}

return (this.northeast.insert(point) || this.northwest.insert(point) ||
Expand All @@ -265,16 +268,17 @@ class QuadTree {
return found;
}

for (let p of this.points) {
if (range.contains(p)) {
found.push(p);
}
}
if (this.divided) {
this.northwest.query(range, found);
this.northeast.query(range, found);
this.southwest.query(range, found);
this.southeast.query(range, found);
} else {
for (let p of this.points) {
if (range.contains(p)) {
found.push(p);
}
}
}

return found;
Expand Down