Example #1
0
 def render(self, world: World):
     image = Canvas(self.hsize, self.vsize)
     for y in range(self.vsize):
         print(y)
         for x in range(self.hsize):
             ray = self.ray_for_pixel(x, y)
             color = world.color_at(ray)
             image.write_pixel(x, y, color)
     return image
Example #2
0
 def test_line_length(self):
     canvas = Canvas(10, 2, Color(1, 0.8, 0.6))
     ppm = canvas.to_ppm()
     pixel_data = [s.strip() for s in ppm.splitlines()][3:7]
     expected_data = [
         "255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204",
         "153 255 204 153 255 204 153 255 204 153 255 204 153",
         "255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204",
         "153 255 204 153 255 204 153 255 204 153 255 204 153",
     ]
     self.assertEqual(pixel_data, expected_data)
Example #3
0
def test_canvas():
    c = Canvas(10, 20)
    assert c.width == 10
    assert c.height == 20
    for row in c.pixels:
        for pixel in row:
            assert pixel == Color(0, 0, 0)
Example #4
0
 def test_creation(self):
     canvas = Canvas(10, 20)
     self.assertEqual(canvas.width, 10)
     self.assertEqual(canvas.height, 20)
     black = Color(0, 0, 0)
     for row in canvas.pixels:
         for elem in row:
             self.assertEqual(elem, black)
Example #5
0
def test_ppm():
    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)
    assert (
        "255 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 128 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 255"
        in c.to_ppm()
    )
Example #6
0
 def test_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()
     pixel_data = [s.strip() for s in ppm.splitlines()][3:6]
     expected_data = [
         "255 0 0 0 0 0 0 0 0 0 0 0 0 0 0",
         "0 0 0 0 0 0 0 128 0 0 0 0 0 0 0",
         "0 0 0 0 0 0 0 0 0 0 0 0 0 0 255",
     ]
     self.assertEqual(pixel_data, expected_data)
Example #7
0
def test_newline():
    c = Canvas(5, 3)
    assert c.to_ppm().endswith("\n")
Example #8
0
def test_long_ppm():
    c = Canvas(10, 2, Color(1.0, 0.8, 0.6))
    assert (
        "255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204\n153 255 204 153 255 204 153 255 204 153 255 204 153\n255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204\n153 255 204 153 255 204 153 255 204 153 255 204 153"
        in c.to_ppm()
    )
Example #9
0
def test_header():
    c = Canvas(5, 3)
    assert c.to_ppm().startswith("P3\n5 3\n255\n")
Example #10
0
def test_write_canvas():
    c = Canvas(10, 20)
    red = Color(1, 0, 0)
    c.write_pixel(2, 3, red)
    assert c.read_pixel(2, 3) == red
Example #11
0
 def file_terminated_by_new_line(self):
     canvas = Canvas(5, 3)
     ppm = canvas.to_ppm()
     self.assertTrue(ppm.endswith("\n"))
Example #12
0
 def test_ppm_headers(self):
     canvas = Canvas(5, 3)
     ppm = canvas.to_ppm()
     header = [s.strip() for s in ppm.splitlines()][0:3]
     expected_header = ["P3", "5 3", "255"]
     self.assertEqual(header, expected_header)
Example #13
0
 def test_writepixel(self):
     canvas = Canvas(10, 20)
     red = Color(1, 0, 0)
     canvas.write_pixel(2, 3, red)
     self.assertEqual(canvas.pixel_at(2, 3), red)
     self.assertNotEqual(canvas.pixel_at(2, 4), red)