Esempio n. 1
0
def main():
    resolutions = {'low': 100, 'high': 500}
    parser = argparse.ArgumentParser()
    parser.add_argument('resolution', choices=resolutions.keys())
    args = parser.parse_args()

    canvas = paint(resolutions[args.resolution])
    path = Path(__file__).parent / "chap6.png"
    canvas_to_png(str(path), canvas)
Esempio n. 2
0
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)
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
0
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)
Esempio n. 6
0
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)
Esempio n. 7
0
canvas = Canvas(canvas_pixels, canvas_pixels)
shape = Sphere()

for y in range(canvas_pixels - 1):
    world_y = half - pixel_size * y
    for x in range(canvas_pixels - 1):
        world_x = -half + pixel_size * x

        position = Point(world_x, world_y, wall_z)
        r = Ray(ray_origin, normalize(position - ray_origin))
        xs = intersect(shape, r)
        if hit(xs) is not None:
            write_pixel(canvas, x, y, RED)

path = Path(__file__).parent / "chap5.png"
canvas_to_png(str(path), canvas)

# Copyright 2020 Bloomberg Finance L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.