use distanceSquaredToSegment instead as suggested by @ahocevar