Displaying a Hex Grid
Explore how to compute the corner points of regular hexagons and calculate overall canvas dimensions in order to display hexagonal grids. Understand the geometric relationships and code implementations that enable rendering hex grids and mazes with precise coordinates for each cell.
We'll cover the following...
Displaying a hexagon grid using corner coordinates
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 small derivation involving equilateral triangles (triangles whose sides are all equal) that lets us get the measurements we need. In the context of this course, we’ll skip the derivation itself, but you are encouraged to try it yourself.
Essentially, what we want are lengths of