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 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_from_coordinates(self): coordinates = [2, 1, 0, 1, 0, 0, 2, 0] coordinates2 = [-3, -2, -5, -2, -5, -3, -3, -3] coordinates3 = [1, 1, 0, 1, 0, 0, 1, 0] self.assertEqual( ''.join(map(str, TwoDPoint.from_coordinates(coordinates))), '(2, 1)(0, 1)(0, 0)(2, 0)') self.assertEqual( ''.join(map(str, TwoDPoint.from_coordinates(coordinates2))), '(-3, -2)(-5, -2)(-5, -3)(-3, -3)') self.assertEqual( ''.join(map(str, TwoDPoint.from_coordinates(coordinates3))), '(1, 1)(0, 1)(0, 0)(1, 0)') self.assertEqual(''.join(map(str, TwoDPoint.from_coordinates([]))), '') with self.assertRaises(Exception) as error: TwoDPoint.from_coordinates([0]) # self.assertFalse('[0] is invalid' in str(error.exception)) with self.assertRaises(Exception) as error: TwoDPoint.from_coordinates([-0.0, 1.0, 2.0]) # self.assertFalse('[-0.0, 1.0, 2.0] is invalid' in str(error.exception)) self.assertEqual(str(TwoDPoint(0, 1)), '(0, 1)') self.assertTrue(TwoDPoint(0, 1) == TwoDPoint(0, 1)) self.assertTrue(TwoDPoint(0, 1).__ne__(TwoDPoint(1, 2))) self.assertEqual(str(TwoDPoint(0, 1) + TwoDPoint(1, 2)), '(1, 3)') self.assertEqual(str(TwoDPoint(0, 1) - TwoDPoint(1, 2)), '(-1, -1)')
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_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 __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 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 __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 __init__(self, *floats): points = TwoDPoint.from_coordinates(list(floats)) self.__vertices = tuple(points[0:4])
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 __init__(self, *floats): if len(floats) != 8: raise Exception("A Quadrilateral must have 4 vertices!") points = TwoDPoint.from_coordinates(list(floats)) self.__vertices = tuple(points[0:4])
def test_from_coordinates_decimal_odd(self): # Odd values (decimal) with self.assertRaises(Exception): TwoDPoint.from_coordinates([1.4, 1.9, 0.5, -0.5, 3.0])
def test_from_coordinates_zero(self): # zero self.assertEqual([], TwoDPoint.from_coordinates([]))
def test_from_coordinates_even(self): # Even values self.assertEqual([TwoDPoint(1, 1), TwoDPoint(0, 0)], TwoDPoint.from_coordinates([1, 1, 0, 0]))
def __init__(self, *floats) -> None: # added -> NONE points = TwoDPoint.from_coordinates(list(floats)) self.__vertices = tuple(points[0:4]) # bug fixed from 0:3 to 0:4
def test_from_coordinates(self): self.assertEqual( TwoDPoint.from_coordinates(self.coordinates_even), [TwoDPoint(1, -2.8), TwoDPoint(3, 4.6)]) with self.assertRaises(Exception): TwoDPoint.from_coordinates(self.coordinates_odd)
def __init__(self, *floats) -> None: points = TwoDPoint.from_coordinates(list(floats)) self.__vertices = tuple(points[0:4]) if not self.__is_member(): raise TypeError("Not a quadrilateral")