nearestRectangle method

Rectangle<num>? nearestRectangle(
  1. List<Rectangle<num>> rectangles,
  2. Point<num> p
)

Implementation

Rectangle<num>? nearestRectangle(
    List<Rectangle<num>> rectangles, Point<num> p) {
  if (rectangles.isEmpty) return null;

  Rectangle<num>? nearest;
  double? nearestDistance;

  for (var rect in rectangles) {
    var rectCenter = getRectangleCenter(rect);
    var distance = rectCenter.distanceTo(p);
    if (nearestDistance == null || distance < nearestDistance) {
      nearest = rect;
      nearestDistance = distance;
    }
  }

  return nearest;
}