Exemple #1
0
    def test_sphere_miss(self):

        r = ray(point(0, 2, -5), vector(0, 0, 1))

        s = sphere()

        xs = intersect(s, r)

        self.assertEqual(len(xs), 0, 'There should be no intersections ')
Exemple #2
0
    def test_sphere_transformed2(self):

        r = ray(point(0, 0, -5), vector(0, 0, 1))

        s = sphere()

        s.set_transform(translation(5, 0, 0))
        xs = intersect(s, r)

        self.assertEqual(len(xs), 0, 'There sould be no intersection')
Exemple #3
0
    def test_sphere_intersect_group(self):

        r = ray(point(0, 0, -5), vector(0, 0, 1))

        s = sphere()

        xs = intersect(s, r)

        self.assertEqual(len(xs), 2, 'There should be 2 intersections')
        self.assertEqual(xs[0].object, s, 'The intersection object failed')
        self.assertEqual(xs[1].object, s, 'The intersection object failed')
Exemple #4
0
    def test_sphere(self):

        r = ray(point(0, 0, -5), vector(0, 0, 1))

        s = sphere()

        xs = intersect(s, r)

        self.assertEqual(len(xs), 2, 'There should be 2 intersections')
        self.assertEqual(xs[0].t, 4.0, 't=4 should intersect')
        self.assertEqual(xs[1].t, 6.0, 't=6 should intersect')
Exemple #5
0
    def test_sphere_behind(self):

        r = ray(point(0, 0, 5), vector(0, 0, 1))

        s = sphere()

        xs = intersect(s, r)

        self.assertEqual(len(xs), 2,
                         'There should be 2 intersections (virtual)')
        self.assertEqual(xs[0].t, -6.0, 't=-6 should be an intersection time')
        self.assertEqual(xs[1].t, -4.0, 't=-4 should be an intersection time')
Exemple #6
0
    def test_sphere_inner(self):

        r = ray(point(0, 0, 0), vector(0, 0, 1))

        s = sphere()

        xs = intersect(s, r)

        self.assertEqual(len(xs), 2,
                         'There should be 2 intersections (virtual)')
        self.assertEqual(xs[0].t, -1.0, 't=-1 should be an intersection time')
        self.assertEqual(xs[1].t, 1.0, 't=1 should be an intersection time')
Exemple #7
0
    def test_sphere_transformed(self):

        r = ray(point(0, 0, -5), vector(0, 0, 1))

        s = sphere()

        s.set_transform(scaling(2, 2, 2))

        xs = intersect(s, r)

        self.assertEqual(len(xs), 2, 'There should have been 2 intersections')
        self.assertEqual(xs[0].t, 3, 't=3 should have been present')
        self.assertEqual(xs[1].t, 7, 't=4 should have been present')
light_color = color(1, 1, 1)
light = PointLight(light_position, light_color)

s = sphere()
s.material = Material()
s.material.color = color(1, 0.2, 1)
trans = scaling(200, 200, 1) * translation(0, 0, 800)
s.set_transform(trans)

print('{:04d},{:04d}'.format(0, 0), end='\r')
h = False
for i in range(CANVAS_W):
    #print()
    for j in range(CANVAS_H):
        print('{:04d},{:04d}'.format(i, j), end='\r')
        r = ray(point(0, 0, 0), vector(i + pad_x, -pad_y - j, CANVAS_DISTANCE))
        inter = intersect(s, r)
        if hit(inter) != None:
            c[i, j] = HIT_COLOR
            h = True
        #if h:
        #print('\033[48;2;255;0;0m  \033[0m', end='', flush=True)
        #else:
        #print('\033[48;2;230;230;230m  \033[0m', end='', flush=True)
        h = False

PPM = c.to_PPM()

with open('shadow_sphere.ppm', 'w') as f:
    f.write(PPM)