July 2015
Intermediate to advanced
286 pages
6h 31m
English
The to_png method we’ve used to this point isn’t going to help us much with polar grids, so let’s just subclass Grid and rewrite to_png to be what we need it to be.
Create a new file, named polar_grid.rb. We’ll start it off with the following Grid subclass and to_png implementation.
| polar_grid.rb | |
| Line 1 | require 'grid' |
| - | |
| - | class PolarGrid < Grid |
| - | def to_png(cell_size: 10) |
| 5 | img_size = 2 * @rows * cell_size |
| - | |
| - | background = ChunkyPNG::Color::WHITE |
| - | wall = ChunkyPNG::Color::BLACK |
| - | |
| 10 | img = ChunkyPNG::Image.new(img_size + 1, img_size + 1, background) |
| - | center = img_size / 2 |
| - | |
| - | each_cell do |cell| |
| - | theta = 2 * Math::PI / @grid[cell.row].length |
| 15 | inner_radius = cell.row * cell_size |
| - | outer_radius ... |