intersectsCircle method

bool intersectsCircle(
  1. double cx,
  2. double cy,
  3. double radius
)

Checks if this rectangle intersects with a circle

Mathematical Operations:

  1. Finds the closest point on the rectangle to the circle center
  2. Calculates squared distance between circle center and closest point
  3. Compares against squared radius (avoids expensive sqrt operation)

Implementation

bool intersectsCircle(double cx, double cy, double radius) {
  // Find closest x-coordinate on rectangle to circle center
  // Clamped between left and right edges
  final double closestX = math.max(x, math.min(cx, x + width));

  // Find closest y-coordinate on rectangle to circle center
  // Clamped between top and bottom edges
  final double closestY = math.max(y, math.min(cy, y + height));

  // Calculate distance components
  final double distanceX = cx - closestX;
  final double distanceY = cy - closestY;

  // Compare squared distance to squared radius
  return (distanceX * distanceX + distanceY * distanceY) <= (radius * radius);
}