使用两个左下角和右上角检查两个矩形是否在python中重叠

时间:2016-11-24 23:22:14

标签: python python-3.x

class Point:

    def __init__(self, xcoord=0, ycoord=0):
        self.x = xcoord
        self.y = ycoord

class Rectangle:
    def __init__(self, bottom_left, top_right, colour):
        self.bottom_left = bottom_left
        self.top_right = top_right
        self.colour = colour

    def intersects(self, other):

我试图看看两个矩形是否基于右上角和左下角相交,但是当我创建函数时:

def intersects(self, other):
    return self.top_right.x>=other.top_right.x>=self.bottom_left.x and self.top_right.x>=other.bottom_left.x>=self.bottom_left.x and self.top_right.y>=other.top_right.y>=self.bottom_left.y and self.top_right.x>=other.bottom_left.x>=self.bottom_left.x

输入时,该函数将返回false:

r1=Rectangle(Point(1,1), Point(2,2), 'blue')
r3=Rectangle(Point(1.5,0), Point(1.7,3), 'red')
r1.intersects(r3)

进入shell。

5 个答案:

答案 0 :(得分:13)

您可以使用Separating Axis Theorem的简单版本来测试交叉点。如果矩形不相交,则右侧中的至少一个将位于另一个矩形的左侧的左侧(即,它将是分离轴),或反之亦然,或者顶侧中的一个将是低于另一个矩形的底部,反之亦然。

因此,更改测试以检查它们是否不相交:

def intersects(self, other):
    return not (self.top_right.x < other.bottom_left.x or self.bottom_left.x > other.top_right.x or self.top_right.y < other.bottom_left.y or self.bottom_left.y > other.top_right.y)

此代码假定“top”的y值大于“bottom”(y减少屏幕),因为这就是你的例子似乎如何工作。如果您正在使用其他约定,那么您只需翻转y比较的符号。

答案 1 :(得分:1)

我最近遇到了这个问题,今天遇到了命名的元组,所以我想去尝试一下:

from collections import namedtuple

RECT_NAMEDTUPLE = namedtuple('RECT_NAMEDTUPLE', 'x1 x2 y1 y2')

Rect1 = RECT_NAMEDTUPLE(10,100,40,80)
Rect2 = RECT_NAMEDTUPLE(20,210,10,60)

def overlap(rec1, rec2):
  if (Rect2.x2 > Rect1.x1 and Rect2.x2 < Rect1.x2) or \
     (Rect2.x1 > Rect1.x1 and Rect2.x1 < Rect1.x2):
    x_match = True
  else:
    x_match = False
  if (Rect2.y2 > Rect1.y1 and Rect2.y2 < Rect1.y2) or \
     (Rect2.y1 > Rect1.y1 and Rect2.y1 < Rect1.y2):
    y_match = True
  else:
    y_match = False
  if x_match and y_match:
    return True
  else:
    return False

print ("Overlap found?", overlap(Rect1, Rect2))

Overlap found? True

答案 2 :(得分:0)

也可以从多边形开始使用多边形(例如,具有[x0,y0,x1,y1]的矩形

from shapely.geometry import Polygon
import numpy as np

rect1=np.array([0  ,0 , 4,  4])
rect2=np.array([1 , 1 , 5 , 5])

def overlap(rect1,rect2):
    try:
        p1 = Polygon([(rect1[0],rect1[1]), (rect1[1],rect1[1]),(rect1[2],rect1[3]),(rect1[2],rect1[1])])
        p2 = Polygon([(rect2[0],rect2[1]), (rect2[1],rect2[1]),(rect2[2],rect2[3]),(rect2[2],rect2[1])])
        return(p1.intersects(p2))
    except:
        return True

print(overlap(rect1,rect2))

答案 3 :(得分:0)

我要做的是弄清楚哪个矩形在顶部,在底部。也是在左边,在右边最终,我们正在谈论要比较的两个相同的矩形。但是获得右/左和上/下帮助有助于简化条件。一旦我们获得了右/左和上/下,我们就可以比较重叠,非重叠和包含。

class Rectangle:
# Create rectangle with center at (x, y)
# width x, and height h

    def __init__(self, x, y, w, h):
    self._x = float(x)
    self._y = float(y)
    self._width = float(w)
    self._height = float(h)
    # Extended four instance variables
    self._x0 = self._x - self._width / 2
    self._x1 = self._x + self._width / 2
    self._y0 = self._y - self._height / 2
    self._y1 = self._y + self._height/2
    # True if self intersects other; False otherwise
    def intersects(self, other):

        # find which rectangle is on the left
        leftRec = None
        rightRec = None
        if self._x1 >= other._x1:
            leftRec = other
            rightRec = self
        else:
            leftRec = self
            rightRec = other

        # find which rectangle is on the top
        topRec = None
        lowRec = None
        if self._y1 >= other._y1:
            topRec = self
            lowRec = other
        else:
            topRec = other
            lowRec = self

        if (leftRec._x0 + leftRec._width <= rightRec._x0) or (lowRec._y0 + lowRec._height <= topRec._y0):
            # Not overlap
            return False
        elif (leftRec._x0 + leftRec._width <= rightRec._x0 + rightRec._width) or (lowRec._y0 + lowRec._height <= topRec._y0 + topRec._height):
            # full overlap, contains
            return False
        else:
            # intersect
            return True

基本上,如果左矩形的左下x值加上它的宽度小于右矩形的左下x值,则它是不重叠的。如果左矩形的左下x值加上其宽度小于或等于右矩形的左下x值加上其宽度,那么右将与左完全重叠。除此之外,是交叉路口。比较上下两者,然后组合在一起,就可以找到交点。

答案 4 :(得分:0)

在编写代码之前,您可以执行以下操作: 1.考虑两个矩形不重叠的情况 2.首先选择每个颜色编码的x和y的对比较。 例如,比较rectangle.A.X1和比较Rectangle.B.X2

这是代码

package com.sample

public class Error{
...
}

enter image description here