1401. Circle and Rectangle Overlapping

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public boolean checkOverlap(int radius, int xCenter, int yCenter, int x1, int y1, int x2, int y2) {
int closestX = closet(x1, x2, xCenter);
int closestY = closet(y1, y2, yCenter);
return closestX * closestX + closestY * closestY <= radius * radius;
}

private int closet(int x1, int x2, int xCenter) {
if (x1 <= xCenter && xCenter <= x2) {
return 0;
}

if (xCenter < x1) {
return x1 - xCenter;
} else {
return xCenter - x2;
}
}
}

References

1401. Circle and Rectangle Overlapping