-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #30
- Loading branch information
Showing
4 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export default function(x, y, radius, filter) { | ||
const quadtree = this, | ||
result = [], | ||
radius2 = radius * radius, | ||
accept = filter | ||
? d => filter(d) && result.push(d) | ||
: d => result.push(d); | ||
|
||
quadtree.visit(function(node, x1, y1, x2, y2) { | ||
if (node.length) { | ||
return x1 >= x + radius || y1 >= y + radius || x2 < x - radius || y2 < y - radius; | ||
} | ||
|
||
const dx = +quadtree._x.call(null, node.data) - x, | ||
dy = +quadtree._y.call(null, node.data) - y; | ||
if (dx * dx + dy * dy < radius2) { | ||
do { accept(node.data); } while (node = node.next); | ||
} | ||
}); | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var tape = require("tape"), | ||
d3_quadtree = require("../"); | ||
|
||
tape("quadtree.findInCircle(x, y, radius) returns all the points within the search radius of the given [x, y]", function(test) { | ||
const points = [[0, 0], [100, 0], [0, 100], [100, 100]]; | ||
const q = d3_quadtree.quadtree(points); | ||
test.deepEqual(q.findInCircle(20, 20, Infinity), points); | ||
test.deepEqual(q.findInCircle(20, 20, 20 * Math.SQRT2 + 1e-6), [points[0]]); | ||
test.deepEqual(q.findInCircle(20, 20, 20 * Math.SQRT2 - 1e-6), []); | ||
test.deepEqual(q.findInCircle(50, 0, 51), [points[0], points[1]]); | ||
test.end(); | ||
}); | ||
|
||
tape("quadtree.findInCircle(x, y, radius, filter) returns all the points within the search radius of the given [x, y] and passing filter", function(test) { | ||
const points = [[0, 0, "a"], [0, 0, "b"], [100, 0, "a"], [0, 100, "b"], [100, 100, "a"]]; | ||
const q = d3_quadtree.quadtree(points); | ||
const filter = d => d[2] === "a"; | ||
test.deepEqual(q.findInCircle(20, 20, Infinity, filter), points.filter(filter)); | ||
test.deepEqual(q.findInCircle(0, 0, 2, filter), [points[0]]); | ||
test.end(); | ||
}); |