Esempio 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
Esempio n. 2
0
 def test_canvas_to_ppm_header(self):
     c = Canvas(5, 3)
     outfile = 'test_canvas_to_ppm_header.ppm'
     c.to_ppm(outfile)
     with open(outfile, 'r') as infile:
         assert 'P3' in infile.readline()
         assert '5 3' in infile.readline()
         assert '255' in infile.readline()
     try:
         os.remove(outfile)
     except FileNotFoundError:
         pass
Esempio n. 3
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')
Esempio n. 4
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')
Esempio n. 5
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')
Esempio n. 6
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')