Hacker News new | ask | show | jobs
by danbruc 108 days ago
Hex math is weird. Since there are 6 directions instead of 4, there's no simple mapping between hex positions and 2D x,y coordinates.

There is, a hexagonal grid is isomorphic to a [skewed] rectangular grid, i.e. it can also be indexed with a coordinate pair (u, v). The neighbours are at offsets (+1, 0), (-1, 0), (0, +1), and (0, -1) - just as in a rectangular grid - and the two additional neighbours are at (+1, -1) and (-1, +1). The coordinates in the plane are (x, y) = (√3(u + v/2), 3v/2) or some variation of this, depending on how exactly one lays out the hexagons and picks axes.

It is surprisingly hard to find a good illustration for this, or maybe I am using the wrong search terms, but here [1] is the best one I could quickly find.

[1] https://www.researchgate.net/figure/Rhomboidal-and-hexagonal...

1 comments

Amit has a pretty good overview of the (many) ways to map coordinates to hexes: https://www.redblobgames.com/grids/hexagons/
That is one of the first results I found and it is probably a great resource if you want to dive into the topic, but it also lacks an image of the rectangular - or rhombic - grid on top of the hexagonal tiling which I think is visually much clearer than those interactive maps highlighting only the axes of the cell under the cursor. But I understand why they made the choice, other coordinate systems do not admit the same type of visualization while the interactive maps are universal in that sense.