Skip to content

Commit 68840e5

Browse files
committed
1401. Circle and Rectangle Overlapping
1 parent 0078452 commit 68840e5

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
3+
1. 아이디어 : 기하학의 원리 활용
4+
직사각형은 x,y축과 평행하므로 원과 가장 가까운 점을 찾는다.
5+
그 점과 원의 중심사이의 거리가 반지름보다 크다면 겹치지않고, 이하라면 겹침
6+
7+
2. 시간복잡도 : O(1)
8+
9+
3. 자료구조/알고리즘 : 기하학
10+
11+
*/
12+
13+
class Solution {
14+
public boolean checkOverlap(int radius, int xCenter, int yCenter, int x1, int y1, int x2, int y2) {
15+
// 겹치면 true, 아니면 false
16+
17+
int y = Math.max(y1, Math.min(y2, yCenter));
18+
int x = Math.max(x1, Math.min(x2, xCenter));
19+
20+
if(((y-yCenter)*(y-yCenter) + (x-xCenter)*(x-xCenter))<=radius*radius) return true;
21+
22+
return false;
23+
}
24+
}

0 commit comments

Comments
 (0)