码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode1401

时间:2020-04-05 09:35:21      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:problems   span   几何   detail   class   ret   hal   last   ref   

 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

leetcode1401

标签:problems   span   几何   detail   class   ret   hal   last   ref   

原文地址:https://www.cnblogs.com/asenyang/p/12635713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!