def test_inequality(self): a = Vector2(1, 2) b = Vector2(3, 2) c = Vector2(1, 4) self.assertTrue(a != b) self.assertTrue(a != c)
def test_rotate(self): a = Vector2(6, 2) self.assertEqual(a.rotate(R0), a) self.assertEqual(a.rotate(R90), Vector2(2, -6)) self.assertEqual(a.rotate(R180), Vector2(-6, -2)) self.assertEqual(a.rotate(R270), Vector2(-2, 6))
def __init__(self, src: Coordinate2, tgt: Coordinate2) -> None: if not isinstance(src, Coordinate2): raise TypeError() if not isinstance(tgt, Coordinate2): raise TypeError() direction = { Vector2(1, 0): RIGHT, Vector2(0, -1): DOWN, Vector2(-1, 0): LEFT, Vector2(0, 1): UP, }.get(tgt - src) if direction is None: raise ValueError("Source and target are not adjacent") self.src = src self.tgt = tgt self.direction = direction
def test_repeat_rotate(self): a = Vector2(5, 7) self.assertEqual( a.rotate(R90).rotate(R90).rotate(R90).rotate(R90), a, ) self.assertEqual( a.rotate(R270).rotate(R270).rotate(R270).rotate(R270), a, )
def test_subtract_from_vector(self): c = Coordinate2(1, 2) v = Vector2(3, 5) with self.assertRaises(TypeError): v - c
def test_subtract_vector(self): c = Coordinate2(1, 2) v = Vector2(3, 5) self.assertEqual(c - v, Coordinate2(-2, -3))
def test_subtract_coordinate(self): a = Coordinate2(1, 2) b = Coordinate2(3, 5) self.assertEqual(b - a, Vector2(2, 3))
def test_add_vector(self): a = Coordinate2(100, 50) b = Vector2(1, 1) self.assertEqual(a + b, Coordinate2(101, 51)) self.assertEqual(b + a, Coordinate2(101, 51))
def test_hash(self): a = Vector2(1, 2) b = Vector2(1, 2) self.assertTrue(hash(a) == hash(b))
def _turn_left(half_edge: _HalfEdge) -> _HalfEdge: return _HalfEdge( half_edge.tgt, half_edge.tgt + Vector2.unit_vector(half_edge.direction - R90), )
def test_equality(self): a = Vector2(1, 2) b = Vector2(1, 2) self.assertTrue(a == b)
def test_right_scalar_product(self): self.assertEqual(2 * Vector2(2, 3), Vector2(4, 6))
def _turn_ahead(half_edge: _HalfEdge) -> _HalfEdge: return _HalfEdge( half_edge.tgt, half_edge.tgt + Vector2.unit_vector(half_edge.direction), )
def test_left_scalar_product(self): self.assertEqual(Vector2(2, 3) * 2, Vector2(4, 6))
def test_dot_product(self): a = Vector2(2, 3) b = Vector2(4, 5) self.assertEqual(a * b, Vector2(8, 15))
def test_subtract_vector(self): a = Vector2(3, 5) b = Vector2(7, 11) self.assertEqual(b - a, Vector2(4, 6))
def test_add_vector(self): a = Vector2(1, 2) b = Vector2(3, 5) self.assertEqual(a + b, Vector2(4, 7))
def test_negate(self): self.assertEqual(-Vector2(1, 3), Vector2(-1, -3))