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. ...
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.
Read now
Unlock full access