O'Reilly logo

Mazes for Programmers by Jamis Buck

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required