objective c - Find closest point from array of points

How would I find out which CGPoint, in my array, is the closest to nextPoint?

网友答案:

Have you considered simply iterating through your array and finding the distance between all the points?

You could store the distance in a new NSMutableArray and then grab the smallest value from it.

网友答案:

Just iterate and calculate like Pythagoras and compare the results.

sqrt( ( exp( x1 - x2, 2) + exp( y1 - y2, 2 ) ) );

You might want to check if CGFloat is a float or a double and use the float versions of these functions if appropriate.
There is a macro that defines this.

if (CGFLOAT_IS_DOUBLE == YES) {

If YES, then use the above, if NO, then use the function versions that end with f

If your set of points to compare is large enough you need to do it faster you could try various means of finding the shortest distance in subsets of your points in parallel then comparing the result of those operations.