Hexagonal maps

Filed under: Programming · Date: Wed Oct 6 16:53:27 2004

Hexagonal map showing coordinate system and the two axisWorking with hexagonal maps is not much different to working with square grid maps. Look at the map to the right. It shows a hexagonal grid with labeled hexes. The labels tell the coordinates of each of the hex. Hexes with yellow background mark the X-coordinate; blue background marks Y-coordinate. You can rotate the image if it helps you understand the coordinate system.

Note that each hex on a row in alignment with the yellow row, have the same value for their X-coordinate. And likewise, each hex on a row in alignment with the blue row have the same value for their Y-coordinate.

Calculating distance

Calculating distance on hexagonal map is not difficult. The following pseudocode describes how the calculation is done between to hexes A and B.

dX = B.x - A.x
dY = B.y - A.y
distance = (abs (dX) + abs (dY) + abs (dX - dY)) / 2

And here's an example. A is 0:2 and B is -2:-2. Now lets put this to the equation: dX = -2 - 0 = -2; dY = -2 - 2 = -4; distance = (abs(-2) + abs(-4) + abs(-2 - -4)) / 2 = (2 + 4 + 2) / 2 = 8 / 2 = 4. From the map above you can see the distance of these two points is 4 hexes, which proves our example right.

More information

For more math on hexagonal maps, and other interesting reads, go to Amit Patel's game programming page. This coordinate model was originally suggested by Lars Jensen.

Comments

1
From Yome · Written on Jan 08, 2008 at 20:54

Hi,

This algo does not works (my hex is vertically aligned…

2
From Yome · Written on Jan 08, 2008 at 22:18

..i’ve also tryed it with the hexspace transformation…

3
From Frank · Written on Feb 06, 2008 at 22:24

You have to re-orientate your axxis within the calculation, too:

distance = -(abs(dx) + abs(dy) + abs(dx + dy)) / 2


Comments are disabled for this post.