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

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.