def test_pattern_with_both_an_object_and_a_pattern_transformation(self): s = Sphere() s.set_transform(scale(2, 2, 2)) p = TestPattern() p.set_pattern_transform(translate(0.5, 1, 1.5)) c = p.pattern_at_shape(s, Point(2.5, 3, 3.5)) self.assertTrue(c.equals(Color(0.75, 0.5, 0.25)))
def test_stripes_with_an_object_transformation(self): black = Color(0, 0, 0) white = Color(1, 1, 1) s = Sphere() s.set_transform(scale(2, 2, 2)) p = StripePattern(white, black) c = p.pattern_at_shape(s, Point(1.5, 0, 0)) self.assertTrue(c.equals(white))
def test_transform2(self): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = Sphere() s.set_transform(scale(2, 2, 2)) xs = s.intersect(r) self.assertEqual(xs.count, 2) self.assertTrue(xs[0].t, 3) self.assertTrue(xs[1].t, 7)
def __init__(self, light=Light(Point(-10, 10, -10), Color(1, 1, 1))): self.light = light s1 = Sphere() s1.material.color = Color(0.8, 1.0, 0.6) s1.material.diffuse = 0.7 s1.material.specular = 0.2 s2 = Sphere() t2 = scale(0.5, 0.5, 0.5) s2.set_transform(t2) self.objs = [s1, s2]
def test_shade_hit_with_reflective_transparent_material(self): w = World() r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2)/2, sqrt(2)/2)) floor = Plane() floor.set_transform(translate(0, -1, 0)) floor.material.reflective = 0.5 floor.material.transparency = 0.5 floor.material.refractive_index = 1.5 w.objs.append(floor) ball = Sphere() ball.material.color = Color(1, 0, 0) ball.material.ambient = 0.5 ball.set_transform(translate(0, -3.5, -0.5)) w.objs.append(ball) ls = [Intersection(sqrt(2), floor)] xs = Intersections(ls) comps = xs[0].prepare_computations(r, xs) color = w.shade_hit(comps, 5) # print(color) self.assertTrue(Color(0.93391, 0.69643, 0.69243) == color)
def test_shade_hit_with_transparent_material(self): w = World() floor = Plane() floor.set_transform(translate(0, -1, 0)) floor.material.transparency = 0.5 floor.material.refractive_index = 1.5 ball = Sphere() ball.material.color = Color(1., 0., 0.) ball.material.ambient = 0.5 ball.set_transform(translate(0, -3.5, -0.5)) w.objs = [floor, ball] r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2)/2, sqrt(2)/2)) ls = [Intersection(sqrt(2), floor)] xs = Intersections(ls) comps = xs[0].prepare_computations(r, xs) color = w.shade_hit(comps, 5) self.assertTrue(Color(0.93642, 0.68642, 0.68642) == color)
from transform import rotate_x, scale, translate, scale, shear, rotate_z from matrix import Matrix from canvas import Canvas from point import Point from color import Color from ray import Ray from sphere import Sphere if __name__ == '__main__': pixel_size = 100 WIDTH = 100 HEIGHT = 100 c = Canvas(WIDTH, HEIGHT, Color(0.5, 0.5, 0.5)) sphere = Sphere(Point(0, 0, 0), radius=1) sphere.set_transform( rotate_z(pi / 6) * scale(1, 0.5, 1) * shear(1, 0, 0, 0, 0, 0)) camera = Point(0, 0, -5) wall_z = 10 wall_size = 7 canvas_pixels = 100 pixel_size = wall_size / canvas_pixels half = wall_size / 2 for j in range(HEIGHT): print(j) y = half - pixel_size * j for i in range(WIDTH): x = -half + pixel_size * i target = Point(x, y, wall_z) ray = Ray(camera, (target - camera).normalize()) intersections = sphere.intersect(ray)
def test_pattern_with_an_object_transformation(self): s = Sphere() s.set_transform(scale(2, 2, 2)) p = TestPattern() c = p.pattern_at_shape(s, Point(2, 3, 4)) self.assertTrue(c.equals(Color(1, 1.5, 2)))
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)))
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)))
def test_transform2(self): s = Sphere() t = translate(2, 3, 4) s.set_transform(t) self.assertTrue(matrix.equals(s.transform, t))