Circle and Rectangle Overlapping Problem
Description
LeetCode Problem 1401.
You are given a circle represented by its radius radius and center coordinates (xCenter, yCenter). You are also given an axis-aligned rectangle represented by (x1, y1, x2, y2), where (x1, y1) is the bottom-left corner and (x2, y2) is the top-right corner of the rectangle.
Return true if the circle and rectangle overlap; otherwise, return false.
Two shapes overlap if they share at least one point. Points on the boundaries are included, so touching at a single point also counts as overlapping.
Example 1:

Input: radius = 1, xCenter = 0, yCenter = 0, x1 = 1, y1 = -1, x2 = 3, y2 = 1
Output: true
Explanation:
- The circle is centered at (0, 0) with radius 1, and the rectangle extends from (1, -1) to (3, 1).
- The circle and rectangle touch at the point (1, 0), which lies on the circle's boundary and the rectangle's left edge.
- Since they share at least one point, they overlap. Therefore, return true.
Example 2:
Input: radius = 1, xCenter = 1, yCenter = 1, x1 = 1, y1 = -3, x2 = 2, y2 = -1
Output: false
Explanation:
- The circle is centered at (1, 1) with radius 1, while the rectangle extends from (1, -3) to (2, -1).
- The closest point on the rectangle to the circle's center is (1, -1). The distance between these points is 2, which is greater than the circle's radius of 1.
- Since the circle cannot reach the rectangle, they do not overlap. Therefore, return false.
Example 3:

Input: radius = 1, xCenter = 0, yCenter = 0, x1 = -1, y1 = 0, x2 = 0, y2 = 1
Output: true
Explanation:
- The circle is centered at (0, 0) with radius 1, and the rectangle extends from (-1, 0) to (0, 1).
- The point (0, 0) is both the center of the circle and the bottom-right corner of the rectangle.
- Since this point belongs to both shapes, they overlap. Therefore, return true.
Constraints:
1 <= radius <= 2000-10^4 <= xCenter, yCenter <= 10^4-10^4 <= x1 < x2 <= 10^4-10^4 <= y1 < y2 <= 10^4
Solution
Approach: Closest Point of the Rectangle
The circle and the rectangle share a point exactly when the rectangle's point closest to the center lies inside the circle (or on its edge). So we only need that one point and one distance check.
For an axis-aligned rectangle, the closest point can be found one coordinate at a time, because the squared distance (x - xCenter)^2 + (y - yCenter)^2 splits into an x part and a y part that can be minimized separately:
- The best
xin[x1, x2]isxCenteritself if it lies in the range, otherwise the nearer end:x = min(max(xCenter, x1), x2). This is called clamping. - In the same way,
y = min(max(yCenter, y1), y2).
Then the shapes overlap if dx * dx + dy * dy <= radius * radius, with dx = x - xCenter and dy = y - yCenter. If the center is inside the rectangle, the closest point is the center and the distance is 0.
Only checking the four corners is a common mistake: the circle can cross an edge of the rectangle without containing any corner, as in Example 1, where it touches the left edge at (1, 0). Everything here is an integer, so comparing squared distances (no sqrt) gives an exact answer, even for a circle that only touches the rectangle. The largest square is around 8 * 108, which fits in 32 bits.
Complexity: O(1) time and space.
class Solution:
def checkOverlap(self, radius: int, xCenter: int, yCenter: int, x1: int, y1: int, x2: int, y2: int) -> bool:
# Closest point of the rectangle to the center: clamp each coordinate into the rectangle.
x = min(max(xCenter, x1), x2)
y = min(max(yCenter, y1), y2)
dx, dy = x - xCenter, y - yCenter
return dx * dx + dy * dy <= radius * radius # integers only, so touching is exact