Coordinates to distance function