計算兩個矩形的重疊面積(均平行於坐標軸)


 1 '''
 2 input: rect1, rect2, 均為list,其分別為
 3 xl(left),yb(bottom),xr(right),yt(top)
 4 '''
 5 def calc_area(rect1, rect2):
 6     xl1, yb1, xr1, yt1 = rect1
 7     xl2, yb2, xr2, yt2 = rect2
 8     xmin = max(xl1, xl2)
 9     ymin = max(yb1, yb2)
10     xmax = min(xr1, xr2)
11     ymax = min(yt1, yt2)
12     width = xmax - xmin
13     height = ymax - ymin
14     if width <= 0 or height <= 0:
15         return 0
16     cross_square = width * height
17     return cross_square

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM