def test_intersecting_translated_sphere_with_ray(self): r = Ray(point(0, 0, -5), vector(0, 0, 1)) s = Sphere() s.set_transform(translation(5, 0, 0)) xs = s.intersect(r) self.assertEqual(0, len(xs))
def test_sphere_behind_ray(self): r = Ray(point(0, 0, 5), vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(2, len(xs)) self.assertEqual(-6, xs[0].t) self.assertEqual(-4, xs[1].t)
def test_ray_originates_inside_sphere(self): r = Ray(point(0, 0, 0), vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(2, len(xs)) self.assertEqual(-1, xs[0].t) self.assertEqual(1, xs[1].t)
def test_ray_intersects_sphere_at_tangent(self): r = Ray(point(0, 1, -5), vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(2, len(xs)) self.assertEqual(5, xs[0].t) self.assertEqual(5, xs[1].t)
def test_intersecting_scaled_sphere_with_ray(self): r = Ray(point(0, 0, -5), vector(0, 0, 1)) s = Sphere() s.set_transform(scaling(2, 2, 2)) xs = s.intersect(r) self.assertEqual(2, len(xs)) self.assertEqual(3, xs[0].t) self.assertEqual(7, xs[1].t)
wall_size = 7.0 canvas_pixels = 1000 pixel_size = wall_size / canvas_pixels half = wall_size / 2 c = Canvas(canvas_pixels, canvas_pixels) col = color(1, 0, 0) start_time = time.time() for y in range(canvas_pixels): elapsed_time = time.time() - start_time if elapsed_time > 0 and y > 0: print("time_to_finish: " + str(1.0 * (canvas_pixels - y) / (1.0 * y / elapsed_time))) world_y = half - pixel_size * y for x in range(canvas_pixels): world_x = -half + pixel_size * x position = point(world_x, world_y, wall_z) ray = Ray(ray_origin, (position - ray_origin).normalize()) xs = s.intersect(ray) if hit(xs): hit_point = ray.position(hit(xs).t) norma_at_hit_point = s.normal_at(hit_point) eye = -ray.direction color = s.material.lighting(light, hit_point, eye, norma_at_hit_point) c.write_pixel(x, y, color) c.save_to_file('sphere6.ppm')
def test_ray_misses_sphere(self): r = Ray(point(0, 2, -5), vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(0, len(xs))