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_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"
예제 #3
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"
예제 #4
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')
예제 #5
0
    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())
            hit = sphere.intersect(ray).hit()
            if hit:
                point = ray.position(hit.t)
                normal = hit.object.normal_at(point)
                eye = -ray.direction

                color = hit.object.material.lighting(light, point, eye, normal)

                canvas.write_pixel(x, y, color)

    write_ppm_to_file(canvas.to_ppm(),
                      f'..{sep}..{sep}resources{sep}circle.ppm')
예제 #6
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))
from raytracer.matrices import scaling, rotation_z, shearing
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()
    sphere.transformation = scaling(1, .5, 1).shear(1, 0, 0, 0, 0, 0)

    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())
            if sphere.intersect(ray).hit():
                canvas.write_pixel(x, y, red)

    write_ppm_to_file(canvas.to_ppm(),
                      f'..{sep}..{sep}resources{sep}circle.ppm')
예제 #8
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
예제 #9
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)