阅读背景:

两个矩形的交, 拓展可求得矩形的并

来源:互联网 

拍成一维的,取两个左端点的右边, 取两个右端点的左边
同理 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()def compute_



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: