def snap(self): """Snaps the sides of the square such that each corner (x,y) is modified to be a corner (x',y') where x' is the integer value closest to x and y' is the integer value closest to y. This, of course, may change the shape to a general quadrilateral, hence the return type. The only exception is when the square is positioned in a way where this approximation will lead it to vanish into a single point. In that case, a call to snap() will not modify this square in any way.""" newPoint1 = TwoDPoint(Square.regular_round(self.vertices[0].x), Square.regular_round(self.vertices[0].y)) newPoint2 = TwoDPoint(Square.regular_round(self.vertices[1].x), Square.regular_round(self.vertices[1].y)) newPoint3 = TwoDPoint(Square.regular_round(self.vertices[2].x), Square.regular_round(self.vertices[2].y)) newPoint4 = TwoDPoint(Square.regular_round(self.vertices[3].x), Square.regular_round(self.vertices[3].y)) if (newPoint1 == newPoint2 or newPoint1 == newPoint3 or newPoint1 == newPoint4 or newPoint2 == newPoint3 or newPoint2 == newPoint4 or newPoint3 == newPoint4): return self return Quadrilateral(Square.regular_round(self.vertices[0].x), Square.regular_round(self.vertices[0].y), Square.regular_round(self.vertices[1].x), Square.regular_round(self.vertices[1].y), Square.regular_round(self.vertices[2].x), Square.regular_round(self.vertices[2].y), Square.regular_round(self.vertices[3].x), Square.regular_round(self.vertices[3].y)) # TODO
def setUp(self) -> None: self.point1 = TwoDPoint(0, 0) self.point2 = TwoDPoint(1, 2) self.point3 = TwoDPoint(5, 0) self.point4 = TwoDPoint(0, 0) self.coordinates_even = [1, -2.8, 3, 4.6] self.coordinates_odd = [-1, 2, -3.7, 4, 5]
def test_from_coordinates_decimal_even(self): # Even values (decimal) self.assertEqual( [TwoDPoint(1.4, 1.9), TwoDPoint(0.5, -0.5), TwoDPoint(3.0, -1)], TwoDPoint.from_coordinates([1.4, 1.9, 0.5, -0.5, 3.0, -1]))
def test_from_coordinates(self): a = TwoDPoint(1, 2) b = TwoDPoint(1, 3) c = TwoDPoint(0, -1) d = TwoDPoint(5, 6.5) l = [a, b, c, d] l2 = [1, 2, 1, 3, 0, -1, 5, 6.5] self.assertEqual(l, TwoDPoint.from_coordinates(l2))
def test_from_coordinates(self): list_of_points = [ TwoDPoint(0, 4), TwoDPoint(6, 4), TwoDPoint(6, 0), TwoDPoint(0, 0) ] self.assertEqual(TwoDPoint.from_coordinates([0, 4, 6, 4, 6, 0, 0, 0]), list_of_points) # TODO
def test_center(self): r1 = Rectangle(2, 1, 0, 1, 0, 0, 2, 0) self.assertEqual(str(r1.center()), str(TwoDPoint(1.0, 0.5))) with self.assertRaises(TypeError) as error: r2 = Rectangle(2, 4, -2, 4, -5, 0, 5, 0) # Trapezoid r3 = Rectangle(1, 1, 0, 1, 0, 0, 1, 0) # Square self.assertEqual(str(r3.center()), str(TwoDPoint(0.5, 0.5))) with self.assertRaises(TypeError) as error: r4 = Rectangle(1, 1, 0, 1, 0, 0, 1, 0, 5, 6) # Too many arguments
def test_center(self): print("Testing center method") A = Rectangle(0, 0, -3.5, 0.0, -3.5, -7.8, 0, -7.8) B = Rectangle(0, 0.0, -5, 0.0, -5, -5, 0, -5) aCenter = TwoDPoint(-1.75, -3.9) bCenter = TwoDPoint(-2.5, -2.5) self.assertEqual(aCenter, A.center()) self.assertEqual(bCenter, B.center()) self.assertNotEqual(A.center(),TwoDPoint(0,0)) print("Done testing center method successfully")
class TestTwoDPoint(TestCase): def setUp(self) -> None: self.p1 = TwoDPoint(1, 2) self.p2 = TwoDPoint(1, 2) self.p3 = TwoDPoint(3, 2) def test_from_coordinates(self): list_of_points = [ TwoDPoint(0, 4), TwoDPoint(6, 4), TwoDPoint(6, 0), TwoDPoint(0, 0) ] self.assertEqual(TwoDPoint.from_coordinates([0, 4, 6, 4, 6, 0, 0, 0]), list_of_points) # TODO def test_eq(self): self.assertTrue(self.p1.__eq__(self.p2)) self.assertFalse(self.p1.__eq__(self.p3)) self.assertFalse(self.p3.__eq__(self.p2)) def test_ne(self): self.assertTrue(self.p1.__ne__(self.p3)) self.assertTrue(self.p2.__ne__(self.p3)) self.assertFalse(self.p1.__ne__(self.p2)) def test_str(self): self.assertEqual(self.p1.__str__(), "(1, 2)") def test_add(self): self.assertEqual(self.p1.__add__(self.p3), TwoDPoint(4, 4)) def test_sub(self): self.assertEqual(self.p3.__sub__(self.p1), TwoDPoint(2, 0))
def __init__(self, *floats): if isinstance(floats[0], list): points = TwoDPoint.from_coordinates(floats[0]) else: points = TwoDPoint.from_coordinates(list(floats)) if len(points) != 4: raise TypeError("Too many or too few points") self.__vertices = tuple(points[0:4]) if not self.__is_member(): raise TypeError( "A quadrilateral cannot be formed from given coordinates")
def center(self): """Returns the center of this rectangle, calculated to be the point of intersection of its diagonals.""" v = self.vertices x, y = 0, 0 for pt in v: x += pt.x y += pt.y if TwoDPoint.midpoint(v[0], v[2]) == TwoDPoint.midpoint(v[1], v[3]): return TwoDPoint.midpoint(v[0], v[2]) else: return TwoDPoint(x/4, y/4)
def __init__(self, *floats): points = TwoDPoint.from_coordinates(list(floats)) if len(points) < 4: raise Exception self.__vertices = tuple(points[0:4]) if not self.__is_member(): raise Exception
def center(self): newX = self.vertices[0].x + self.vertices[1].x + self.vertices[ 2].x + self.vertices[3].x newY = self.vertices[0].y + self.vertices[1].y + self.vertices[ 2].y + self.vertices[3].y return TwoDPoint(newX / 4.0, newY / 4.0) # TODO
def test__is_member(self): print("Testing is_member method") B = Rectangle(4, 2, -4, 2, -4, 0, 4, 0) self.assertTrue((B._Rectangle__is_member())) B._Quadrilateral__vertices = (TwoDPoint(0,0),TwoDPoint(0,0),TwoDPoint(0,0),TwoDPoint(0,0)) self.assertFalse((B._Rectangle__is_member())) B._Quadrilateral__vertices = (TwoDPoint(0,0),TwoDPoint(-3.5,0),TwoDPoint(-3.5,-7.8),TwoDPoint(0,-7.8)) self.assertTrue((B._Rectangle__is_member())) print("Done testing is_member method successfully")
def snap(self) -> 'TwoDPoint': """Snaps the sides of the square such that each corner (x,y) is modified to be a corner (x',y') where x' is the integer value closest to x and y' is the integer value closest to y. This, of course, may change the shape to a general quadrilateral, hence the return type. The only exception is when the square is positioned in a way where this approximation will lead it to vanish into a single point. In that case, a call to snap() will not modify this square in any way.""" vertices = self.vertices point0 = TwoDPoint(int(round(vertices[0].x)), int(round(vertices[0].y))) point1 = TwoDPoint(int(round(vertices[1].x)), int(round(vertices[1].y))) if point0 == point1: return self return Quadrilateral(int(round(vertices[0].x)), int(round(vertices[0].y)), int(round(vertices[1].x)), int(round(vertices[1].y)), int(round(vertices[2].x)), int(round(vertices[2].y)), int(round(vertices[3].x)), int(round(vertices[3].y))) # TODO
def test_from_coordinates(self): print("Testing _from_coordinates method") c = TwoDPoint.from_coordinates([0, 0, -3.5, 0.0]) d = [TwoDPoint(0, 0), TwoDPoint(-3.5, 0.0)] e = [TwoDPoint(0, 0), TwoDPoint(-3.5, 3.5)] self.assertEqual(c, d) self.assertNotEqual(d, e) with self.assertRaises(Exception) as assertStatement: TwoDPoint.from_coordinates([1, 1, 1]) self.assertEqual( "Odd number of floats given to build a list of 2-d points", str(assertStatement.exception)) print("Done testing _from_coordinates successfully")
def test___ne__(self): print("Testing __ne__ method") twoD1 = TwoDPoint(0, 0) twoD2 = TwoDPoint(0, 0) twoD3 = TwoDPoint(1, 1) self.assertFalse(twoD1 != twoD2) self.assertTrue(twoD1 != twoD3) self.assertFalse(twoD1.__ne__(twoD2)) self.assertTrue(twoD1.__ne__(twoD3)) print("Done testing __ne__ method successfully")
def __init__(self, *floats): if not (len(list(floats)) == 8): raise TypeError( "A {} cannot be formed by the given coordinates.".format( self.__class__.__name__)) points = TwoDPoint.from_coordinates(list(floats)) self.__vertices = tuple(points[0:4]) if ((self.vertices[0] == self.vertices[1]) or (self.vertices[0] == self.vertices[2]) or (self.vertices[0] == self.vertices[3]) or (self.vertices[1] == self.vertices[2]) or (self.vertices[1] == self.vertices[3]) or (self.vertices[2] == self.vertices[3])): raise TypeError( "A {} cannot be formed by the given coordinates.".format( self.__class__.__name__))
def test_from_coordinates( self): # Not sure if this is how it is supposed to be tested # TODO Expected = [] Expected.append(TwoDPoint(1.0, 1.0)) Expected.append(TwoDPoint(0.0, 1.0)) Expected.append(TwoDPoint(0.0, 0.0)) Expected.append(TwoDPoint(1.0, 0.0)) lst = [1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0] self.assertEqual(Expected, TwoDPoint.from_coordinates(lst)) Expected2 = [] Expected2.append(TwoDPoint(1.0, 1.0)) lst2 = [2.0, 2.0] self.assertFalse(Expected2 == TwoDPoint.from_coordinates( lst2)) # These are two points that are not equal to each other
def test__sub__(self): print("Testing __add__ method") twoD1 = TwoDPoint(1, 1) twoD2 = TwoDPoint(1, 0) twoD3 = TwoDPoint(0, 1) self.assertEqual(twoD1.__sub__(twoD2), twoD3) self.assertEqual(twoD1 - twoD2, twoD3) self.assertNotEqual(twoD1.__sub__(twoD3), twoD3) self.assertNotEqual(twoD1 - twoD1, twoD3) with self.assertRaises(ValueError) as assertStatement: twoD1 - "string" self.assertEqual("Subtraction is possibile only between TwoDPoint", str(assertStatement.exception)) print("Done testing __add__ method successfully")
def test_sub(self): self.assertEqual(self.p3.__sub__(self.p1), TwoDPoint(2, 0))
def test_add(self): self.assertEqual(self.p1.__add__(self.p3), TwoDPoint(4, 4))
def center(self) -> 'TwoDPoint': """Returns the center of this rectangle, calculated to be the point of intersection of its diagonals.""" return TwoDPoint((self.vertices[0].x + self.vertices[2].x) / 2, (self.vertices[0].y + self.vertices[2].y) / 2) # TODO
def test_str(self): self.assertEqual('(%g, %g)' % (1, 1), TwoDPoint.__str__(TwoDPoint(1, 1)))
def test_sub_false(self): self.assertFalse( TwoDPoint(9, 2) == (TwoDPoint(3, 7) - TwoDPoint(3, 5)))
def __init__(self, *floats): points = TwoDPoint.from_coordinates(list(floats)) self.__vertices = tuple(points[0:4])
def setUp(self) -> None: self.p1 = TwoDPoint(1, 2) self.p2 = TwoDPoint(1, 2) self.p3 = TwoDPoint(3, 2)
def __init__(self, *floats): points = TwoDPoint.from_coordinates(list(floats)) self.__vertices = tuple(points[0:4]) if not self.__is_member(): raise TypeError("A Quadrilateral cannot be formed by the given coordinates.")
def test_from_coordinates_odd(self): # Odd values with self.assertRaises(Exception): TwoDPoint.from_coordinates([1, 1, 0, 0, 3])
def center(self) -> TwoDPoint: """Returns the center of this rectangle, calculated to be the point of intersection of its diagonals.""" val = self.vertices return TwoDPoint(((val[0].x + val[2].x) / 2), ((val[0].x + val[2].x) / 2))
def test_sub_true(self): self.assertTrue(TwoDPoint(5, 2) == (TwoDPoint(8, 7) - TwoDPoint(3, 5)))