相关疑难解决方法(0)

两个矩形的交点和差异

搜索互联网并没有给出以下问题的满意解决方案.给定一个Rectangle定义如下的类:

class Rectangle:

    def __init__(self, x1, y1, x2, y2):
        if x1 > x2 or y1 > y2:
            raise ValueError('coordinates are invalid')
        self.x1, self.y1, self.x2, self.y2 = x1, y1, x2, y2

    @property
    def width(self):
        return self.x2 - self.x1

    @property
    def height(self):
        return self.y2 - self.y1

    def bounds(self, other):
        return Rectangle(min(self.x1, other.x1), min(self.y1, other.y1),
                         max(self.x2, other.x2), max(self.y2, other.y2))

    def intersect(self, other):
        return self.x1 < other.x2 and self.x2 > other.x1 and \
               self.y1 < other.y2 and self.y2 > other.y1
Run Code Online (Sandbox Code Playgroud)

你将如何创建一个方法来获得交集和生成器来获得两个矩形的差异?据推测,需要更完整地实现以下方法,但我不清楚应该写什么. …

python geometry python-3.x

4
推荐指数
1
解决办法
5170
查看次数

标签 统计

geometry ×1

python ×1

python-3.x ×1