getVerticalPointOnLine static method

Offset getVerticalPointOnLine(
  1. Offset target,
  2. Offset begin,
  3. Offset end
)

获取点到直线上的垂足

Implementation

static Offset getVerticalPointOnLine(
    Offset target, Offset begin, Offset end) {
  double dx = begin.dx - end.dx;
  double dy = begin.dy - end.dy;
  if (dx.abs() < 0.00000001 && dy.abs() < 0.00000001) {
    return begin;
  }
  double u = ((target.dx - begin.dx) * dx + (target.dy - begin.dy) * dy) /
      (dx * dx + dy * dy);
  return Offset((begin.dx + u * dx), (begin.dy + u * dy));
}