**nearestNeighbor** lets you find the closest neighbor to a given point in a two-dimensional space. Demo: click around up there.

It uses d3’s voronoi geometry, but it convenient for more abstract uses where you might not want to draw anything onscreen at all. All it really adds is an algorithm from StackOverflow for finding which polygon generated by the voronoi geometry contains the point. And then it just brute-forces it. It’s idiotic. At this point you might as well just brute-force distances to every neighboring point, right? But maybe I can improve it to be non-worthless. “If the polygons form a planar graph with each edge labelled with its two polygon neighbors, then one simple way is to run a semi-infinite ray up from the point until it hits its first edge. Then the relevant containing polygon is one of that edge’s neighbors.” OK rpi.edu, I don’t know how to cast a ray until it hits its first edge.

First, generate your neighbor-finding function.

```
var near = nearestNeighbor(friends)
.x(function(d) { return d.age; })
.y(function(d) { return d.salary; })
```

Then use it.

`near({'age': 25, 'salary': 75000})`

Now you have the person out of your friends who is closest to being 25 and having a salary of $75,000. I guess you could use the accessors to do weighting, right? But idk if…

- http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
- http://stackoverflow.com/questions/8721406/how-to-determine-if-a-point-is-inside-a-2d-convex-polygon
- https://github.com/mbostock/d3/wiki/Voronoi-Geom
- https://en.wikipedia.org/wiki/Point_location
- http://cgm.cs.mcgill.ca/~athens/cs507/Projects/2002/JukkaKaartinen/
- https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=stochastic%20nearest%20neighbor
- https://github.com/NathanEpstein/kNear
- http://stackoverflow.com/questions/16774935/javascript-function-nearest-neighbor
- https://en.wikipedia.org/wiki/Nearest_neighbor_search
- http://stackoverflow.com/questions/1901139/closest-point-to-a-given-point
- http://mathoverflow.net/questions/49516/n-dimensional-voronoi-diagram