intersectsCircle method
Checks if this rectangle intersects with a circle
Mathematical Operations:
- Finds the closest point on the rectangle to the circle center
- Calculates squared distance between circle center and closest point
- 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);
}