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

Displaying a Hex Grid

To display a hex grid, we need to be able to compute the coordinates of each of its corners, or vertices. We’ll see how to compute those coordinates relative to the center of each hexagon, as well as how to compute the overall dimensions of a hex grid, and then we’ll plug that all into a new to_png implementation.

We’re going to assume that our grid is composed of regular hexagons—hexagons whose sides are all the same length. With that assumption, there is a lovely little derivation involving equilateral triangles (triangles whose sides are all equal) that lets us get the measurements we need. For the sake of brevity, we’ll skip the derivation itself here, but if you’re into geometry at all it’s pleasantly straightforward. ...

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