def test_rotating_a_point_around_the_y_axis(): # Given p = Point(0, 0, 1) half_quarter = rotation_y(pi / 4) full_quarter = rotation_y(pi / 2) # Then assert half_quarter * p == Point(sqrt(2) / 2, 0, sqrt(2) / 2) assert full_quarter * p == Point(1, 0, 0)
def test_constructing_a_ray_when_the_camera_is_transformed(): # Given c = Camera(201, 101, pi / 2) # When c.transform = rotation_y(pi / 4) * translation(0, -2, 5) r = c.ray_for_pixel(100, 50) # Then assert r.origin == Point(0, 2, -5) assert r.direction == Vector(sqrt(2) / 2, 0, -sqrt(2) / 2)
def test_finding_the_normal_on_a_child_object(): # Given g1 = Group() g1.transform = rotation_y(pi / 2) g2 = Group() g2.transform = scaling(1, 2, 3) g1.add_child(g2) s = Sphere() s.transform = translation(5, 0, 0) g2.add_child(s) # When n = s.normal_at(Point(1.7321, 1.1547, -5.5774)) # Then assert n == Vector(0.2857, 0.42854, -0.85716)
def test_converting_a_normal_from_object_to_world_space(): # Given g1 = Group() g1.transform = rotation_y(pi / 2) g2 = Group() g2.transform = scaling(1, 2, 3) g1.add_child(g2) s = Sphere() s.transform = translation(5, 0, 0) g2.add_child(s) # When n = s.normal_to_world(Vector(sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3)) # Then assert n == Vector(0.28571, 0.42857, -0.85714)
def test_converting_a_point_from_world_to_object_space(): # Given g1 = Group() g1.transform = rotation_y(pi / 2) g2 = Group() g2.transform = scaling(2, 2, 2) g1.add_child(g2) s = Sphere() s.transform = translation(5, 0, 0) g2.add_child(s) # When p = s.world_to_object(Point(-2, 0, -10)) # Then assert p == Point(0, 0, -1)
if __name__ == "__main__": # pattern = StripePattern(Color(1, 0, 0), Color(1, 1, 1)) # pattern = CheckersPattern(Color(1, 0, 0), Color(1, 1, 1)) pattern = GradientPattern(Color(1, 0, 0), Color(1, 1, 1)) # pattern = RingPattern(Color(1, 0, 0), Color(1, 1, 1)) pattern.transform = scaling(0.2, 0.2, 0.2) floor = Plane() floor.material = Material() floor.material.color = Color(1, 0.9, 0.9) # floor.material.pattern = pattern backdrop = Plane() backdrop.transform = \ rotation_y(pi / 3) * translation(0, 0, 5) * rotation_x(pi / 2) backdrop2 = Plane() backdrop2.transform = \ rotation_y(-pi / 3) * translation(0, 0, 5) * rotation_x(pi / 2) middle = Sphere() # middle = glass_sphere() middle.transform = translation(-0.5, 1, 0.5) # middle.reflective = 1.0 middle.material = Material() middle.material.color = Color(0.1, 1, 0.5) # middle.material.pattern = pattern middle.material.diffuse = 0.7 middle.material.specular = 0.3 # middle.material.reflective = 1.0