def test_ray_intersects_sphere_object(): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = Sphere() x = r.intersects(s) assert len(x) == 2 assert x[0].object == s assert x[1].object == s
def test_sphere_intersection_scaled(): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = Sphere() s.set_transform(scaling(2, 2, 2)) x = r.intersects(s) assert len(x) == 2 assert x[0].t == 3 assert x[1].t == 7
def test_ray_intersect_from_behind(): r = Ray(Point(0, 0, 5), Vector(0, 0, 1)) s = Sphere() x = r.intersects(s) assert len(x) == 2 assert x[0].t == -6 assert x[1].t == -4
def test_ray_intersect_from_middle(): r = Ray(Point(0, 0, 0), Vector(0, 0, 1)) s = Sphere() x = r.intersects(s) assert len(x) == 2 assert x[0].t == -1 assert x[1].t == 1
def test_ray_intersect_sphere_tangent(): r = Ray(Point(0, 1, -5), Vector(0, 0, 1)) s = Sphere() x = r.intersects(s) assert len(x) == 2 assert x[0].t == 5 assert x[1].t == 5
def test_sphere_intersection_translate(): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = Sphere() s.set_transform(translation(5, 0, 0)) x = r.intersects(s) assert len(x) == 0
def test_ray_intersect_sphere_no_intersection(): r = Ray(Point(0, 2, -5), Vector(0, 0, 1)) s = Sphere() x = r.intersects(s) assert len(x) == 0
from src.canvas import Canvas from src.color import Color from src.primitives import Sphere from src.ray import Ray from src.transformations import scaling, rotation_z from src.tupl import Point s = Sphere() t = rotation_z(pi / 4) @ scaling(0.5, 1, 1) s.set_transform(t) r_origin = Point(0, 0, -5) wall_z = 10 wall_size = 7 N = 100 c = Canvas(N, N) pixel_size = wall_size / N half = wall_size / 2 red = Color(255, 0, 0) for y in range(c.height): world_y = half - pixel_size * y for x in range(c.width): world_x = -half + pixel_size * x position = Point(world_x, world_y, wall_z) r = Ray(r_origin, (position - r_origin).normalize()) X = r.intersects(s) if X.hit is not None: c.write_pixel(x, y, red) c.to_ppm('circle.ppm')