Skip to content

Commit 528451a

Browse files
committed
2018_06_07/overlap.py
1 parent 47cc5b4 commit 528451a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

weeks/2018_06_07/overlap.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
# Author: xurongzhong#126.com wechat:pythontesting qq:37391319
4+
# 技术支持 钉钉群:21745728(可以加钉钉pythontesting邀请加入)
5+
# qq群:144081101 591302926 567351477
6+
# CreateDate: 2018-6-07
7+
8+
9+
def get_area(pos):
10+
left, top, right, buttom = pos
11+
left = max(0, left)
12+
top = max(0, top)
13+
width = right - left
14+
height = buttom - top
15+
return (width*height, left, top, right, buttom)
16+
17+
18+
def overlap(pos1, pos2):
19+
area1, left1, top1, right1, buttom1 = get_area(pos1)
20+
area2, left2, top2, right2, buttom2 = get_area(pos2)
21+
22+
left = max(left1, left2)
23+
top = max(top1, top2)
24+
left = max(0, left)
25+
top = max(0, top)
26+
right = min(right1, right2)
27+
buttom = min(buttom1, buttom2)
28+
29+
if right <= left or buttom <= top:
30+
area = 0
31+
else:
32+
area = (right - left)*(buttom - top)
33+
34+
return (area, area1, area2, float(area)/area1, float(area)/area2)
35+
36+
print(overlap((60, 188, 260, 387), (106, 291, 340, 530)))
37+
38+

0 commit comments

Comments
 (0)