#1401
Medium Algorithms Circle and rectangle overlapping
Math Geometry
49.8% acceptance
Feb 25, 2026
402
85
You are given a circle represented as (radius, xCenter, yCenter) and an axis-aligned rectangle represented as (x1, y1, x2, y2), where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the rectangle.
Return true if the circle and rectangle are overlapped otherwise return false. In other words, check if there is any point (xi, yi) that belongs to the circle and the rectangle at the same time.
Solution
Rust
Time O(1)
Space O(1)
impl Solution {
pub fn check_overlap(radius: i32, x_center: i32, y_center: i32, x1: i32, y1: i32, x2: i32, y2: i32) -> bool {
let nearest_x = x_center.clamp(x1, x2);
let nearest_y = y_center.clamp(y1, y2);
let dx = x_center - nearest_x;
let dy = y_center - nearest_y;
dx * dx + dy * dy <= radius * radius
}
}