This "Medium" geometry problem asks you to determine if a circle and an axis-aligned rectangle overlap. You are given the radius and center coordinates of the circle, as well as the coordinates of the bottom-left and top-right corners of the rectangle. Overlapping includes cases where the circle is inside the rectangle, the rectangle is inside the circle, or they just touch.
Companies like Microsoft and Google use this to test your ability to handle geometric logic and edge cases. It requires you to simplify a continuous problem (all points in a circle) to a discrete comparison. It evaluations your skill in finding the "nearest point" on a shape and using the distance formula.
The primary pattern is the Nearest Point on Rectangle logic. To check if a circle overlaps with a rectangle, you find the point on the rectangle that is closest to the circle's center . If the distance from this closest point to the center is less than or equal to the radius, they overlap. The closest is max(x1, min(xCenter, x2)) and the closest is max(y1, min(yCenter, y2)).
Rectangle: [0, 0] to [4, 4]. Circle: Center (5, 2), Radius 2.
min(5, 4) = 4, max(0, 4) = 4. Closest .min(2, 4) = 2, max(0, 2) = 2. Closest .A common error is trying to check only the corners of the rectangle. A circle could overlap with the side of a rectangle without covering any of its corners. Another mistake is using the square root in the distance formula, which can lead to precision errors; it's always better to compare dx^2 + dy^2 to radius^2.
For any "overlap" or "collision" problem between a circle and another shape, the most robust method is finding the "shortest distance" from the circle center to the shape.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Rectangle Area | Medium | Solve | |
| Valid Square | Medium | Solve | |
| Minimum Cuts to Divide a Circle | Easy | Solve | |
| Rectangle Overlap | Easy | Solve | |
| Mirror Reflection | Medium | Solve |