What is the best way to find the nearest scatter plot point (circle) relative to a given coordinate set?
<p>Let's say I have 16 circles in an 2 x 8 grid:</p>

<pre><code>svg = d3.select(body).append('svg').attr('height,h).attr('width',w);

svg.selectAll('.centroids')
      .data(d3.range(0,16))
      .enter()
      .append('circle')
      .attr('class','centroids')
      .attr('r','5')
      .attr('cx', function(d,i) { return i * 10; })
      .attr('cy', function(d,i) {
        if (i &gt; 7) return 20;
        return 10;
      });
</code></pre>

<p>Given a random coordinate in that space, how do I determine the nearest <code>.centroid</code> point? </p>

<p>One way in N time is of course to loop through all the points, measuring the hypotenuse to the difference in <code>x</code> and <code>y</code> coordinates, choosing the smallest value. </p>

<p>I'd like to find a better way though. Does anyone know an optimized way?</p>