A canvas is just a rectangular grid of pixels—much like your computer screen. Your implementation will allow its size to be configurable, so you can specify how wide and high the canvas ought to be.
Add the following test to your suite. It demonstrates how a canvas is created and shows every pixel in the canvas should be initialized to black (color(0, 0, 0)).
| Scenario: Creating a canvas |
| Given c ← canvas(10, 20) |
| Then c.width = 10 |
| And c.height = 20 |
| And every pixel of c is color(0, 0, 0) |
Pixels are drawn to the canvas by specifying a position and a color. Write the following test, introducing a function called write_pixel(canvas, x, y, color) and showing how it is used.
| Scenario: Writing pixels to a canvas |
| Given c ← canvas(10, 20) |
| And red ← color(1, 0, 0) |
| When write_pixel(c, 2, 3, red) |
| Then pixel_at(c, 2, 3) = red |
Note that the x and y parameters are assumed to be 0-based in this book. That is to say, x may be anywhere from 0 to width - 1 (inclusive), and y may be anywhere from 0 to height - 1 (inclusive).
You won’t need any other methods for writing to your canvas, since your ray tracer will work pixel-by-pixel over the entire scene. Make those tests all pass, and then we can talk about how to save this canvas to disk in a format that will actually be meaningful.
3.145.111.183