I have a few tests, and have written the code to check to see if the code works. I have two issues.
- I am not sure why 2 of my tests for checking if a Point is "contained" within a rectangle fail?
- Secondly, I have written a few tests, and code for whether two rectangles touch (or collide). I have nothing to check these with. I am not sure --> test(r.collision(Rectangle(Point(9, 4), 10, 5))) --> fails the test.
- Finally, for the "collision() and collides() code. I am not completes sure if they are correct?
Sorry for the extensive amount of code.
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()