zoukankan      html  css  js  c++  java
  • 面试常考题 两个矩形的交, 拓展可求得矩形的并

    拍成一维的,取两个左端点的右边, 取两个右端点的左边
    同理 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
Copyright © 2011-2022 走看看