예제 #1
0
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))
예제 #2
0
파일: scene.py 프로젝트: lordmauve/wasabi2d
 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
예제 #3
0
def test_rsub_int():
    """It is a TypeError to subtract a vector and an int."""
    with raises(TypeError):
        1 - vec2(0, 0)
예제 #4
0
파일: scene.py 프로젝트: lordmauve/wasabi2d
 def pos(self, v):
     self._pos[:] = vec2(v)
     self._xform[-1, :2] = self._cam_offset - self._pos
예제 #5
0
파일: scene.py 프로젝트: lordmauve/wasabi2d
 def center(self, v):
     x, y = vec2(*v) - 0.5 * self.dims
     self.x = x
     self.y = y
예제 #6
0
파일: scene.py 프로젝트: lordmauve/wasabi2d
 def dims(self) -> vec2:
     """Get the size of the window."""
     return vec2(self.width, self.height)
예제 #7
0
def test_rsub():
    """We can subtract with a tuple on the left hand side."""
    assert (1, 0) - vec2(0, 1) == vec2(1, -1)
예제 #8
0
def test_radd_tuple():
    """We can add with a tuple on the left hand side."""
    assert (1, 0) + vec2(0, 1) == vec2(1, 1)
예제 #9
0
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)
예제 #10
0
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)
예제 #11
0
def test_negate():
    """We can negate a vector."""
    assert -vec2(1, -2) == vec2(-1, 2)
예제 #12
0
def test_rdiv():
    """We can divide by a vec2."""
    assert 8 / vec2(2, 4) == vec2(4, 2)
예제 #13
0
def test_div():
    """We can divide a vec2."""
    assert vec2(10, 20) / 2 == vec2(5, 10)
예제 #14
0
def test_add_none_tuple():
    """It is an error to vectors that don't contain numbers."""
    with raises(TypeError):
        vec2(0, 0) + (None, None)
예제 #15
0
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) + ()
예제 #16
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))
예제 #17
0
def test_add_tuple():
    """We can add with a tuple on the right hand side."""
    assert vec2(0, 1) + (1, 0) == vec2(1, 1)
예제 #18
0
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
예제 #19
0
def test_sub_tuple():
    """We can subtract with a tuple on the right hand side."""
    assert vec2(1, 0) - (0, 1) == vec2(1, -1)
예제 #20
0
def test_repr(x, y):
    """Construct a vec and display its repr."""
    a = vec2(x, y)
    assert eval(repr(a)) == a
예제 #21
0
def test_mul_float():
    """We can multiply a vector by a float."""
    assert vec2(10.0, 5.0) * 2.0 == vec2(20, 10)
예제 #22
0
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)
예제 #23
0
파일: scene.py 프로젝트: lordmauve/wasabi2d
 def center(self):
     return vec2(self.x, self.y) + 0.5 * self.dims
예제 #24
0
def test_len():
    """The length of a vector is 2."""
    assert len(vec2(0, 0)) == 2
예제 #25
0
파일: scene.py 프로젝트: lordmauve/wasabi2d
 def dims(self):
     return vec2(self._width, self._height)
예제 #26
0
def test_iter():
    """We can unpack a vector."""
    x, y = vec2(1, 2)
    assert x, y == (1, 2)
예제 #27
0
파일: scene.py 프로젝트: lordmauve/wasabi2d
 def pos(self):
     return vec2(*self._pos)
예제 #28
0
def test_length(x, y):
    """We can get the length of a vector."""
    vec = vec2(x, y)
    assert vec.length() == approx(hypot(x, y))
예제 #29
0
 def pos(self):
     return vec2(*self.__xfmat[2][:2])
예제 #30
0
def test_add_notimplemented():
    """Adding to an unsupported type returns NotImplemented."""
    assert vec2(0, 0).__add__(None) == NotImplemented