コード例 #1
0
    def test_negation(self):
        """Test that we can negate a vector"""

        a1 = tuples.Tuple(["a", "b", "c", "d"], 1, -2, 3, -4)

        a2 = -a1

        self.assertEqual(a2, tuples.Tuple(["a", "b", "c", "d"], -1, 2, -3, 4))
コード例 #2
0
    def test_scalar_division(self):
        """Test that we can divide a tuple by a scalar"""

        a1 = tuples.Tuple(["a", "b", "c", "d"], 1, -2, 3, -4)

        a2 = a1 / 2

        self.assertEqual(a2,
                         tuples.Tuple(["a", "b", "c", "d"], 0.5, -1, 1.5, -2))
コード例 #3
0
    def test_scalar_multiplication(self):
        """Test that we can multiple a vector by a scalar"""

        a1 = tuples.Tuple(["a", "b", "c", "d"], 1, -2, 3, -4)
        a2 = a1 * 3.5
        a3 = a1 * 0.5

        self.assertEqual(
            a2, tuples.Tuple(["a", "b", "c", "d"], 3.5, -7, 10.5, -14))
        self.assertEqual(a3,
                         tuples.Tuple(["a", "b", "c", "d"], 0.5, -1, 1.5, -2))
コード例 #4
0
    def test_matrix_tuple_multiplication(self):
        """Test that we can multiple a matrix by a tuple"""

        M = matrices.Matrix(4, 4)
        M.set_row(0, [1, 2, 3, 4])
        M.set_row(1, [2, 4, 4, 2])
        M.set_row(2, [8, 6, 4, 1])
        M.set_row(3, [0, 0, 0, 1])

        t = tuples.Tuple(["x", "y", "z", "w"], 1, 2, 3, 1)

        t2 = M * t

        self.assertEqual(t2, tuples.Tuple(["x", "y", "z", "w"], 18, 24, 33, 1))
コード例 #5
0
    def __mul__(self, other):
        """Perform matrix multiplication"""

        if isinstance(other, Matrix):
            if self.rows != other.rows or self.columns != other.columns:
                raise exceptions.IncompatibleLengthError

            M = Matrix(self.rows, self.columns)

            for i in range(self.rows):
                for j in range(self.columns):
                    my_row = self.get_row(i)
                    other_column = other.get_col(j)

                    M.set(i, j,
                          sum([x * y for x, y in zip(my_row, other_column)]))
            return M

        if isinstance(other, tuples.Tuple):

            zeros = [0] * len(other.fillables)
            r = tuples.Tuple(other.fillables, *zeros)

            for i in range(self.rows):
                my_row = self.get_row(i)
                result = sum([
                    my_row[j] * other.values()[j] for j in range(self.columns)
                ])
                setattr(r, other.fillables[i], result)
            return r

        # If we get down here it wasn't any type we can multiply with
        raise ValueError
コード例 #6
0
    def test_point__tuple(self):
        """Test that we can initialize and read from a point as a tuple"""

        p = tuples.Tuple(["x", "y", "z", "w"], 4.3, -4.2, 3.1, 1)

        self.assertEqual(p.x, 4.3)
        self.assertEqual(p.y, -4.2)
        self.assertEqual(p.z, 3.1)
        self.assertEqual(p.w, 1)
コード例 #7
0
ファイル: test_vectors.py プロジェクト: craigmbooth/raytracer
    def test_vector__tuple(self):
        """Test that we can initialize and read from a vector as a tuple"""

        v = tuples.Tuple(["x", "y", "z", "w"], 4.3, -4.2, 3.1, 0)

        self.assertEqual(v.x, 4.3)
        self.assertEqual(v.y, -4.2)
        self.assertEqual(v.z, 3.1)
        self.assertEqual(v.w, 0)
コード例 #8
0
    def test_addition(self):
        """test that we can add a vector to a point"""

        a1 = points.Point(3, -2, 5)
        a2 = vectors.Vector(-2, 3, 1)

        a3 = a1 + a2

        self.assertEqual(a3, tuples.Tuple(["x", "y", "z", "w"], 1, 1, 6, 1))
        self.assertEqual(a3, points.Point(1, 1, 6))
コード例 #9
0
    def test_identity_matrix_mult(self):
        """Test we can identify the identity matrix by a matrix and tuple"""

        ident = transforms.Identity(3)

        M = matrices.Matrix(3, 3)
        M.set_row(0, [1, 2, 3])
        M.set_row(1, [3, 2, 1])
        M.set_row(2, [2, 4, 6])

        M2 = M * ident
        self.assertEqual(M2, M)

        t = tuples.Tuple(["a", "b", "c"], 1, 2, 3)
        t2 = ident * t
        self.assertEqual(t2, t)