distanceToLine method

double distanceToLine(
  1. Offset lineStart,
  2. Offset lineEnd
)

Implementation

double distanceToLine(Offset lineStart, Offset lineEnd) {
  double a = this.dx - lineStart.dx;
  double b = this.dy - lineStart.dy;
  double c = lineEnd.dx - lineStart.dx;
  double d = lineEnd.dy - lineStart.dy;

  double dot = a * c + b * d;
  double lenSq = c * c + d * d;
  double param = (lenSq != 0) ? dot / lenSq : -1;

  double xx, yy;

  if (param < 0) {
    xx = lineStart.dx;
    yy = lineStart.dy;
  } else if (param > 1) {
    xx = lineEnd.dx;
    yy = lineEnd.dy;
  } else {
    xx = lineStart.dx + param * c;
    yy = lineStart.dy + param * d;
  }

  double dx = this.dx - xx;
  double dy = this.dy - yy;

  return sqrt(dx * dx + dy * dy);
}