Пример #1
0
    def test_canvas_to_PPM(self):

        c = canvas(5, 4)

        ppm = c.to_PPM()
        first_three_lines = ppm.splitlines()[:3]
        expected = ['P3', '5 4', '255']

        self.assertEqual(first_three_lines, expected,
                         'The frist lines of generated PPM is expected')
Пример #2
0
    def test_canvas(self):

        c = canvas(10, 20)

        self.assertEqual(c.width, 10, 'The canvas width should be 10')
        self.assertEqual(c.height, 20, 'The canvas height should be 20')

        black_color = color(0, 0, 0)

        is_black = [c_color == black_color for c_color in c]

        self.assertTrue(all(is_black), 'All canvas pixels should be black')
Пример #3
0
    def test_set_pixel(self):

        c = canvas(10, 20)

        red = color(1, 0, 0)
        black = color(0, 0, 0)
        c[2, 3] = red

        self.assertEqual(c[2, 3], red, 'The pixel at (2,3) should be red')
        self.assertEqual(c[0, 0], black, 'The pixel at (2,3) should be red')
        self.assertEqual(c._grid[32], red, 'The pixel at (2,3) should be red')

        with self.assertRaises(IndexError):
            c[31, 21] = 10
Пример #4
0
    def test_PPM_crop(self):

        c = canvas(10, 2)
        c1 = color(1, 0.8, 0.6)
        c.set_one_color(c1)

        ppm = c.to_PPM()

        ppm_last_4_lines = '\n'.join(ppm.splitlines()[3:]) + ppm[-1]

        expected = ('255 204 153 ' * 5 + '255 204\n' + '153 255 204 ' * 4 +
                    '153\n') * 2

        self.assertEqual(ppm_last_4_lines,expected,\
            'The generated ppm has not the expected crop')
Пример #5
0
    def canvas(self):
        canvas_list = {(round(x),round(y)) for x,y in self.tracjectory}

        max_x = max(canvas_list, key=lambda loc: loc[0])[0]+1
        max_y = max(canvas_list, key=lambda loc: loc[1])[1]+1

        c = canvas(max_x,max_y)
        background_c = color(0.9,0.9,0.9)
        c.set_one_color(background_c)

        for pt in canvas_list:
            x = pt[0]
            y = max_y-pt[1]-1
            pt2 = x,y
            c[pt2] = color(1,0,0)

        return c
Пример #6
0
    def test_PPM_colors(self):

        c = canvas(5, 3)
        c1 = color(1.5, 0, 0)
        c2 = color(0, 0.5, 0)
        c3 = color(-0.5, 0, 1)

        c[0, 0] = c1
        c[2, 1] = c2
        c[4, 2] = c3
        ppm = c.to_PPM()

        ppm_last_3_lines = '\n'.join(ppm.splitlines()[-3:])
        expected = '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'

        self.assertEqual(ppm_last_3_lines, expected,
                         'The generated PPM is not what expected')
Пример #7
0
This file draws the shadow of the sphere in the canvas
'''

from raytrace.util import color, point, vector
from raytrace.ray import ray
from raytrace.canvas import canvas
from raytrace.objects import sphere, intersect, hit
from raytrace.transformation import translation, scaling
from raytrace.light import Material, PointLight

CANVAS_DISTANCE = 100
CANVAS_H = 1000
CANVAS_W = 1000
HIT_COLOR = color(1, 0, 0)

c = canvas(CANVAS_H, CANVAS_W)

background_c = color(0.9, 0.9, 0.9)
c.set_one_color(background_c)

with open('shadow_sphere.ppm', 'w') as f:
    f.write(c.to_PPM())

print('blank canvas wrote')

pad_x = -CANVAS_W / 2
pad_y = -CANVAS_H / 2

light_position = point(-10, 10, -10)
light_color = color(1, 1, 1)
light = PointLight(light_position, light_color)
Пример #8
0
ticks = [2* n * pi/div for n in range(div) ]

points = [ rotation_z(tick) * p for tick in ticks ]

canvas_list = {(round(pos.x), round(pos.y)) for pos in points}

min_x = min(canvas_list,key=lambda loc: loc[0])[0]
pad_x = 0 if min_x >= 0 else - min_x

min_y = min(canvas_list, key= lambda loc: loc[1])[1]
pad_y = 0 if min_y >=0 else - min_y

max_x = max(canvas_list, key = lambda loc: loc[0])[0] + 1 + pad_x
max_y = max(canvas_list, key = lambda loc: loc[1])[1] +1 + pad_y

canvas_list_padded = [(x+pad_x,max_y -1-y-pad_y) for x,y in canvas_list]

c = canvas(max_x, max_y)

background_c = color(0.9,0.9,0.9)
c.set_one_color(background_c)

for pt in canvas_list_padded:
    c[pt] = color(1,0,0)

ppm = c.to_PPM()

with open('clock.ppm','w') as f:
    f.write(ppm)