Exemplo n.º 1
0
 def __init__(self, origin, direction):
     if not Tuple.is_point(origin):
         raise ValueError("Expected Point as first argument: {}".format(
             str(origin)))
     if not Tuple.is_vector(direction):
         raise ValueError("Expected Vector as second argument: {}".format(
             str(direction)))
     self.origin = origin
     self.direction = direction
Exemplo n.º 2
0
    def test_identity(self):
        m = Matrix(
            vals=[[0, 1, 2, 4], [1, 2, 4, 8], [2, 4, 8, 16], [4, 8, 16, 32]])
        i = Matrix.identity()
        self.assertEqual(1.0, i[0, 0])
        self.assertEqual(1.0, i[1, 1])
        self.assertEqual(1.0, i[2, 2])
        self.assertEqual(1.0, i[3, 3])
        self.assertEqual(m * i, m)

        t = Tuple(1, 2, 3, 4)
        self.assertEqual(i * t, t)
Exemplo n.º 3
0
 def __mul__(self, other):
     if isinstance(other, Matrix):
         a, b = self, other
         assert a.size == b.size
         m = self._create(a.size)
         for r in range(a.size):
             for c in range(a.size):
                 m[r][c] = sum(a[r, i] * b[i, c] for i in range(a.size))
         return Matrix(vals=m)
     if isinstance(other, Tuple):
         a, b = self, other
         assert a.size == len(b)
         t = [0.0] * a.size
         for r in range(a.size):
                 t[r] = sum(a[r, i] * b[i] for i in range(a.size))
         return Tuple(*t)
Exemplo n.º 4
0
 def test_tuple_multiplication(self):
     m = Matrix(
         vals=[[1, 2, 3, 4], [2, 4, 4, 2], [8, 6, 4, 1], [0, 0, 0, 1]])
     t = Tuple(1, 2, 3, 1)
     c = m * t
     self.assertEqual(c, Tuple(18, 24, 33, 1))
Exemplo n.º 5
0
 def test_scalar_division(self):
     a = Tuple(1, -2, 3, -4)
     self.assertEqual(a / 2.0, Tuple(0.5, -1, 1.5, -2))
Exemplo n.º 6
0
 def test_scalar_multiplication(self):
     a = Tuple(1, -2, 3, -4)
     self.assertEqual(a * 3.5, Tuple(3.5, -7, 10.5, -14))
     self.assertEqual(a * 0.5, Tuple(0.5, -1, 1.5, -2))
Exemplo n.º 7
0
 def test_negation(self):
     a = Tuple(1, -2, 3, -4)
     self.assertEqual(-a, Tuple(-1, 2, -3, 4))
Exemplo n.º 8
0
 def test_subtraction(self):
     t1 = Tuple(3, -2, 5, 1)
     t2 = Tuple(-2, 3, 1, 0)
     self.assertEqual(t1 - t2, Tuple(5, -5, 4, 1))
Exemplo n.º 9
0
 def test_addition(self):
     t1 = Tuple(3, -2, 5, 1)
     t2 = Tuple(-2, 3, 1, 0)
     self.assertEqual(t1 + t2, Tuple(1, 1, 6, 1))
Exemplo n.º 10
0
 def test_vector_tuple_equality(self):
     a = Vector(4, -4, 3)
     t = Tuple(4, -4, 3, 0)
     self.assertEqual(a, t)
Exemplo n.º 11
0
 def test_point_tuple_equality(self):
     p = Point(4, -4, 3)
     t = Tuple(4, -4, 3, 1)
     self.assertEqual(p, t)