Hexagon grid coordinate system

39.6k Views Asked by At

If I had a grid of squares, they can be labeled with Cartesian coordinates such that given square $(x,y)$, you know it shares a boundary with squares $(x+1,y),(x-1,y),(x,y+1),(x,y-1)$.

Is there a way of labeling a tessellated hexagon grid, so that given any hexagon label you can work out it's neighbors?

2

There are 2 best solutions below

0
On

Create the grid by drawing horizontal lines thru ALL hexagon centers and drawing vertical lines thru ALL hexagon centers. The neighbors are:

(+1,-1), (+2,0), (+1,+1), (-1,+1), (-2,0), (-1,-1)

Note: the x-axis is scaled by a factor of sqrt(3). This is ok. Cartesian coordinates can have an axis that is scaled.

Note: the x,y does not match the hexagon label. This is ok.

If we set the upper left hexagon with a coordinate of (0,0), the hexagon label is: Math.floor(x/2)+1 plus y+1

(0,0) => 0101 (2,0) => 0201 (4,0) => 0301 (1,1) => 0102 (3,1) => 0202 (5,1) => 0302

0
On

While I use redblobgames' hexagons page for all sorts of things hexagonal, there is a simple 'other way', very similar to the cartesian neighbour calculation. The normal way we imagine bricks (stretcher bond) has the same relationship as a hex-grid.
Recognising that bricks on every other row have a horizontal offset, we count (along the rows) using +2 increments instead of +1.

This gives us our x values as following

3:0 2 4 6 8
2: 1 3 5 7
1:0 2 4 6 8
0: 1 3 5 7
  012345678

Whereas y values are the same as cartesian. Now we can work out our neighbours:

For any brick/hex (x, y) where (x+y)%2==1 the neighbours are (x-2,y),(x+2,y),(x-1,y+1),(x+1,y+1),(x-1,y-1),(x+1,y-1)

Placing a hex (x,y) into an approximate cartesian position can be done by (x+y%2,y) (where % is the modulo operation)

If we want to label without using +2 increments, we can still do that, but we need to use the modulo operation for calculating neighbours.

3:0 1 2 3 4
2: 1 2 3 4
1:0 1 2 3 4
0: 1 2 3 4
  0-1-2-3-4 y%2=1
  -1-2-3-4- y%2=0

Now we can work out our neighbours: For any brick/hex (x, y) the neighbours are (x-1,y),(x+1,y),(x,y+1),(x+2(y%2)-1,y+1),(x,y-1),(x+2(y%2)-1,y-1).