leetcode1401

 1 class Solution:
 2     def checkOverlap(self, radius: int, x_center: int, y_center: int, x1: int, y1: int, x2: int, y2: int) -> bool:
 3         
 4         # Getting the coords of centre of rectangle
 5         c1 = (x2 + x1) / 2
 6         c2 = (y2 + y1) / 2
 7         
 8         # Getting distance between centre of circle and rectangle in x, y direction
 9         # Abs for suppose centre of circle in 3rd quad and of rectangle in 1st quad
10         v1 = abs(x_center - c1) 
11         v2 = abs(y_center - c2)
12         
13         # Getting half of breadth and lenght of rectangle
14         h1 = (x2 - x1) / 2 
15         h2 = (y2 - y1) / 2
16          
17         # Difference in distance between (i) half of side of rectangle (h1,h2) (ii) distance between circle and rectangle
18         # It can be negative For eg. If circle is completely in rectangle. Hence taking max with zero
19         u1 = max(0, v1 - h1)
20         u2 = max(0, v2 - h2)
21         
22         # Now try to think yourself for this last step
23         # Hint is hypotenuse !!
24         return (u1 * u1 + u2 * u2 <= radius * radius)
25 
26         # Hope you get it :)

算法类型:几何图形计算。

参考:https://leetcode.com/problems/circle-and-rectangle-overlapping/discuss/563319/Python-O(1)-With-Detailed-Explanation

猜你喜欢

转载自www.cnblogs.com/asenyang/p/12635713.html
今日推荐