def plot_points_on_canvas(ps: List[Point], c: Canvas):
    blue = Color(0, 0, 1)
    for p in ps:
        x, y = int(p.x + c.width / 2), int(p.y + c.height / 2)
        print(f'x={x}, y={y}')
        if 0 <= x < c.width and 0 <= y < c.height:
            c.write_pixel(x, y, blue)
Пример #2
0
 def test_construct_ppm_header(self):
     canvas = Canvas(5, 3)
     ppm = canvas.to_ppm()
     lines = ppm.split('\n')
     self.assertTrue(lines[0] == 'P3')
     self.assertTrue(lines[1] == '5 3')
     self.assertTrue(lines[2] == '255')
Пример #3
0
 def test_wrap_ppm_pixel_data_at_70_chars(self):
     canvas = Canvas(10, 2, Color(1, 0.8, 0.6))
     ppm = canvas.to_ppm()
     lines = ppm.split('\n')
     self.assertTrue(lines[3] == '255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204')
     self.assertTrue(lines[4] == '153 255 204 153 255 204 153 255 204 153 255 204 153')
     self.assertTrue(lines[5] == '255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204')
     self.assertTrue(lines[6] == '153 255 204 153 255 204 153 255 204 153 255 204 153')
Пример #4
0
 def test_splitting_long_lines_in_ppm(self):
     c = Canvas(10, 2)
     for x in range(c.width):
         for y in range(c.height):
             c.write_pixel(x, y, Color(1, 0.8, 0.6))
     ppm = c.to_ppm()
     assert ''.join(ppm[3:]) == "255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204\n"\
                                "153 255 204 153 255 204 153 255 204 153 255 204 153\n" \
                                "255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204\n"\
                                "153 255 204 153 255 204 153 255 204 153 255 204 153\n"
Пример #5
0
 def test_constructing_ppm_pixel_data(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)
     ppm = c.to_ppm()
     assert ''.join(ppm[3:]) == "255 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" \
                                "0 0 0 0 0 0 0 128 0 0 0 0 0 0 0\n" \
                                "0 0 0 0 0 0 0 0 0 0 0 0 0 0 255\n"
Пример #6
0
 def test_construct_ppm_pixel_data(self):
     canvas = Canvas(5, 3)
     c1 = Color(1.5, 0, 0)
     c2 = Color(0, 0.5, 0)
     c3 = Color(-0.5, 0, 1)
     canvas.write_pixel(0, 0, c1)
     canvas.write_pixel(2, 1, c2)
     canvas.write_pixel(4, 2, c3)
     ppm = canvas.to_ppm()
     lines = ppm.split('\n')
     self.assertTrue(lines[3] == '255 0 0 0 0 0 0 0 0 0 0 0 0 0 0')
     self.assertTrue(lines[4] == '0 0 0 0 0 0 0 128 0 0 0 0 0 0 0')
     self.assertTrue(lines[5] == '0 0 0 0 0 0 0 0 0 0 0 0 0 0 255')
Пример #7
0
from os import sep
from raytracer.canvas import Canvas, write_ppm_to_file
from raytracer.tuples import Point, Vector, Color
from tests.showcase.projectiles import Projectile, Environment, tick


def plot_point_on_canvas(p: Point, c: Canvas):
    x, y = int(p.x), c.height - int(p.y)
    if 0 <= x < c.width and 0 <= y < c.height:
        c.write_pixel(x, y, Color(1, 0, 0))


if __name__ == '__main__':
    start = Point(0, 1, 0)
    velocity = Vector(1, 1.8, 0).normalize() * 11.25
    proj = Projectile(start, velocity)

    env = Environment(Vector(0, -0.1, 0), Vector(-0.01, 0, 0))

    canvas = Canvas(900, 550)
    while proj.position.y > 0:
        proj = tick(env, proj)
        plot_point_on_canvas(proj.position, canvas)

    write_ppm_to_file(canvas.to_ppm(),
                      f'..{sep}..{sep}resources{sep}proj2.ppm')
Пример #8
0
 def test_create_canvas(self):
     c = Canvas(10, 20)
     assert c.width == 10
     assert c.height == 20
     assert c._pixels == [[Color(0, 0, 0) for _ in range(c.height)]
                          for _ in range(c.width)]
Пример #9
0
 def test_ppm_files_terminated_with_newline(self):
     c = Canvas(5, 3)
     ppm = c.to_ppm()
     assert ppm[-1][-1] == '\n'
Пример #10
0
 def test_constructing_ppm_header(self):
     c = Canvas(5, 3)
     ppm = c.to_ppm()
     assert ''.join(ppm[:3]) == "P3\n" \
                                "5 3\n" \
                                "255\n"
Пример #11
0
 def test_write_pixel_to_canvas(self):
     canvas = Canvas(10, 20)
     red = Color(1, 0, 0)
     canvas.write_pixel(2, 3, red)
     self.assertTrue(canvas.pixel_at(2, 3) == red)
from raytracer.matrices import rotation_z
from typing import List
import math


def plot_points_on_canvas(ps: List[Point], c: Canvas):
    blue = Color(0, 0, 1)
    for p in ps:
        x, y = int(p.x + c.width / 2), int(p.y + c.height / 2)
        print(f'x={x}, y={y}')
        if 0 <= x < c.width and 0 <= y < c.height:
            c.write_pixel(x, y, blue)


if __name__ == '__main__':
    canvas = Canvas(120, 120)
    points = []

    middle = Point(0, 0, 0)
    points.append(middle)

    hour = Point(0, -30, 0)
    points.append(hour)
    for _ in range(1, 12):
        hour = rotation_z(math.pi / 6) * hour
        points.append(hour)

    plot_points_on_canvas(points, canvas)

    write_ppm_to_file(canvas.to_ppm(),
                      f'..{sep}..{sep}resources{sep}clock.ppm')
Пример #13
0
 def test_ppm_ends_with_newline_char(self):
     canvas = Canvas(5, 3)
     ppm = canvas.to_ppm()
     self.assertTrue(ppm[len(ppm) - 1] == '\n')
Пример #14
0
def plot_point_on_canvas(p: Point, c: Canvas):
    x, y = int(p.x), c.height - int(p.y)
    if 0 <= x < c.width and 0 <= y < c.height:
        c.write_pixel(x, y, Color(1, 0, 0))
Пример #15
0
 def test_write_pixel_to_canvas(self):
     c = Canvas(10, 20)
     red = Color(1, 0, 0)
     c.write_pixel(2, 3, red)
     assert c.pixel_at(2, 3) == red
Пример #16
0
from raytracer.canvas import Canvas, write_ppm_to_file
from raytracer.lights import PointLight
from raytracer.materials import Material
from raytracer.rays import Ray
from raytracer.shapes import Sphere
from raytracer.tuples import Point, Color

if __name__ == '__main__':
    ray_origin = Point(0, 0, -5)
    wall_z = 10
    wall_size = 7.0
    canvas_pixels = 100
    pixel_size = wall_size / canvas_pixels
    half = wall_size / 2

    canvas = Canvas(canvas_pixels, canvas_pixels)
    red = Color(1, 0, 0)
    sphere = Sphere()
    m = Material()
    m.color = Color(1, 0.2, 1)
    sphere.material = m

    light = PointLight(Point(-10, 10, -10), Color(1, 1, 1))

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

            position = Point(world_x, world_y, wall_z)
            ray = Ray(ray_origin, (position - ray_origin).normalize())
Пример #17
0
 def test_create_canvas(self):
     canvas = Canvas(10, 20)
     self.assertEqual(canvas.width == 10, True)
     self.assertEqual(canvas.height == 20, True)
     self.assertEqual(canvas.sum(),  0)