def test_multiply_matrix_by_tuple(self): a = Matrix4(1, 2, 3, 4, 2, 4, 4, 2, 8, 6, 4, 1, 0, 0, 0, 1) b = Tuple(1, 2, 3, 1) self.assertEqual(Tuple(18, 24, 33, 1), a * b)
def test_is_point(self): a = Tuple(4.3, -4.2, 3.1, 1.0) self.assertEqual(4.3, a.x) self.assertEqual(-4.2, a.y) self.assertEqual(3.1, a.z) self.assertEqual(1.0, a.w) self.assertTrue(a.is_point()) self.assertFalse(a.is_vector())
def __mul__(self, other): if type(other) == Matrix4: outcome = Matrix4() for row in range(4): for col in range(4): outcome[row, col] = self[row, 0] * other[0, col] + \ self[row, 1] * other[1, col] + \ self[row, 2] * other[2, col] + \ self[row, 3] * other[3, col] return outcome elif type(other) == Tuple: return Tuple( self[0, 0] * other.x + self[0, 1] * other.y + self[0, 2] * other.z + self[0, 3] * other.w, self[1, 0] * other.x + self[1, 1] * other.y + self[1, 2] * other.z + self[1, 3] * other.w, self[2, 0] * other.x + self[2, 1] * other.y + self[2, 2] * other.z + self[2, 3] * other.w, self[3, 0] * other.x + self[3, 1] * other.y + self[3, 2] * other.z + self[3, 3] * other.w)
def test_divide_by_scalar(self): a1 = Tuple(1, -2, 3, -4) self.assertEqual(Tuple(0.5, -1, 1.5, -2), a1 / 2)
def test_multiple_by_scalar(self): a1 = Tuple(1, -2, 3, -4) self.assertEqual(Tuple(3.5, -7, 10.5, -14), a1 * 3.5)
def test_negation_tuples(self): a1 = Tuple(1, -2, 3, -4) self.assertEqual(Tuple(-1, 2, -3, 4), -a1)
def test_add_two_tuples(self): a1 = Tuple(3, -2, 5, 1) a2 = Tuple(-2, 3, 1, 0) self.assertEqual(Tuple(1, 1, 6, 1), a1 + a2)
def test_vector_method(self): v = vector(4, -4, 3) self.assertEqual(Tuple(4, -4, 3, 0), v)
def test_point_method(self): p = point(4, -4, 3) self.assertEqual(Tuple(4, -4, 3, 1), p)
def multiply3x3matrix(self, other): return Tuple( self[0, 0] * other.x + self[0, 1] * other.y + self[0, 2] * other.z, self[1, 0] * other.x + self[1, 1] * other.y + self[1, 2] * other.z, self[2, 0] * other.x + self[2, 1] * other.y + self[2, 2] * other.z, 0)