拍成一维的,取两个左端点的右边, 取两个右端点的左边
同理 y轴
def compute_intersect(rect1, rect2):
x0, y0, x1, y1 = rect1
x2, y2, x3, y3 = rect2
x4 = max(x0, x2)
y4 = min(y0, y2)
x5 = min(x1, x3)
y5 = max(y1, y3)
assert x4 <= x5 and y4 >= y5, "输入无交集"
print("({}, {})".format(x4, y4))
print("({}, {})".format(x5, y5))
return (x5 - x4) * (y4 - y5)
def test_intersect():
rect1 = (0, 3, 3, 0)
rect2 = (2, 5, 5, 2)
intersect = compute_intersect(rect1, rect2)
print(intersect)
test_intersect()
原文:https://www.cnblogs.com/Draymonder/p/11279584.html