def ray_for_pixel(self, pixel_x, pixel_y): xoffset = (pixel_x + 0.5) * self.pixel_size yoffset = (pixel_y + 0.5) * self.pixel_size world_x = self.half_width - xoffset world_y = self.half_height - yoffset pixel = multiply_tuple(self.__inverse_transform, point(world_x, world_y, -1)) origin = multiply_tuple(self.__inverse_transform, point(0, 0, 0)) direction = normalize(subtract(pixel, origin)) return Ray(origin, direction)
def normal_to_world(self, normal): normal = multiply_tuple(transpose(self.__inverse_transform), normal) normal[3] = 0 normal = normalize(normal) if self.parent: normal = self.parent.normal_to_world(normal) return normal
def main(): print("What happens when you invert the identity matrix?") a = identity_matrix() print(inverse(a)) print() print("What do you get when you multiply a matrix by its inverse?") a = identity_matrix() a[1][0] = 2 a[0][3] = -5 b = multiply_matrix(a, inverse(a)) print(b) print() print( "Is there any difference between the inverse of the transpose of a matrix, and the transpose of the inverse?" ) c = inverse(transpose(a)) d = transpose(inverse(a)) print(c) print(d) print() print( "Remember how multiplying the identity matrix by a tuple gives you the tuple, unchanged?" ) t = tuple_4d(5, 4, 3, 1) i = identity_matrix() c = multiply_tuple(i, t) print(c) print() print( "Now, try changing any single element of the identity matrix to a different number, and then multiplying it by a tuple. What happens to the tuple?" ) i[1][1] = 2 i[2][2] = 3 c = multiply_tuple(i, t) print(c)
def main(): c = Canvas(500, 500) p = point(0, 0, 1) translate = translation(250, 0, 250) scale = scaling(100, 0, 100) for h in range(12): r = rotation_y(h * pi / 6) transform = multiply_matrix(translate, multiply_matrix(scale, r)) p2 = multiply_tuple(transform, p) print(f"position ({p2[0]}, {p2[1]}, {p2[2]})") c.set_pixel(round(p2[0]), c.height - round(p2[2]), color(0.0, 1.0, 0.0)) with open('clock.ppm', 'w') as out_file: out_file.write(c.to_ppm())
def step_impl(context): context.p4 = multiply_tuple(context.C, context.p3)
def step_impl(context): context.p3 = multiply_tuple(context.B, context.p2)
def step_impl(context): context.p2 = multiply_tuple(context.full_quarter, context.p)
def step_impl(context, x, y, z): actual = multiply_tuple(context.full_quarter, context.p) expected = point(x, y, z) assert_tuple(actual, expected)
def step_impl(context): actual = multiply_tuple(context.inv, context.p) expected = point(0, sqrt(2) / 2, -sqrt(2) / 2)
def step_impl(context): actual = multiply_tuple(context.half_quarter, context.p) expected = point(-sqrt(2) / 2, sqrt(2) / 2, 0) assert_tuple(actual, expected)
def step_impl(context): actual = multiply_tuple(context.transform, context.v) expected = context.v assert actual == expected, "%r is not %r" % (actual, expected)
def step_impl(context, x, y, z): actual = multiply_tuple(context.transform, context.p) expected = point(x, y, z) assert_tuple(actual, expected)
def world_to_object(self, point): if self.parent: point = self.parent.world_to_object(point) return multiply_tuple(self.__inverse_transform, point)
def pattern_at_shape(self, shape, point): """""" shape_point = shape.world_to_object(point) pattern_point = multiply_tuple(self.__inverse_transform, shape_point) return self.pattern_at(pattern_point)
def step_impl(context): assert multiply_tuple(context.a, context.b) == tuple_4d(18, 24, 33, 1)
def step_impl(context): assert multiply_tuple(identity_matrix(), context.a) == context.a
def step_impl(context, x, y, z): actual = multiply_tuple(context.transform, context.v) expected = vector(x, y, z) assert actual == expected, "%r is not %r" % (actual, expected)