def normal_at(self, world_point): if minkowski_norm(world_point) <= 0: if world_point.z > 0: return Vector(1, 0, 0) else: return Vector(-1, 0, 0) if world_point.x >= 0: return Vector(0, 1, 1) if world_point.x <= 0: return Vector(0, 1, -1)
def main(): logging.basicConfig( level=logging.INFO, format= '%(asctime)s %(levelname)s %(filename)s:%(lineno)s %(name)s %(message)s' ) shape = ClippedShape(CrookedPlane()) shape.material = Material() shape.material.color = Color(1, .2, 1) #shape.material.pattern = gradient_pattern(BLUE, GREEN) #shape.material.pattern.transform = Scaling(4, 2, 2) objects = (shape, ) rad = 6 camera_height = .8 * rad frames = [] for t in np.arange(0, math.pi, .1 * math.pi): logging.info("New Frame %s", t) camera_position = Point(rad * math.cos(t), rad * math.sin(t), camera_height) vt = view_transform(camera_position, Point(0, 0, 0), Vector(0, 0, 1)) canvas = make_single_frame(objects, vt) frames.append(canvas._image) imageio.mimsave("examples/crooked_plane.gif", frames, duration=0.25)
def main(resolution): logging.basicConfig( level=logging.INFO, format= '%(asctime)s %(levelname)s %(filename)s:%(lineno)s %(name)s %(message)s' ) shape = ClippedShape(CrookedPlane()) shape.material = Material() shape.material.color = Color(1, .2, 1) shape.material.transparency = 1.0 shape.material.refractive_index = 1.5 objects = (shape, ) rad = 6 camera_height = .8 * rad frames = [] for t in np.arange(0, math.pi, .1 * math.pi): logging.info("New Frame %s", t) camera_position = Point(rad * math.cos(t), rad * math.sin(t), camera_height) vt = view_transform(camera_position, Point(0, 0, 0), Vector(0, 0, 1)) canvas = make_single_frame(objects, vt, resolution) frames.append(canvas._image) path = Path(__file__).parent / "chap11.gif" imageio.mimsave(str(path), frames, duration=0.25)
def test_ray_misses_to_wing2(): # Given cp = DefaultCrookedPlane() ray = Ray(Point(1, 0, 0), Vector(0, 1, -1)) # When xs = cp.intersect_wing2(ray) assert xs is None
def test_normal_at_stem(): # Given p = Point(0, .5, 1) # When cp = DefaultCrookedPlane() n = cp.normal_at(p) # Then assert n == Vector(1, 0, 0)
def test_ray_intersects_stem(): # Given cp = DefaultCrookedPlane() ray = Ray(Point(0, 0, -1), Vector(1, 0, 0)) # When xs = cp.intersect_stem(ray) # Then assert xs.t == 0
def test_ray_misses_stem(): # Given cp = DefaultCrookedPlane() ray = Ray(Point(0, 1, 0), Vector(1, 0, 1)) # When xs = cp.intersect_stem(ray) # Then assert xs is None
def test_ray_parallel_to_wing2(): # Given cp = DefaultCrookedPlane() ray = Ray(Point(-1, 1, 0), Vector(0, 1, 1)) # When xs = cp.intersect_wing2(ray) # Then assert xs is None
def test_ray_intersects_wing2(): # Given cp = DefaultCrookedPlane() ray = Ray(Point(-1, 0, -1), Vector(0, 0, 1)) # When xs = cp.intersect_wing2(ray) # Then assert xs.t == 1
def test_normal_at_wing2(): # Given p = Point(-3, 5, -5) # When cp = DefaultCrookedPlane() n = cp.normal_at(p) # Then assert n == Vector(0, 1, -1)
def main(canvas_dimensions): start = Point(0, 1, 0) velocity = normalize(Vector(1, 1.8, 1)) * 11.25 p = Projectile(start, velocity) gravity = Vector(0, -0.1, 0) wind = Vector(-0.01, 0, 0) e = Environment(gravity, wind) print(canvas_dimensions) c = Canvas(*canvas_dimensions) while p.position.y > 0: x, y = int(p.position.x), c.height - int(p.position.y) print(x, y) print(p.position) c.write_pixel(x, y, Color(0, 0, 1)) p = tick(e, p) print(f"p has landed at {p}") path = Path(__file__).parent / "chap2.png" canvas_to_png(str(path), c)
def main(camera_resolution): logging.basicConfig( level=logging.INFO, format= '%(asctime)s %(levelname)s %(filename)s:%(lineno)s %(name)s %(message)s' ) objects = (make_floor(), make_middle(), make_right(), make_left()) light = point_light(Point(-10, 10, -10), Color(1, 1, 1)) world = World(objects=objects, light=light) camera = Camera(*camera_resolution, math.pi / 3) camera.transform = view_transform(Point(0, 1 / 5, -5), Point(0, 1, 0), Vector(0, 1, 0)) canvas = camera.render(world) path = Path(__file__).parent / "chap10.png" canvas_to_png(str(path), canvas)
def main(): c = Canvas(200, 200) adjustment = Vector(c.width/2, c.height/2 ,0) radius = 3 * c.width / 8 center = Point(0, 0, 0) + adjustment c.write_pixel(int(center.x), int(center.y), RED) twelve = Point(0, radius, 0) points = [twelve] for k in range(12): R = rotation_z(k * math.pi / 6) points.append(R * twelve) points = [p + adjustment for p in points] for p in points: c.write_pixel(int(p.x), int(p.y), GREEN) path = Path(__file__).parent / "chap4.png" canvas_to_png(str(path), c)
def draw_objects(objects): logging.basicConfig( level=logging.INFO, format= '%(asctime)s %(levelname)s %(filename)s:%(lineno)s %(name)s %(message)s' ) lights = [ point_light(Point(10, 10, 15), WHITE), point_light(Point(-10, -10, 15), WHITE) ] world = World(objects=objects, light=lights) camera = Camera(100, 50, math.pi / 3) camera.transform = view_transform(Point(12, -6, 10), Point(0, 0, 0), Vector(0, 0, 1)) canvas = camera.render(world) path = Path(__file__).parent / "output.png" canvas_to_png(str(path), canvas)
def main(canvas_dimensions): logging.basicConfig( level=logging.INFO, format= '%(asctime)s %(levelname)s %(filename)s:%(lineno)s %(process)d %(name)s %(message)s' ) objects = (make_floor(), make_wall(-math.pi / 4), make_wall(math.pi / 4), make_middle(), make_right(), make_left()) light = point_light(Point(-10, 10, -10), Color(1, 1, 1)) world = World(objects=objects, light=light) camera = Camera(canvas_dimensions[0], canvas_dimensions[1], math.pi / 3) camera.transform = view_transform(Point(0, 1 / 5, -5), Point(0, 1, 0), Vector(0, 1, 0)) start = time.time() canvas = camera.render(world) stop = time.time() logging.info("Rendering took %s seconds", stop - start) path = Path(__file__).parent / "chap7.png" canvas_to_png(str(path), canvas)
def step_impl(context, r, x, y, z, dx, dy, dz): _r = Ray(Point(x, y, z), Vector(dx, dy, dz)) setattr(context, r, _r)
def step_impl(context, name, x, y, z): setattr(context, name, Vector(x, y, z))
def test_ray_parallel_to_stem(): ray = Ray(Point(-1, 0, 1), Vector(0, 1, 1)) assert [] == intersect_stem(ray)
def test_ray_misses_to_wing2(): ray = Ray(Point(1, 0, 0), Vector(0, 1, -1)) assert [] == intersect_wing2(ray)
def test_ray_intersects_wing2(): ray = Ray(Point(-1, 0, -1), Vector(0, 0, 1)) assert [1] == intersect_wing2(ray)
def test_ray_parallel_to_wing2(): ray = Ray(Point(-1, 1, 0), Vector(0, 1, 1)) assert [] == intersect_wing2(ray)
def test_ray_misses_stem(): ray = Ray(Point(0, 1, 0), Vector(1, 0, 1)) assert [] == intersect_stem(ray)
def test_ray_intersects_stem(): ray = Ray(Point(0, 0, -1), Vector(1, 0, 0)) assert [0] == intersect_stem(ray)
def step_impl(context, n, dx, dy, dz): setattr(context, n, Vector(dx, dy, dz))
def normal_at(self, point): return Vector(point.x, point.y, point.z)
def normal_at(self, world_point): return Vector(0, 1, 0)
def test_ray_parallel_to_wing1(): ray = Ray(Point(1, 1, 0), Vector(0, 1, -1)) assert [] == intersect(ray)
def test_ray_misses_to_wing1(): ray = Ray(Point(-1, 0, 0), Vector(0, 1, 1)) assert [] == intersect(ray)