forked from mehul-1607/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1401.java
70 lines (65 loc) · 2.39 KB
/
_1401.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.fishercoder.solutions;
/**
* 1401. Circle and Rectangle Overlapping
*
* Given a circle represented as (radius, x_center, y_center) and an axis-aligned rectangle represented as (x1, y1, x2, y2), where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the rectangle.
* Return True if the circle and rectangle are overlapped otherwise return False.
* In other words, check if there are any point (xi, yi) such that belongs to the circle and the rectangle at the same time.
*
* Example 1:
* Input: radius = 1, x_center = 0, y_center = 0, x1 = 1, y1 = -1, x2 = 3, y2 = 1
* Output: true
* Explanation: Circle and rectangle share the point (1,0)
*
* Example 2:
* Input: radius = 1, x_center = 0, y_center = 0, x1 = -1, y1 = 0, x2 = 0, y2 = 1
* Output: true
*
* Example 3:
* Input: radius = 1, x_center = 1, y_center = 1, x1 = -3, y1 = -3, x2 = 3, y2 = 3
* Output: true
*
* Example 4:
* Input: radius = 1, x_center = 1, y_center = 1, x1 = 1, y1 = -3, x2 = 2, y2 = -1
* Output: false
*
* Constraints:
* 1 <= radius <= 2000
* -10^4 <= x_center, y_center, x1, y1, x2, y2 <= 10^4
* x1 < x2
* y1 < y2
* */
public class _1401 {
public static class Solution1 {
public boolean checkOverlap(int radius, int xCenter, int yCenter, int x1, int y1, int x2, int y2) {
if (x1 <= xCenter && x2 >= xCenter && y1 <= yCenter && y2 >= yCenter) {
return true;
}
int circleDistance = radius * radius;
for (int x = x1; x <= x2; x++) {
if (dist(x, y1, xCenter, yCenter) <= circleDistance) {
return true;
}
}
for (int x = x1; x <= x2; x++) {
if (dist(x, y2, xCenter, yCenter) <= circleDistance) {
return true;
}
}
for (int y = y1; y <= y2; y++) {
if (dist(x1, y, xCenter, yCenter) <= circleDistance) {
return true;
}
}
for (int y = y1; y <= y2; y++) {
if (dist(x2, y, xCenter, yCenter) <= circleDistance) {
return true;
}
}
return false;
}
private int dist(int x1, int y1, int x2, int y2) {
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
}
}