Skip to content

Commit 92ed5c9

Browse files
authored
Create circle-and-rectangle-overlapping.cpp
1 parent 5914f3f commit 92ed5c9

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
bool checkOverlap(int radius, int x_center, int y_center, int x1, int y1, int x2, int y2) {
7+
x1 -= x_center;
8+
y1 -= y_center;
9+
x2 -= x_center;
10+
y2 -= y_center;
11+
const auto& x = (x1 > 0) ? x1 : (x2 < 0) ? x2 : 0;
12+
const auto& y = (y1 > 0) ? y1 : (y2 < 0) ? y2 : 0;
13+
return x * x + y * y <= radius * radius;
14+
}
15+
};
16+
17+
// Time: O(1)
18+
// Space: O(1)
19+
class Solution2 {
20+
public:
21+
bool checkOverlap(int radius, int x_center, int y_center, int x1, int y1, int x2, int y2) {
22+
x1 -= x_center;
23+
y1 -= y_center;
24+
x2 -= x_center;
25+
y2 -= y_center;
26+
const auto& x = (x1 * x2 > 0) ? min(abs(x1), abs(x2)) : 0;
27+
const auto& y = (y1 * y2 > 0) ? min(abs(y1), abs(y2)) : 0;
28+
return x * x + y * y <= radius * radius;
29+
}
30+
};

0 commit comments

Comments
 (0)