Exemplo n.º 1
0
    def test_canvas_to_ppm_body(self):
        c = Canvas(5, 3)
        c1 = Color(1.5, 0, 0)
        c2 = Color(0, 0.5, 0)
        c3 = Color(-0.5, 0, 1)

        c.write_pixel(0, 0, c1)
        c.write_pixel(2, 1, c2)
        c.write_pixel(4, 2, c3)

        outfile = 'test_canvas_to_ppm_body.ppm'
        c.to_ppm(outfile)

        with open(outfile, 'r') as infile:
            [infile.readline() for i in range(3)]  # skip header
            body = list(map(str.strip, infile.readlines()))
            l1 = "255 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            l2 = "0 0 0 0 0 0 0 128 0 0 0 0 0 0 0"
            l3 = "0 0 0 0 0 0 0 0 0 0 0 0 0 0 255"
            assert body[0] == l1
            assert body[1] == l2
            assert body[2] == l3
        try:
            os.remove(outfile)
        except FileNotFoundError:
            pass
Exemplo n.º 2
0
def run() -> None:
    STEPS = 180
    SIZE = 600
    MIDDLE = SIZE // 2

    canvas = Canvas(SIZE, SIZE)
    color = Color(1, 0, 0)
    color_increment = Color(0, 0, 1.0 / STEPS)

    position = point(0, 1, 0)

    rotate = rotation_z(-2 * math.pi / STEPS)
    translate = translation(MIDDLE, MIDDLE, 0)
    scale = scaling(SIZE // 3, SIZE // 3, 1)

    for i in range(STEPS):
        canvas_position = translate * scale * position
        assert isinstance(canvas_position, Point)
        canvas.write_pixel(
            int(round(canvas_position.x)),
            SIZE - int(round(canvas_position.y)),
            color,
        )
        position = rotate * position
        color += color_increment

    ppm = PPM(canvas)

    file_name = "clocks.ppm"
    ppm.save_to_file(file_name)
    print(f"Output stored to {file_name}")
Exemplo n.º 3
0
def run() -> None:
    # Eye is at (0,0, 5)
    origin = point(0, 0, 5)

    shape = Sphere()
    # shape.set_transform(scaling(0.5, 1, 1))
    shape.material.color = Color(0.9, 0.2, 1)

    light = PointLight(point(-10, 10, 10), Color(1, 1, 1))
    canvas = Canvas(CANVAS_SIZE, CANVAS_SIZE)

    for i in range(CANVAS_SIZE):
        for j in range(CANVAS_SIZE):
            target = canvas_to_world(point(i, j, 0))
            ray = Ray(origin, normalize(target - origin))
            hit = find_hit(shape.intersect(ray))
            if hit is not None:
                hit_point = position(ray, hit.t)
                normal = hit.shape.normal_at(hit_point)
                pixel_color = lighting(hit.shape.material, light, hit_point,
                                       -ray.direction, normal)
                canvas.write_pixel(i, j, pixel_color)

    PPM(canvas).save_to_file("sphere.ppm")
Exemplo n.º 4
0
    def test_canvas_write_pixel(self):
        c = Canvas(32, 32)
        col = Color(12, 3, 5)
        c.write_pixel(2, 3, col)

        assert c[2, 3] == col
Exemplo n.º 5
0
from src.canvas import Canvas
from src.color import Color
from src.tupl import Vector, Point


def tick(p, v):
    p += v
    v += Vector(0, -0.1, 0) + Vector(-0.01, 0, 0)
    return p, v


c = Canvas(900, 550)
col = Color(255, 255, 255)

if __name__ == '__main__':
    p = Point(0, 1, 0)
    v = Vector(1, 1.8, 0).normalize() * 11.25

    for i in range(100):
        X = int(p.x)
        Y = int(p.y)

        if p.y <= 0:
            break
        if (0 <= X < c.width) and (0 <= c.height - Y < c.height):
            c.write_pixel(X, c.height - Y, col)
        p, v = tick(p, v)

    c.to_ppm('trajectory.ppm')
Exemplo n.º 6
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')
Exemplo n.º 7
0
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)
            normal = X.hit.object.normal_at(point)
            eye = -r.direction
            color = X.hit.object.material.lighting(light, point, eye, normal)
            c.write_pixel(x, y, color)
c.to_ppm('circle_shaded.ppm')
Exemplo n.º 8
0
from math import pi

from src.canvas import Canvas
from src.color import Color
from src.transformations import rotation_z
from src.tupl import Point

c = Canvas(400, 400)
origin = Point(0, 0, 0)
r = pi / 6

hand = Point(0, 1, 0) * c.height * (3 / 8)
t = rotation_z(r)
for i in range(12):
    X, Y = int(hand.x + c.width / 2), int(hand.y + c.height / 2)
    c.write_pixel(X, Y, Color(255, 255, 255))
    hand = t * hand
c.to_ppm('clock.ppm')