getNearestDistance static method

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

Get o point distance o1 and o2 line segment distance https://blog.csdn.net/yjukh/article/details/5213577

Implementation

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

  final a = o2.distanceTo(o);
  final b = o1.distanceTo(o);
  final c = o1.distanceTo(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;
}