Determine whether a circle and an axis-aligned rectangle overlap in the plane.
1401. Circle and Rectangle Overlapping
gfgProblem
Given a circle with center and radius , and an axis-aligned rectangle whose bottom-left corner is and top-right corner is , determine whether the two shapes overlap.
The circle and rectangle are considered overlapping if they share at least one point. Touching the boundary counts as overlapping.
Goal
Return true if the circle intersects the rectangle, otherwise return false.
Clarification
The rectangle sides are parallel to the coordinate axes. The circle may be entirely inside the rectangle, the rectangle may be entirely inside the circle, or they may intersect partially.
Input Format
A single test case is provided as integers/coordinates describing:
- circle center
(x_c, y_c)and radiusr - rectangle corners
(x_1, y_1)and(x_2, y_2)
Output Format
Return a boolean value indicating whether the circle and rectangle overlap.
Constraints
- The rectangle is axis-aligned.
- Boundary contact counts as overlap.
- Coordinates and radius fit in standard 32-bit integer ranges in typical interview settings.
Example 1
Input
circle = (1, 1), r = 1; rectangle = [(0, 0), (2, 2)]
Output
true
Explanation
The circle is fully inside the rectangle, so they overlap.
Example 2
Input
circle = (1, 1), r = 1; rectangle = [(3, 0), (5, 2)]
Output
false
Explanation
The closest point in the rectangle to the circle center is (3, 1), which is 2 units away from the center, greater than the radius.
Show 1 more example
Example 3
Input
circle = (0, 0), r = 5; rectangle = [(4, 4), (8, 8)]
Output
true
Explanation
The point (4, 4) lies on the circle boundary since its distance from the center is , which is less than 5, so the shapes overlap.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.