def test_normalized(x, y): """We can normalize a vector.""" vec = vec2(x, y) if vec.is_zero(): return r, theta = vec.to_polar() assert vec.normalized().to_polar() == approx((1.0, theta))
def __init__(self, ctx, width, height): self.ctx = ctx self._xform = np.identity(4, dtype='f4') self._cam_offset = np.zeros(2, dtype='f4') self._cam_vel = np.zeros(2, dtype='f4') self._pos = np.zeros(2, dtype='f4') self._fbs = {} self.resize(width, height) self.pos = vec2(self.width, self.height) * 0.5
def test_rsub_int(): """It is a TypeError to subtract a vector and an int.""" with raises(TypeError): 1 - vec2(0, 0)
def pos(self, v): self._pos[:] = vec2(v) self._xform[-1, :2] = self._cam_offset - self._pos
def center(self, v): x, y = vec2(*v) - 0.5 * self.dims self.x = x self.y = y
def dims(self) -> vec2: """Get the size of the window.""" return vec2(self.width, self.height)
def test_rsub(): """We can subtract with a tuple on the left hand side.""" assert (1, 0) - vec2(0, 1) == vec2(1, -1)
def test_radd_tuple(): """We can add with a tuple on the left hand side.""" assert (1, 0) + vec2(0, 1) == vec2(1, 1)
def test_construct_from_array(): """We can construct a vector by passing any sequence.""" from array import array a = array('d', [6.0, 5.0]) assert vec2(a) == vec2(6, 5)
def test_rotate(angle): """We can rotate a vector.""" r, theta = vec2(1, 1).rotated(angle).to_polar() assert theta % tau == approx((angle + tau / 8) % tau) assert r == approx(2**0.5)
def test_negate(): """We can negate a vector.""" assert -vec2(1, -2) == vec2(-1, 2)
def test_rdiv(): """We can divide by a vec2.""" assert 8 / vec2(2, 4) == vec2(4, 2)
def test_div(): """We can divide a vec2.""" assert vec2(10, 20) / 2 == vec2(5, 10)
def test_add_none_tuple(): """It is an error to vectors that don't contain numbers.""" with raises(TypeError): vec2(0, 0) + (None, None)
def test_add_invalid_tuple_length(): """It is an error to add a vector and a tuple of length != 2.""" with raises(TypeError): vec2(0, 0) + ()
def test_normalize(): """We can normalize a vector.""" roothalf = 0.5**0.5 assert tuple(vec2(-1.0, 1.0).normalized()) == approx((-roothalf, roothalf))
def test_add_tuple(): """We can add with a tuple on the right hand side.""" assert vec2(0, 1) + (1, 0) == vec2(1, 1)
def test_getattr(x, y): """We can get the x and y attributes.""" a = vec2(x, y) assert a.x == x assert a.y == y
def test_sub_tuple(): """We can subtract with a tuple on the right hand side.""" assert vec2(1, 0) - (0, 1) == vec2(1, -1)
def test_repr(x, y): """Construct a vec and display its repr.""" a = vec2(x, y) assert eval(repr(a)) == a
def test_mul_float(): """We can multiply a vector by a float.""" assert vec2(10.0, 5.0) * 2.0 == vec2(20, 10)
def test_add(ax, ay, bx, by): """We can add two vectors.""" if ax + bx in (inf, -inf) or ay + by in (inf, -inf): return assert vec2(ax, ay) + vec2(bx, by) == vec2(ax + bx, ay + by)
def center(self): return vec2(self.x, self.y) + 0.5 * self.dims
def test_len(): """The length of a vector is 2.""" assert len(vec2(0, 0)) == 2
def dims(self): return vec2(self._width, self._height)
def test_iter(): """We can unpack a vector.""" x, y = vec2(1, 2) assert x, y == (1, 2)
def pos(self): return vec2(*self._pos)
def test_length(x, y): """We can get the length of a vector.""" vec = vec2(x, y) assert vec.length() == approx(hypot(x, y))
def pos(self): return vec2(*self.__xfmat[2][:2])
def test_add_notimplemented(): """Adding to an unsupported type returns NotImplemented.""" assert vec2(0, 0).__add__(None) == NotImplemented