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_hit_positive(): s = Sphere() i1 = Intersection(s, 1) i2 = Intersection(s, 2) x = Intersections(i1, i2) h = x.hit assert h == i1
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_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_hit_one_negative(): s = Sphere() i1 = Intersection(s, -1) i2 = Intersection(s, 2) x = Intersections(i1, i2) h = x.hit assert h == i2
def test_hit_all_negative(): s = Sphere() i1 = Intersection(s, -1) i2 = Intersection(s, -2) x = Intersections(i1, i2) h = x.hit assert h is None
def test_intersection_aggregation(): s = Sphere() i1 = Intersection(s, 1) i2 = Intersection(s, 2) x = Intersections(i1, i2) assert len(x) == 2 assert x[0] == i1 assert x[1] == i2
def test_hit_always_lowest_non_negative(): s = Sphere() i1 = Intersection(s, -1) i2 = Intersection(s, 2) i3 = Intersection(s, 6) i4 = Intersection(s, 3) x = Intersections(i1, i2, i3, i4) h = x.hit assert h is i2
def test_sphere_normal_at_translate(): s = Sphere() s.set_transform(translation(0, 1, 0)) n = s.normal_at(Point(0, 1.70711, -0.70711)) assert n == Vector(0, 0.7071067811865475, -0.7071067811865476)
def test_sphere_normal_at_scaled(): s = Sphere() s.set_transform(scaling(1, 0.5, 1)) n = s.normal_at(Point(0, sqrt(2) / 2, -sqrt(2) / 2)) assert n == Vector(0, 0.9701425001453319, -0.24253562503633297)
def test_sphere_normal_at_arbitrary(): s = Sphere() n = s.normal_at(Point(sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3)) assert n == Vector(sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3)
def test_sphere_normal_normalized(): s = Sphere() n = s.normal_at(Point(sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3)) assert n == n.normalize()
def test_sphere_normal_at_x(): s = Sphere() n = s.normal_at(Point(1, 0, 0)) assert n == Vector(1, 0, 0)
def test_sphere_normal_at_z(): s = Sphere() n = s.normal_at(Point(0, 0, 1)) assert n == Vector(0, 0, 1)
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
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_sphere_translate(): s = Sphere() t = translation(2, 3, 4) s.set_transform(t) assert s.transform == t
def test_sphere_default_transform(): s = Sphere() assert s.transform == IdentityMatrix(4)
from math import pi 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)
def test_sphere_material(): s = Sphere() m = s.material assert m == DefaultMaterial()
def test_intersection_init(): s = Sphere() i = Intersection(s, 3.5) assert i.t == 3.5 assert i.object == s
def test_sphere_material_assignment(): s = Sphere() m = Material(Color(1, 1, 1), 2, 1, 0.5, 12) s.set_material(m) assert s.material == m
from src.canvas import Canvas from src.color import Color from src.lights import PointLight from src.primitives import Sphere from src.ray import Ray from src.tupl import Point s = Sphere() r_origin = Point(0, 0, -5) wall_z = 10 wall_size = 7 light_position = Point(-10, 10, 10) light_color = Color(1, 1, 1) light = PointLight(light_position, light_color) N = 200 c = Canvas(N, N) pixel_size = wall_size / N half = wall_size / 2 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: point = r.position(X.hit.t)