给你一个以 (radius, xCenter, yCenter)
表示的圆和一个与坐标轴平行的矩形 (x1, y1, x2, y2)
,其中 (x1, y1)
是矩形左下角的坐标,而 (x2, y2)
是右上角的坐标。
如果圆和矩形有重叠的部分,请你返回 true
,否则返回 false
。
换句话说,请你检测是否 存在 点 (xi, yi)
,它既在圆上也在矩形上(两者都包括点落在边界上的情况)。
示例 1 :
输入:radius = 1, xCenter = 0, yCenter = 0, x1 = 1, y1 = -1, x2 = 3, y2 = 1 输出:true 解释:圆和矩形存在公共点 (1,0) 。
示例 2 :
输入:radius = 1, xCenter = 1, yCenter = 1, x1 = 1, y1 = -3, x2 = 2, y2 = -1 输出:false
示例 3 :
输入:radius = 1, xCenter = 0, yCenter = 0, x1 = -1, y1 = 0, x2 = 0, y2 = 1 输出:true
提示:
1 <= radius <= 2000
-104 <= xCenter, yCenter <= 104
-104 <= x1 < x2 <= 104
-104 <= y1 < y2 <= 104
对于一个点
而对于矩形内(包括边界)的点,它们的横坐标
因此,问题转化为求
对于
- 如果
$x_1 \leq xCenter \leq x_2$ ,那么$|x - xCenter|$ 的最小值为$0$ ; - 如果
$xCenter \lt x_1$ ,那么$|x - xCenter|$ 的最小值为$x_1 - xCenter$ ; - 如果
$xCenter \gt x_2$ ,那么$|x - xCenter|$ 的最小值为$xCenter - x_2$ 。
同理,我们可以求出
即
class Solution:
def checkOverlap(
self,
radius: int,
xCenter: int,
yCenter: int,
x1: int,
y1: int,
x2: int,
y2: int,
) -> bool:
def f(i: int, j: int, k: int) -> int:
if i <= k <= j:
return 0
return i - k if k < i else k - j
a = f(x1, x2, xCenter)
b = f(y1, y2, yCenter)
return a * a + b * b <= radius * radius
class Solution {
public boolean checkOverlap(
int radius, int xCenter, int yCenter, int x1, int y1, int x2, int y2) {
int a = f(x1, x2, xCenter);
int b = f(y1, y2, yCenter);
return a * a + b * b <= radius * radius;
}
private int f(int i, int j, int k) {
if (i <= k && k <= j) {
return 0;
}
return k < i ? i - k : k - j;
}
}
class Solution {
public:
bool checkOverlap(int radius, int xCenter, int yCenter, int x1, int y1, int x2, int y2) {
auto f = [](int i, int j, int k) -> int {
if (i <= k && k <= j) {
return 0;
}
return k < i ? i - k : k - j;
};
int a = f(x1, x2, xCenter);
int b = f(y1, y2, yCenter);
return a * a + b * b <= radius * radius;
}
};
func checkOverlap(radius int, xCenter int, yCenter int, x1 int, y1 int, x2 int, y2 int) bool {
f := func(i, j, k int) int {
if i <= k && k <= j {
return 0
}
if k < i {
return i - k
}
return k - j
}
a := f(x1, x2, xCenter)
b := f(y1, y2, yCenter)
return a*a+b*b <= radius*radius
}
function checkOverlap(
radius: number,
xCenter: number,
yCenter: number,
x1: number,
y1: number,
x2: number,
y2: number,
): boolean {
const f = (i: number, j: number, k: number) => {
if (i <= k && k <= j) {
return 0;
}
return k < i ? i - k : k - j;
};
const a = f(x1, x2, xCenter);
const b = f(y1, y2, yCenter);
return a * a + b * b <= radius * radius;
}