Class HexUtils
Utility functions for hex grid coordinate conversions.
public static class HexUtils
- Inheritance
-
HexUtils
- Inherited Members
Remarks
All functions use axial coordinates: the x component is the column axis and
the y component is the row axis. This matches the coordinate system used by
the Gamelogic/Fx/HexPixelate and Gamelogic/Fx/HexDitherGridBias
shaders.
Methods
GridPointToIndex(Vector2Int, int, int, int)
Converts a hex grid point to a fundamental region index, or -1 if the point is not in the canonical fundamental region.
public static int GridPointToIndex(Vector2Int gridPoint, int x0, int x1, int y1)
Parameters
gridPointVector2Intx0intx1inty1int
Returns
Remarks
Only tests membership in the canonical region (y in [0, y1), x in the corresponding column range). To test an arbitrary grid point reduced modulo the lattice, reduce it first.
GridToWorld(Vector2Int, Vector2)
Converts hex grid axial coordinates to a world position at the center of the cell.
public static Vector2 GridToWorld(Vector2Int gridPoint, Vector2 cellDimensions)
Parameters
gridPointVector2IntAxial coordinates of the hex cell.
cellDimensionsVector2The (width, height) of a single hex cell.
Returns
IndexToGridPoint(int, int, int, int)
Converts a fundamental region index to a hex grid point.
public static Vector2Int IndexToGridPoint(int index, int x0, int x1, int y1)
Parameters
indexintIndex in [0, Count) where Count = x0 * y1.
x0intx-component of basis vector A.
x1intx-component of basis vector B.
y1inty-component of basis vector B (must be > 0).
Returns
Remarks
The fundamental region is the parallelogram with basis vectors A = (x0, 0) and B = (x1, y1). It contains Count = |x0 * y1| lattice points with indices 0 .. Count-1. Rows run from y = 0 to y = y1-1; within each row y, the x0 columns start at ⌈y · x1 / y1⌉.
Assumes x0 > 0 and y1 > 0.
ReduceToFundamentalRegion(Vector2Int, int, int, int)
Reduces an arbitrary integer grid point modulo the lattice defined by basis vectors A = (x0, 0) and B = (x1, y1), returning the equivalent point in the canonical fundamental region.
public static Vector2Int ReduceToFundamentalRegion(Vector2Int point, int x0, int x1, int y1)
Parameters
pointVector2IntAny integer grid point.
x0intx-component of basis vector A (must be > 0).
x1intx-component of basis vector B.
y1inty-component of basis vector B (must be > 0).
Returns
Remarks
The canonical region has rows y ∈ [0, y1) where each row y contains the x0 columns starting at ⌈y · x1 / y1⌉ — the same layout as IndexToGridPoint(int, int, int, int).
WorldToGrid(Vector2, Vector2)
Converts a world position to hex grid axial coordinates.
public static Vector2Int WorldToGrid(Vector2 worldPoint, Vector2 cellDimensions)
Parameters
worldPointVector2Position in world space (pixels or UV-relative units).
cellDimensionsVector2The (width, height) of a single hex cell in the same units.