title: efficient algorithm to find nearest point in a graph that does not have a known equation
body: <p>I'm asking this questions out of curiostity, since my quick and dirty implementation seems to be good enough.  However I'm curious what a better implementation would be.</p>
  
  <p>I have a graph of real world data.  There are no duplicate X values and the X value increments at a consistant rate across the graph, but Y data is based off of real world output.  I want to find the nearest point on the graph from an arbitrary given point P programmatically.  I'm trying to find an efficient (ie fast) algorithm for doing this.  I don't need the the exact closest point, I can settle for a point that is 'nearly' the closest point.</p>
  
  <p>The obvious lazy solution is to increment through every single point in the graph, calculate the distance, and then find the minimum of the distance.  This however could theoretically be slow for large graphs; too slow for what I want.</p>
  
  <p>Since I only need an approximate closest point I imagine the ideal fastest equation would involve generating a best fit line and using that line to calculate where the point should be in real time; but that sounds like a potential mathematical headache I'm not about to take on.</p>
  
  <p>My solution is a hack which works only because I assume my point P isn't arbitrary, namely I assume that P will usually be close to my graph line and when that happens I can cross out the distant X values from consideration.  I calculating how close the point on the line that shares the X coordinate with P is and use the distance between that point and P to calculate the largest/smallest X value that could possible be closer points.</p>
  
  <p>I can't help but feel there should be a faster algorithm then my solution (which is only useful because I assume 99% of the time my point P will be a point close to the line already).  I tried googling for better algorithms but found so many algorithms that didn't quite fit that it was hard to find what I was looking for amongst all the clutter of inappropriate algorithms.  So, does anyone here have a suggested algorithm that would be more efficient?  Keep in mind I don't need a full algorithm since what I have works for my needs, I'm just curious what the proper solution would have been. </p>