Hexagons are great for game grids because, compared to the standard square grid, the distance from the center of a hex to any adjacent hex is the same (assuming you have a “Normal” hex with width to height ratio of 2:√3). In a square grid, moving from one square to an adjacent diagonal square is further away then moving horizontally or vertically to an adjacent square.

The mathematics and programming of Hexagons for a game grid can get a bit more complex than the math and programming for squares. You have to create polygons instead of rectangles, when you create a grid of hexagons you have to offset each row by half the width of the hex, pathfinding can be more complex, and hit detection is more complex.

There are some choices you have to make when creating your hex grid:

How big are my hexagons going to be?

Are my hexagons going to be symmetric? Meaning the width is equal to the height (ratio 1:1, or square). Or will I choose some other ratio of width to height for my hexagons? A “Regular Hexagon” is quite common since all the angles and side lengths are equal (consequently the width to height ratio of regular hexagons turns out to be 2/√3).

The key variables we need to know before we are ready to draw a hexagon are:

Width (w), Height (h), and side length (z). We will also define x and y to be the other sides of the triangle made using the top left angled side as shown in this image:

#### Width and Height are known

Let’s say we know we want our hexes to be a certain size, so we know the width and height (maybe 100×100), but we also need to know z, the length of each side.

We know from the Pythagorean theorem that we have:

Also, from the above image you can see that there is a relationship between x, z and the width, and y and the height, specifically:

and

Now we should be able to solve for z (our side length).

Plugging x and y back into pythagoras we get:

After expanding and simplifying:

Remember that w and h are constants we have already chosen, so we can solve this using the quadratic formula:

**Quadratic formula**

where

In our case:

Thus, to get z (the hex side length from the width and height we can use this formula:

#### Side Length and Ratio are known

Now let’s say instead of knowing the width and height for our hexagons, we know we want a certain side length z (say 100) and a certain width to height ratio r (say of 4:3, or 1.3).

We can still use our formulas from above, pythagoras and our formulas relating x, y and z to the width and height. Now since we don’t know the exact width and height, we’ll substitute that ratio (r) variable for w and h:

And using our equations from above:

Solving for y:

Now we’ll substitute our value for y into pythagoras:

After simplifying and turning this into quadratic form:

Now for the quadratic we have:

If we substitute this into the quadratic, it looks pretty ugly:

#### Other Great Hex for Game Programming Resources:

Amit Patel’s Thoughts on Grids

PlayTechs: Programming for fun Hex Grids

Hexnet.org’s Introduction to Hexagonal Geometry

#### Can you do this for me? My HTML5 Hexagon Tools

If you are like me, you skim over all these equations and just want to get to the good stuff, like concrete examples. So I’ve put together a simple form to do these calculations for you using JavaScript. Once you fill out the form you should see a visual representation of the computed hexagon, drawn on a canvas (assuming you have an HTML5 enabled browser).

### Hex Based on Side Length

Side Length (z):

Ratio of Width:Height (4:3, r = 1.333), (2/√3, r = 1.1547…)

### Hex Based on Width and Height

Width:

Height:

#### Source Files

If you want to take a look at the JavasScript for this here are my source files, you are welcome to use these for your own projects:

HexCalcs.js

HexagonTools.js

HexCalcs.js is mostly just for parsing the forms above and performing the math described in this post. HexagonTools.js defines a Hexagon class that defines all the points in a hexagon and defines a draw method for the canvas.

You can also find the entire HexagonTools library code on GitHub.

In a future post I’ll show how to use the Hexagon class to build a Hex grid.