projectPoint method

Point projectPoint(
  1. Point point
)

Get closest point on this line segment to point

Returns the perpendicular projection clamped to the segment.

Implementation

Point projectPoint(Point point) {
  final dx = b.x - a.x;
  final dy = b.y - a.y;
  final lenSq = dx * dx + dy * dy;
  if (lenSq == 0) return a;

  var t = ((point.x - a.x) * dx + (point.y - a.y) * dy) / lenSq;
  t = t.clamp(0.0, 1.0);
  return Point(a.x + t * dx, a.y + t * dy);
}