コード例 #1
0
 def test_finding_normal_on_child_object(self):
     g1 = Group()
     g1.transform = Transformations.rotation_y(math.pi / 2)
     g2 = Group()
     g2.transform = Transformations.scaling(1, 2, 3)
     g1.add_child(g2)
     s = Sphere()
     s.transform = Transformations.translation(5, 0, 0)
     g2.add_child(s)
     n = s.normal_at(Point(1.7321, 1.1547, -5.5774))
     self.assertEqual(n, Vector(0.2857, 0.4286, -0.8571))
コード例 #2
0
 def test_normal_sphere_z(self):
     s = Sphere()
     n = s.normal_at(Point(0, 0, 1))
     self.assertEqual(n, Vector(0, 0, 1))
コード例 #3
0
 def test_normal_transformed_sphere(self):
     s = Sphere()
     m = Transformations.scaling(1, 0.5, 1).dot(Transformations.rotation_z(math.pi / 5))
     s.transform = m
     n = s.normal_at(Point(0, math.sqrt(2) / 2, -(math.sqrt(2) / 2)))
     self.assertEqual(n, Vector(0, 0.97014, -0.24254))
コード例 #4
0
 def test_normal_translated_sphere(self):
     s = Sphere()
     s.transform = Transformations.translation(0, 1, 0)
     n = s.normal_at(Point(0, 1.70711, -0.70711))
     self.assertEqual(n, Vector(0, 0.70711, -0.70711))
コード例 #5
0
 def test_normalized_vector(self):
     s = Sphere()
     n = s.normal_at(Point(math.sqrt(3) / 3, math.sqrt(3) / 3, math.sqrt(3) / 3))
     self.assertEqual(n, Tuple.normalize(n))
コード例 #6
0
 def test_normal_sphere_nonaxial(self):
     s = Sphere()
     n = s.normal_at(Point(math.sqrt(3) / 3, math.sqrt(3) / 3, math.sqrt(3) / 3))
     self.assertEqual(n, Vector(math.sqrt(3) / 3, math.sqrt(3) / 3, math.sqrt(3) / 3))
コード例 #7
0
        for x in range(canvas_pixels):
            world_x = -half + pixel_size * x

            position = Point(world_x, world_y, wall_z)

            r = Ray(ray_origin, Vector.normalize(position - ray_origin))
            # shrink along y-axis
            # r = r.transform(Transformations.scaling(1, 0.5, 1))

            # shrink along x-axis
            # r = r.transform(Transformations.scaling(0.5, 1, 1))

            # shrink and rotate
            # r = r.transform(Transformations.rotation_z(math.pi / 4)).transform(Transformations.scaling(0.5, 1, 1))

            # shrink and skew
            # r = r.transform(Transformations.shearing(1, 0, 0, 0, 0, 0)).transform(Transformations.scaling(0.5, 1, 1))

            xs = shape.intersect(r)

            hit = Intersection.hit(xs)
            if hit is not None:
                point = Ray.position(r, hit.t)
                normal = shape.normal_at(point)
                eye = -r.direction
                color = PointLight.lighting(hit.object.material, hit.object,
                                            light, point, eye, normal, False)
                canvas.write_pixel(x, y, color)

    canvas.canvas_to_ppm()
コード例 #8
0
 def test_normal6(self):
     s = Sphere()
     s.set_transform(scale(1, 0.5, 1) * rotate_z(pi / 5))
     n = s.normal_at(Point(0, sqrt(2) / 2, -sqrt(2) / 2))
     self.assertTrue(n.equals(Vector(0, 0.97014, -0.24254)))
コード例 #9
0
 def test_normal5(self):
     s = Sphere()
     s.set_transform(translate(0, 1, 0))
     n = s.normal_at(Point(0, 1.70711, -0.70711))
     self.assertTrue(n.equals(Vector(0, 0.70711, -0.70711)))
コード例 #10
0
 def test_normal4(self):
     s = Sphere()
     n = s.normal_at(Point(sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3))
     self.assertTrue(n.equals(Vector(sqrt(3) / 3,
                                     sqrt(3) / 3,
                                     sqrt(3) / 3)))
コード例 #11
0
 def test_normal3(self):
     s = Sphere()
     n = s.normal_at(Point(0, 0, 1))
     self.assertTrue(n.equals(Vector(0, 0, 1)))