a little trick: finding the closest

Let’s say you have a bunch of points and you need to either order them by distance to some fixed point, or find the closest.

Euclidean distance is easy, right?

\sqrt{{(x_j - x_i)}^2 + {(y_j - y_i)}^2}

Hold it!  Why are you taking the square root?

If all you need  is to rank by distance,  then ranking by distance squared will give you the same result. Forget getting the square root.

Obviously, this makes no discernible difference for onesie-twosie calculations. But can make a big difference if you have lots to do.

Jus’ sayin’.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s