Comparing Distances

sqrt is considered an expensive (i.e. slow) function

Avoid using it if you can
For example, to determine which of 2 points (P1 or P2) is closer to point P0:

  dist1 = (x1-x0)*(x1-x0) + (y1-y0)*(y1-y0) + (z1-z0)*(z1-z0)
  dist2 = (x2-x0)*(x2-x0) + (y2-y0)*(y2-y0) + (z2-z0)*(z2-z0)
  if dist1 < dist2:
        P1 is closer
  else:
        P2 is closer