getNearestDistance static method

double getNearestDistance(
  1. DFPosition o1,
  2. DFPosition o2,
  3. DFPosition o
)

Get o point distance o1 and o2 line segment distance

Implementation

static double getNearestDistance(DFPosition o1, DFPosition o2, DFPosition o) {
  if (o1 == o || o2 == o) return 0;

  final a = DFUtil.distanceTo(o2, o);
  final b = DFUtil.distanceTo(o1, o);
  final c = DFUtil.distanceTo(o1, o2);

  if (a * a >= b * b + c * c) return b;
  if (b * b >= a * a + c * c) return a;

  // 海伦公式
  final l = (a + b + c) / 2;
  final area = sqrt(l * (l - a) * (l - b) * (l - c));
  return 2 * area / c;
}