Circle and Rectangle Overlapping LeetCode solution in C++

1
class Solution {
2
public:
3
bool checkOverlap(int radius, int xCenter, int yCenter, int x1, int y1, int x2, int y2) {
4
// nearest_x = x1 when xCenter<x1<x2 (OR) x2 when x1<x2<xCenter (OR) xCenter
5
// when x1<xCenter<x2
6
int nearest_x = (xCenter < x1) ? x1 : (xCenter > x2) ? x2 : xCenter;
7
// same logic for nearest_y as in nearest_x
8
int nearest_y = (yCenter < y1) ? y1 : (yCenter > y2) ? y2 : yCenter;
9
int dist_x = xCenter - nearest_x, dist_y = yCenter - nearest_y;
10
return dist_x * dist_x + dist_y * dist_y <= radius * radius;
11
}
12
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0