Skip to Main Content
Mazes for Programmers
book

Mazes for Programmers

by Jamis Buck
July 2015
Intermediate to advanced content levelIntermediate to advanced
286 pages
6h 31m
English
Pragmatic Bookshelf
Content preview from Mazes for Programmers

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Exercises for Programmers

Exercises for Programmers

Brian P. Hogan

Publisher Resources

ISBN: 9781680501315Errata Page