Working with the dual graph of a hex grid

Started by
4 comments, last by Ninfur 1 year, 8 months ago

I have a pointy topped hex grid based on cube coordinates, created by following this great guide from Red Blob Games: https://www.redblobgames.com/grids/hexagons/.

I know that the dual graph of a hex grid, a triangle grid, can be formed by treating the center of each hexagon as a vertex of a triangle. I want to use this fact to draw a terrain on the triangular grid, based on the hex grid, as explained in this fantastic talk by Oskar Stålberg (at 7:00):

However, I'm struggling with how to “access” the dual grid. More specifically, how can I iterate over every triangle in the dual grid and also get the 3 hexagons it is formed by, without visiting the same triangles multiple times?

Do I create and store the two grids as separate data structures in code, or do I derive the dual grid from the hex grid?

A naive way would be to iterate over all hexagon, and get each hexagon's 6 cartesian corner coordinates, which is also the center position of each triangle face. Then based on the face position find the cube coordinates of the 3 closest hexagons. But that means it will visit every triangle 3 times, which seems unnecessary.

Advertisement

Just to be sure, you’re doing this on a plane and not on a sphere?

@taby Yes, on a plane.

Never done such thing, but i would form a square grid, so each square has two triangles inside and 4 hexagons at its corners.
Sounds efficient traversal isn't that hard to figure out, and there would be two variants:

My ‘square grid’ would be the dual of the memory representation on the right, if that's the convention you use.

@JoeJ Thanks for the suggestion.

I went with a slightly different approach for now. I manage to figure out the relation between the hexagon grid's cube coordinates and triangle grid's 3-axis coordinates (which is basically the same coordinate system). That way I can ask a hexagon for it's surrounding 6 triangle coordinates, or a triangle for it's 3 surrounding hexagon coordinates. Then I just iterate over every hexagon in the grid, and add each unique triangle coordinate to a list that I keep as my triangle grid. To check if a triangle is pointing up or down I just check if the sum of the triangle coordinates is 1 or 2.

This topic is closed to new replies.

Advertisement