Circle and Rectangle Overlapping LeetCode solution in C++
1class Solution {2public:3bool 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) xCenter5// when x1<xCenter<x26int nearest_x = (xCenter < x1) ? x1 : (xCenter > x2) ? x2 : xCenter;7// same logic for nearest_y as in nearest_x8int nearest_y = (yCenter < y1) ? y1 : (yCenter > y2) ? y2 : yCenter;9int dist_x = xCenter - nearest_x, dist_y = yCenter - nearest_y;10return dist_x * dist_x + dist_y * dist_y <= radius * radius;11}12};