The outstanding questions around this revolve around scale and rotation. To preserve the same orientation between hexagons and the underlying grid and align all of the different grid parts with integer coordinates, address the hexagons as described at https://www.redblobgames.com/grids/hexagons/but scale each coordinate by 6. Considering a rendering with +q east, +r southwest, +s northwest
The neighbors (q,r,s) of (0,0,0) are:
(6,-6,0) //northeast
(0,-6,6) //north
(-6,0,6) //northwest
(-6,6,0) //southwest
(0,6,-6) //south
(6,0,-6) //southeast
The diagonals, accordingly, are at:
(12,-6,-6) //east
(6,-12,6) //northeast
(-6,-6,12) //northwest
(-12,6,6) //west
(-6,12,-6) //southwest
(6,6,-12) //southeast
The borders are halfway between the centers, so:(3,-3,0)
(0,-3,3)
(-3,0,3)
(-3,3,0)
(0,3,-3)
(3,0,-3)
The corners come out to one third the distance to the diagonals:
(4,-2,-2)
(2,-4,2)
(-2,-2,4)
(-4,2,2)
(-2,4,-2)
(2,2,-4)
Please just take my word for it that all the algorithms still work, with tweaking. I don't intend to prove it here because I haven't written the tweaks yet myself.
This is all nice and neat, but the astute reader will note that there are also integer triplets that are valid points within each hex that are neither the center of a face, nor a corner, nor a border. In this system there are 30 in each hexagon. Is this actually a problem? I don't mind a sparse hash table. Your storage option might not work best in these circumstances. There is a balance. By giving integer coordinates to centers and (corners OR borders) you can reduce the number of these "empty" points. We can also get good savings by loosening a constraint and allowing the hexagon grid to rotate relative to the underlying coordinate system. If you have a trustworthy way of storing rational numbers, it might make more sense for you to store the coordinates as fractions. You could probably store fractional coordinates as IEEE754 numbers or strings if you wanna deal with that.
I have more thoughts! And housework to do! so more later!
#hexgrid #hexagons