Ejemplo n.º 1
0
def _matrix_loaded():
    try:
        x = a5.Matrix(1.0, 0.0, 0.0, 1.0)
        return True
    except:
        pass
    return False
Ejemplo n.º 2
0
def test_matrix_constructor():
    """Tests the constructor for matrices"""
    #Test default values for matrices:
    m = a5.Matrix()
    cunittest.assert_floats_equal(1.0, m.a)
    cunittest.assert_floats_equal(0.0, m.b)
    cunittest.assert_floats_equal(0.0, m.c)
    cunittest.assert_floats_equal(1.0, m.d)

    #Test matrix constructor with float arguments:
    m = a5.Matrix(1.23, 2.34, 3.45, 4.56)
    cunittest.assert_floats_equal(1.23, m.a)
    cunittest.assert_floats_equal(2.34, m.b)
    cunittest.assert_floats_equal(3.45, m.c)
    cunittest.assert_floats_equal(4.56, m.d)

    #Test matrix constructor with int arguments
    # (ints should be converted to floats)
    m = a5.Matrix(1, 2, 3, 4)
    cunittest.assert_floats_equal(1.0, m.a)
    cunittest.assert_floats_equal(2.0, m.b)
    cunittest.assert_floats_equal(3.0, m.c)
    cunittest.assert_floats_equal(4.0, m.d)
Ejemplo n.º 3
0
def test_matrix_vector_multiplication():
    """Tests multiplication between matrices and vectors"""
    #Test x-component of resulting vector
    m = a5.Matrix(5, 7, 0, 0)
    v = a5.Vector(3, 2)
    result = m * v
    cunittest.assert_floats_equal(29.0, result.x)
    cunittest.assert_floats_equal(0.0, result.y)

    #Test y-component of resulting vector:
    m = a5.Matrix(0, 0, 2, 3)
    v = a5.Vector(1, 1)
    result = m * v
    cunittest.assert_floats_equal(0.0, result.x)
    cunittest.assert_floats_equal(5.0, result.y)

    #Identity Matrix Test Case:
    m = a5.Matrix(1, 0, 0, 1)
    v = a5.Vector(426.317, 9921.33)
    result = m * v
    cunittest.assert_floats_equal(426.317, result.x)
    cunittest.assert_floats_equal(9921.33, result.y)

    #General Test Case:
    m = a5.Matrix(0, 1, 2, 3)
    v = a5.Vector(1, 0)
    result = m * v
    cunittest.assert_floats_equal(0.0, result.x)
    cunittest.assert_floats_equal(2.0, result.y)

    #Identity Matrix Test Case:
    m = a5.Matrix(1, 0, 0, 1)
    v = a5.Vector(426.317, 9921.33)
    result = m * v
    cunittest.assert_floats_equal(426.317, result.x)
    cunittest.assert_floats_equal(9921.33, result.y)
Ejemplo n.º 4
0
    def ShearY(cls, k):
        """**Constructor** (alternate): Returns a Shear transform along the y-axis.

            :param k: the shear amount
            **Precondition**: a number (float or int).

        The matrix is a shear Matrix, while the vector component is None. The
        method ``transform`` will use the default value of translation.
        
        Should this constructor fail (because of an incomplement implementation
        in module ``a5``, it will return an Identity transform."""
        try:
            matrix = a5.Matrix(1, 0, k, 1)
            self = cls(matrix)
            return self
        except:
            pass

        return cls.Identity()
Ejemplo n.º 5
0
    def Rotation(cls, angle):
        """**Constructor** (alternate): Returns a Rotation transform for the given angle.

            :param angle: the angle of rotation
            **Precondition**: a number (float or int).
        
        The matrix is a rotation Matrix, while the vector component is None. The
        method ``transform`` will use the default value of translation.
        
        Should this constructor fail (because of an incomplement implementation
        in module ``a5``, it will return an Identity transform."""
        try:
            matrix = a5.Matrix(math.cos(angle), -math.sin(angle),
                               math.sin(angle), math.cos(angle))
            self = cls(matrix)
            return self
        except:
            pass

        return cls.Identity()
Ejemplo n.º 6
0
    def Scale(cls, sx, sy=None):
        """**Constructor** (alternate): Returns a Scale transform for the given magnification.

            :param x: the magnification in the x direction
            **Precondition**: a number (float or int).

            :param y: the magnification in the y direction
            **Precondition**: a number (float or int).
        
        The matrix is a scaling Matrix, while the vector component is None. The
        method ``transform`` will use the default value of translation.
        
        Should this constructor fail (because of an incomplement implementation
        in module ``a5``, it will return an Identity transform."""
        try:
            sy = sx if sy is None else sy
            matrix = a5.Matrix(sx, 0, 0, sy)
            self = cls(matrix)
            return self
        except:
            pass

        return cls.Identity()
Ejemplo n.º 7
0
    def Projection(cls, x=1, y=0):
        """**Constructor** (alternate): Returns a Projection transform on to the given vector.

            :param x: the x coordinate of the vector to project on
            **Precondition**: a number (float or int).

            :param y: the y coordinate of the vector to project on
            **Precondition**: a number (float or int).
        
        The matrix is a projection Matrix, while the vector component is None. The
        method ``transform`` will use the default value of translation.
        
        Should this constructor fail (because of an incomplement implementation
        in module ``a5``, it will return an Identity transform."""
        try:
            norm = x * x + y * y  # dot product
            matrix = a5.Matrix(x * x / norm, x * y / norm, x * y / norm,
                               y * y / norm)
            self = cls(matrix)
            return self
        except:
            pass

        return cls.Identity()
Ejemplo n.º 8
0
def test_matrix_matrix_multiplication():
    """Tests multiplication between matrices"""
    #Test element a of the resulting matrix:
    m1 = a5.Matrix(3, 5, 0, 0)
    m2 = a5.Matrix(1, 0, 1, 0)
    result = m1 * m2
    cunittest.assert_floats_equal(8.0, result.a)
    cunittest.assert_floats_equal(0.0, result.b)
    cunittest.assert_floats_equal(0.0, result.c)
    cunittest.assert_floats_equal(0.0, result.d)

    #Test element b of the resulting matrix:
    m1 = a5.Matrix(3, 5, 0, 0)
    m2 = a5.Matrix(0, 2, 0, 7)
    result = m1 * m2
    cunittest.assert_floats_equal(0.0, result.a)
    cunittest.assert_floats_equal(41.0, result.b)
    cunittest.assert_floats_equal(0.0, result.c)
    cunittest.assert_floats_equal(0.0, result.d)

    #Test element c of the resulting matrix:
    m1 = a5.Matrix(0, 0, 1, 7)
    m2 = a5.Matrix(3, 0, 2, 0)
    result = m1 * m2
    cunittest.assert_floats_equal(0.0, result.a)
    cunittest.assert_floats_equal(0.0, result.b)
    cunittest.assert_floats_equal(17.0, result.c)
    cunittest.assert_floats_equal(0.0, result.d)

    #Test element d of the resulting matrix:
    m1 = a5.Matrix(0, 0, 2, 3)
    m2 = a5.Matrix(0, 9, 0, 1)
    result = m1 * m2
    cunittest.assert_floats_equal(0.0, result.a)
    cunittest.assert_floats_equal(0.0, result.b)
    cunittest.assert_floats_equal(0.0, result.c)
    cunittest.assert_floats_equal(21.0, result.d)

    #General Test Case:
    m1 = a5.Matrix(1, 2, 3, 4)
    m2 = a5.Matrix(4, 3, 2, 1)
    result = m1 * m2
    cunittest.assert_floats_equal(8.0, result.a)
    cunittest.assert_floats_equal(5.0, result.b)
    cunittest.assert_floats_equal(20.0, result.c)
    cunittest.assert_floats_equal(13.0, result.d)

    #Identity Matrix Test Case:
    m1 = a5.Matrix(1, 0, 0, 1)
    m2 = a5.Matrix(1, 4, 9, 16)
    result = m1 * m2
    cunittest.assert_floats_equal(1.0, result.a)
    cunittest.assert_floats_equal(4.0, result.b)
    cunittest.assert_floats_equal(9.0, result.c)
    cunittest.assert_floats_equal(16.0, result.d)