检查两个矩形是否重叠

时间:2016-06-24 09:28:48

标签: python python-3.x

我有一些测试,并编写了代码来检查代码是否有效。我有两个问题。

  1. 我不确定为什么我的两个测试用于检查Point是否包含"包含"在一个矩形内失败?
  2. 其次,我已经写了一些测试,以及两个矩形是否接触(或碰撞)的代码。我没有什么可以检查这些。我不确定 - > test(r.collision(Rectangle(Point(9,4),10,5))) - >没有通过测试。
  3. 最后,对于" collision()和collides()代码。我不确定他们是否正确?
  4. 抱歉有大量的代码。

    import sys
    
    
    def test(did_pass):
        """ Print the rests of the test """
        linenum = sys._getframe(1).f_lineno  # gets the callers line number
        if did_pass:
            msg = "Test at line {} is ok.".format(linenum)
        else:
            msg = "Test at line {} FAILED.".format(linenum)
        print(msg)
    
    
    class Point:
        """ Point class represents and manipulates x, y coordinates. """
    
        def __init__(self, x=0, y=0):
            """ Create a new point at x, y. """
            self.x = x
            self.y = y
    
        def __str__(self):
            """ Converting the point to a string. """
            return "({0}, {1})".format(self.x, self.y)
    
    
    class Rectangle:
        """ A classes to manufacture rectangle objects """
    
        def __init__(self, posn, w, h):
            """ Initialise the rectange at posn, with width w, height h """
            self.corner = posn
            self.width = w
            self.height = h
    
        def __str__(self):
            return "({0}, {1}, {2})".format(self.corner, self.width, self.height)
    
    
        def contains(self, point):
            """ Test if a point falls with in the rectangle """
            outer_x = self.corner.x + self.width
            outer_y = self.corner.y + self.height
            return (self.corner.x <= point.x < outer_x and
                    self.corner.y <= point.y < outer_y)
    
        def collision(self, other):
            """ Test if another rectangle collides with the first rectangle. """
            outer_x = self.corner.x + self.width
            outer_y = self.corner.y + self.height
            if (self.corner.x <= other.corner.x <= outer_x and
                self.corner.y <= other.corner.y <= outer_y):
                return True
            if (self.corner.x <= other.corner.x + other.width <= outer_x and
                self.corner.y <= other.corner.y + other.height <= outer_y):
                return True
            else:
                return False
    
    
    def test_suite():
        r = Rectangle(Point(0, 0), 10, 5)
    
        test(r.contains(Point(0, 0)))
        test(r.contains(Point(3, 3)))
        test(not r.contains(Point(3, 7)))  # This fails. I'm not sure why?
        test(not r.contains(Point(3, 5)))  # This also fail. I'm not sure why?
        test(r.contains(Point(3, 4.99999)))
        test(not r.contains(Point(-3, -3)))
    
        # Testing if collision with the point of the Other Rectangle
        test(r.collision(Rectangle(Point(0, 0), 10, 5)))
        test(r.collision(Rectangle(Point(5, 0), 10, 5)))
        test(not r.collision(Rectangle(Point(10, 5), 10, 5)))
        test(r.collision(Rectangle(Point(9, 4), 10, 5)))  # This Code fails, I'm not sure why?
        test(not r.collision(Rectangle(Point(20, 5), 10, 5)))
    
        # Testing if collision with the top right corner of Other Rectangle
        test(not r.collision(Rectangle(Point(-11, 5), 10, 5)))
        test(r.collision(Rectangle(Point(0, 0), 10, 5)))
        test(not r.collision(Rectangle(Point(0, -6), 10, 5)))
        test(r.collision(Rectangle(Point(-9, -4), 10, 5)))
    
    
    test_suite()
    

1 个答案:

答案 0 :(得分:0)

错误的不平等

为什么你的包含函数仅对外点使用严格的不等式?

self.corner.x <= point.x < outer_x

您应该决定&#34;的定义是否包含&#34;严格意味着在内部,或者在边缘上精确计算任何东西。可以使用<=两次,也可以使用<

错误的测试

我们假设您决定使用<=

然后这包含断言是错误的,你应该删除not

test(not r.contains(Point(3, 5)))

该点位于矩形的边缘,因此它应计为包含在矩形中:

这个断言也是错误的:

test(r.collision(Rectangle(Point(10, 5), 10, 5)))

两个矩形在坐标(10,5)处相互接触,因此您应该移除not

如果您计划使用<来使用&#34; contains&#34;的严格版本,那么您的第一个断言是错误的,因为该点仅位于矩形的边缘:

test(r.contains(Point(0, 0)))

错误的碰撞逻辑

最后,你对矩形的逻辑是简单的,也是错误的。太糟糕了,你没有写足够的测试来找到它!

您只是检查两个(upper_left,lower_right)角点是否在您的矩形内。矩形碰撞并不那么简单。也许传入参数的矩形更大,包含你的矩形!在那种情况下,你无法检测到碰撞。

我可以向您推荐另一个SO答案,其中有关于如何找到矩形交叉点的插图:

https://stackoverflow.com/a/25068722/4552193