distanceToSegment static method
Calculates perpendicular distance from point p to segment between a and b.
Implementation
static double distanceToSegment(Offset p, Offset a, Offset b) {
final l2 = (b - a).distanceSquared;
if (l2 == 0) return (p - a).distance;
final t = math.max(
0.0,
math.min(
1.0,
((p.dx - a.dx) * (b.dx - a.dx) + (p.dy - a.dy) * (b.dy - a.dy)) / l2,
),
);
final projection = Offset(
a.dx + t * (b.dx - a.dx),
a.dy + t * (b.dy - a.dy),
);
return (p - projection).distance;
}