Quick tip: if you want to compare distances based on points, it's not necessary to do the square roots.
E.g. to see if P-to-Q is a smaller distance than Q-to-R, just check (pseudocode):
square(P.x-Q.x) + square(P.y-Q.y) < square(Q.x-R.x) + square(Q.y-R.y)