两个矩形是否相撞

http://vip.cocode.cc/guacode/code/685

def intesects(rect1, rect2):
    x1, y1, w1, h1 = rect1
    x2, y2, w2, h2 = rect2
    return x1 + w1 > x2 and x2 + w2 > x1 and \
           y1 + h1 > y2 and y2 + h2 > y1


def test():
    data = [
        # rect1             rect2           结果
        [[0, 0, 100, 50], [50, 20, 100, 50], True],
        [[10, 20, 60, 40], [50, 43, 73, 50], True],
        # 下面是十字相交矩形
        [[0, 0, 100, 50], [25, -25, 50, 100], True],
    ]
    for d in data:
        a, b, r = d
        print('测试', d)
        if intesects(a, b) == r:
            print('测试成功')
        else:
            print('测试出错')


if __name__ == '__main__':
    test()

你可能感兴趣的:(两个矩形是否相撞)