# LEETCODE 1401. Circle and Rectangle Overlapping 解题思路分析

```输入：radius = 1, x_center = 0, y_center = 0, x1 = 1, y1 = -1, x2 = 3, y2 = 1

```输入：radius = 1, x_center = 0, y_center = 0, x1 = -1, y1 = 0, x2 = 0, y2 = 1

```输入：radius = 1, x_center = 1, y_center = 1, x1 = -3, y1 = -3, x2 = 3, y2 = 3

`输入：radius = 1, x_center = 1, y_center = 1, x1 = 1, y1 = -3, x2 = 2, y2 = -1输出：false`

• 1 <= radius <= 2000
• -10^4 <= x_center, y_center, x1, y1, x2, y2 <= 10^4
• x1 < x2
• y1 < y2

```int circleTop=y_center+radius; // 圆形最上端
x1 // 矩形最左端
x2 // 矩形最右端
y1 // 矩形最下端
y2 // 矩形最上端```
1. 圆形与矩形完全不相交，该情况下返回false。本情况用代码形容即是：
`circleTop<y1||circleBottom>y2||circleLeft>x2||circleRight<x1`

2. 圆形在矩形内部，该情况下返回true。本情况用代码形容即是：

`circleTop<=y2&&circleBottom>=y1&&circleLeft>=x1&&circleRight<=x2`

3. 剩下的情况即是两个图形或在x或者y上有交集，对于这种情况，不论是仅在x上有交集，或者仅在y上有交集，亦或是在x和y上都有交集，我们都无法确定2图形是否相交，比如下图三种情况下两个图形都不相交：

```public boolean checkOverlap(int radius, int x_center, int y_center, int x1, int y1, int x2, int y2) {
if(circleTop<y1||circleBottom>y2||circleLeft>x2||circleRight<x1){
return false;
}
if(circleTop<=y2&&circleBottom>=y1&&circleLeft>=x1&&circleRight<=x2){
return true;
}
for(int i=x1;i<=x2;i++){
}
for(int i=y1;i<=y2;i++){
}
return false;
}
// 计算两点间距离
double getDis(int x1, int y1, int x2, int y2){
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}```

Runtime: 0 ms, faster than 100.00% of Java online submissions for Circle and Rectangle Overlapping.

Memory Usage: 36.3 MB, less than 100.00% of Java online submissions for Circle and Rectangle Overlapping.