Leetcode 1401.圓和矩形是否有重疊(Circle and Rectangle Overlapping)

Leetcode 1401.圓和矩形是否有重疊

1 題目描述(Leetcode題目鏈接

  給你一個以 (radius, x_center, y_center) 表示的圓和一個與座標軸平行的矩形 (x1, y1, x2, y2),其中 (x1, y1) 是矩形左下角的座標,(x2, y2) 是右上角的座標。

如果圓和矩形有重疊的部分,請你返回 True ,否則返回 False 。

換句話說,請你檢測是否 存在 點 (xi, yi) ,它既在圓上也在矩形上(兩者都包括點落在邊界上的情況)。

在這裏插入圖片描述

輸入:radius = 1, x_center = 0, y_center = 0, x1 = 1, y1 = -1, x2 = 3, y2 = 1
輸出:true
解釋:圓和矩形有公共點 (1,0)

提示:

  • 1<=radius<=20001 <= radius <= 2000
  • 104<=xcenter,ycenter,x1,y1,x2,y2<=104-10^4 <= x_center, y_center, x1, y1, x2, y2 <= 10^4
  • x1<x2x1 < x2
  • y1<y2y1 < y2

2 題解

  參考

class Solution:
    def checkOverlap(self, radius: int, x_center: int, y_center: int, x1: int, y1: int, x2: int, y2: int) -> bool:
        v = (abs(x_center - (x1 + x2)/2), abs(y_center - (y1 + y2)/2))
        h = ((x2 - x1)/2, (y2 - y1)/2)
        u = (max(0, v[0] - h[0]), max(0, v[1] - h[1]))
        return u[0]**2 + u[1]**2 <= radius**2
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章