getNearestPointInside static method

  1. @visibleForTesting
Vector3 getNearestPointInside(
  1. Vector3 point,
  2. Quad quad
)

Get the point inside (inclusively) the given Quad that is nearest to the given Vector3.

Implementation

@visibleForTesting
static Vector3 getNearestPointInside(Vector3 point, Quad quad) {
  // If the point is inside the axis aligned bounding box, then it's ok where
  // it is.
  if (pointIsInside(point, quad)) {
    return point;
  }

  // Otherwise, return the nearest point on the quad.
  final List<Vector3> closestPoints = <Vector3>[
    BetterInteractiveViewerBase.getNearestPointOnLine(
        point, quad.point0, quad.point1),
    BetterInteractiveViewerBase.getNearestPointOnLine(
        point, quad.point1, quad.point2),
    BetterInteractiveViewerBase.getNearestPointOnLine(
        point, quad.point2, quad.point3),
    BetterInteractiveViewerBase.getNearestPointOnLine(
        point, quad.point3, quad.point0),
  ];
  double minDistance = double.infinity;
  late Vector3 closestOverall;
  for (final Vector3 closePoint in closestPoints) {
    final double distance = math.sqrt(
      math.pow(point.x - closePoint.x, 2) +
          math.pow(point.y - closePoint.y, 2),
    );
    if (distance < minDistance) {
      minDistance = distance;
      closestOverall = closePoint;
    }
  }
  return closestOverall;
}