Skip to content

Commit

Permalink
Add simulation.find. Fixes #31.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed May 12, 2016
1 parent 80ea690 commit 61ac684
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/simulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ export default function(nodes) {
return delete fixes[node.index], simulation;
},

find: function(x, y, radius) {
var i = 0,
n = nodes.length,
dx,
dy,
d2,
node,
closest;

if (radius == null) radius = Infinity;
else radius *= radius;

for (i = 0; i < n; ++i) {
node = nodes[i];
dx = x - node.x;
dy = y - node.y;
d2 = dx * dx + dy * dy;
if (d2 < radius) closest = node, radius = d2;
}

return closest;
},

on: function(name, _) {
return arguments.length > 1 ? (event.on(name, _), simulation) : event.on(name);
}
Expand Down

0 comments on commit 61ac684

Please sign in to comment.