Implementing a Hex Grid

So far we’ve made regular grids and circular grids. Our next goal is to create a grid of hexagons, also called a hex grid for short. We’ll approach this by first considering a single cell, with an eye to understanding how it relates spatially to its neighbors. From there, we’ll take that information and implement the grid itself.

The cells aren’t difficult to implement. The trickiest part is just understanding how they all fit together. Let’s look at a simple hex grid here:

images/hex-grid.png

From that, we can see that each hexagon neighbors up to six other hexagons, one for each of north, south, northwest, northeast, southwest, and southeast. ...

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.