distToSegment function
The distance from p to the segment a–b.
Implementation
double distToSegment(Vector2 p, Vector2 a, Vector2 b) {
final d = b - a;
final len2 = d.dot(d);
if (len2 == 0) return (p - a).length;
final t = ((p - a).dot(d) / len2).clamp(0.0, 1.0);
return (p - (a + d * t)).length;
}