Skip to content

Commit

Permalink
quadtree.find(x, y, radius, *filter*)
Browse files Browse the repository at this point in the history
closes #19
  • Loading branch information
Fil committed Jun 5, 2020
1 parent 9804ee5 commit 1916c66
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ Returns an array of all data in the quadtree.

Returns the total number of data in the quadtree.

<a name="quadtree_find" href="#quadtree_find">#</a> <i>quadtree</i>.<b>find</b>(<i>x</i>, <i>y</i>[, <i>radius</i>])
<a name="quadtree_find" href="#quadtree_find">#</a> <i>quadtree</i>.<b>find</b>(<i>x</i>, <i>y</i>[, <i>radius</i>][, <i>filter</i>])
[<>](https://github.com/d3/d3-quadtree/blob/master/src/find.js "Source")

Returns the datum closest to the position ⟨*x*,*y*⟩ with the given search *radius*. If *radius* is not specified, it defaults to infinity. If there is no datum within the search area, returns undefined.
Returns the datum closest to the position ⟨*x*,*y*⟩ with the given search *radius* that satisfies the *filter*. If *radius* is not specified, it defaults to infinity. If *filter* is not specified, all data are considered. If there is no acceptable datum within the search area, returns undefined.

<a name="quadtree_visit" href="#quadtree_visit">#</a> <i>quadtree</i>.<b>visit</b>(<i>callback</i>)
[<>](https://github.com/d3/d3-quadtree/blob/master/src/visit.js "Source")
Expand Down
16 changes: 11 additions & 5 deletions src/find.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Quad from "./quad.js";

export default function(x, y, radius) {
export default function(x, y, radius, filter) {
var data,
x0 = this._x0,
y0 = this._y0,
Expand Down Expand Up @@ -58,10 +58,16 @@ export default function(x, y, radius) {
dy = y - +this._y.call(null, node.data),
d2 = dx * dx + dy * dy;
if (d2 < radius) {
var d = Math.sqrt(radius = d2);
x0 = x - d, y0 = y - d;
x3 = x + d, y3 = y + d;
data = node.data;
var f, d;
do {
if (!filter || filter(node.data)) f = node;
} while (!f && (node = node.next));
if (f) {
d = Math.sqrt(radius = d2);
x0 = x - d, y0 = y - d;
x3 = x + d, y3 = y + d;
data = f.data;
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/find-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,12 @@ tape("quadtree.find(x, y, null) treats the given radius as Infinity", function(t
test.deepEqual(q.find(20, 20, undefined), [0, 0]);
test.end();
});

tape("quadtree.find(x, y, radius, filter) considers only filtered points", function(test) {
var q = d3_quadtree.quadtree([[0, 0, "red"], [100, 0, "blue"], [0, 100, "red"], [100, 100, "red"], [100, 100, "blue"]]);
test.deepEqual(q.find(20, 0, null, d => d[2] == "red"), [0, 0, "red"]);
test.deepEqual(q.find(20, 0, null, d => d[2] == "blue"), [100, 0, "blue"]);
test.deepEqual(q.find(20, 0, 50, d => d[2] == "blue"), undefined);
test.deepEqual(q.find(20, 0, null, d => d[2] == "green"), undefined);
test.end();
});

0 comments on commit 1916c66

Please sign in to comment.