Skip to content

Commit e39d994

Browse files
authored
Merge pull request #51 from crhallberg/closest-fix
Fix closest search infinite loop
2 parents 26fe09a + 3e50475 commit e39d994

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

quadtree.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,13 @@ class QuadTree {
307307
);
308308
// Together, a circle that encompasses the whole QuadTree
309309
maxDistance = outerReach + pointDistance;
310+
} else {
311+
// Make sure the largest search (maxDistance) contains enough points
312+
let maxOuter = new Circle(point.x, point.y, maxDistance);
313+
let maxDistanceTest = this.query(maxOuter);
314+
if (maxDistanceTest.length < count) {
315+
return maxDistanceTest;
316+
}
310317
}
311318

312319
// Binary search with Circle queries

test/quadtree.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,16 @@ describe('QuadTree', () => {
355355
expect(found).to.have.length(1);
356356
expect(found).to.contain(points[0]);
357357
});
358+
// Supplied maxDistance
359+
it('limits search to maxDistance', () => {
360+
found = quadtree.closest(new Point(0, 0), 3, 25);
361+
expect(found).to.have.length(1);
362+
expect(found).to.contain(points[0]);
363+
});
364+
it('gracefully fails search if nothing within maxDistance', () => {
365+
found = quadtree.closest(new Point(-100, 0), 3, 25);
366+
expect(found).to.have.length(0);
367+
});
358368
});
359369
describe('length', () => {
360370
let quadtree;

0 commit comments

Comments
 (0)