Fork me on GitHub

青禹小生

雁驰万里却作禽,鱼未得水空有鳞。 花开花落花不语,昨是昨非昨亦今。

导航

计算两个矩形的重叠面积(均平行于坐标轴)

 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

 

posted on 2017-12-02 18:52  司徒道  阅读(4290)  评论(0编辑  收藏  举报