I was highly inspired by this article: https://www.redblobgames.com/grids/hexagons/ and this repo is mostly just C# implementation of it. Recommended for reading. Here I will describe some technical details regarding implementation.
To use hexagonal grids you need to create HexagonalGrid and initialize it with grid type and inscribed radius of hex.
var grid = new HexagonalGrid(HexagonalGridType.PointyEven, 1.0f);
Grids layouts and orientations are merged to one enum HexagonalGridType.
- PointyOdd - Horizontal layout shoves odd rows right [odd-r]
- PointyEven - Horizontal layout shoves even rows right [even-r]
- FlatOdd - Vertical layout shoves odd columns down [odd-q]
- FlatEven - Vertical layout shoves even columns down [even-q]
There is three coordinates systems represented in lib:
Note: there is no Doubled coordinates yet. I will add them in later versions.
Struct HexagonalGrid contains a bunch of methods for coordinates conversion (all details are described in original article):
var offsetFromCubic = grid.ToOffset(cubic);
var offsetFromAxial = grid.ToOffset(axial);
var cubicFromOffset = grid.ToCubic(offset);
var cubicFromAxial = grid.ToCubic(axial);
var axialFromOffset = grid.ToAxial(offset);
var axialFromCubic = grid.ToAxial(cubic);
There is the order of neighbors of hex:
You can take a neighbor of any hex by providing a coordinate of hex and required neighbor index.
var oNeighbor = grid.GetNeighbor(offset, neighborIndex);
var cNeighbor = grid.GetNeighbor(cubic, neighborIndex);
var aNeighbor = grid.GetNeighbor(axial, neighborIndex);
The neighbor index can be negative or greater than 5 (it will be normalized)
Or you can take all neighbors of particular hex
var oNeighbors = grid.GetNeighbors(offset);
var cNeighbors = grid.GetNeighbors(cubic);
var aNeighbors = grid.GetNeighbors(axial);
There is also option to check are two hexes neighbors or not:
var isNeighbors1 = grid.IsNeighbors(offset, oNeighbor);
var isNeighbors2 = grid.IsNeighbors(cubic, cNeighbor);
var isNeighbors3 = grid.IsNeighbors(axial, aNeighbor);
You also can get ring of neighbor for particular hex
var oNeighbors = grid.GetNeighborsRing(offset, radius);
var cNeighbors = grid.GetNeighborsRing(cubic, radius);
var aNeighbors = grid.GetNeighborsRing(axial, radius);
Same as ring but you will receive hexes from all rings
var oNeighbors = grid.GetNeighborsAround(offset, radius);
var cNeighbors = grid.GetNeighborsAround(cubic, radius);
var aNeighbors = grid.GetNeighborsAround(axial, radius);
There is also list of methods to convert hex coordinate to its center (point represented as tuple):
var pointFromOffset = grid.ToPoint2(offset);
var pointFromCubic = grid.ToPoint2(cubic);
var pointFromAxial = grid.ToPoint2(axial);
To convert point into a hex coordinate which contains this point you can use:
var offsetFromPoint = grid.ToOffset(x, y);
var cubicFromPoint = grid.ToCubic(x, y);
var axialFromPoint = grid.ToAxial(x, y);
Hexagons have 6 sides and 6 corners. There is corners order of the hexes:
To take them in code simly use:
var cornerFromOffset = grid.GetCorner(offset, cornerIndex);
var cornerFromCubic = grid.GetCorner(cubic, cornerIndex);
var cornerFromAxial = grid.GetCorner(axial, cornerIndex);
The corner index can be negative or greater than 5 (it will be normalized)