Drawing Polar Grids

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)
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 ...

Get Mazes for Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.