Class HexMath
Hexagon math helper class. Coordinate conversions and hexagon geometry calculations. Supports axial, cube and offset coordinates. Supports flat top and pointy top hexagons. Supports odd and even layouts.
Inherited Members
Namespace: Fwt.Core
Assembly: fwt.core.dll
Syntax
[BurstCompile]
public static class HexMath
Fields
Directions_Axial
All directions for axial coordinates
Declaration
public static readonly FixedArray8<int2> Directions_Axial
Field Value
| Type | Description |
|---|---|
| FixedArray8<int2> |
Directions_Cube
All directions for cube coordinates
Declaration
public static readonly FixedArray8<int3> Directions_Cube
Field Value
| Type | Description |
|---|---|
| FixedArray8<int3> |
Directions_Offset_Even_Flat
All directions for even layout flat top hexagons
Declaration
public static readonly FixedArray16<int2> Directions_Offset_Even_Flat
Field Value
| Type | Description |
|---|---|
| FixedArray16<int2> |
Directions_Offset_Even_Point
All directions for even layout pointy top hexagons
Declaration
public static readonly FixedArray16<int2> Directions_Offset_Even_Point
Field Value
| Type | Description |
|---|---|
| FixedArray16<int2> |
Directions_Offset_Odd_Flat
All directions for odd layout flat top hexagons
Declaration
public static readonly FixedArray16<int2> Directions_Offset_Odd_Flat
Field Value
| Type | Description |
|---|---|
| FixedArray16<int2> |
Directions_Offset_Odd_Point
All directions for odd layout pointy top hexagons
Declaration
public static readonly FixedArray16<int2> Directions_Offset_Odd_Point
Field Value
| Type | Description |
|---|---|
| FixedArray16<int2> |
Sqrt3
Square root of 3 (used for calculations with hexagons)
Declaration
public static readonly float Sqrt3
Field Value
| Type | Description |
|---|---|
| float |
Methods
AxialToCube(in int2)
Converts axial coordinates to cube coordinates
Declaration
public static int3 AxialToCube(in int2 axialCoord)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | axialCoord | Axial coordinate to be converted |
Returns
| Type | Description |
|---|---|
| int3 | Converted cube coordinate |
AxialToPixel(in int2, in float, in HexRotations)
Converts axial coordinates to pixel coordinates (point in world)
Declaration
public static float2 AxialToPixel(in int2 axial, in float sideSize, in HexRotations hexRotation)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | axial | axial coordinate |
| float | sideSize | side length (radius) of hexagon |
| HexRotations | hexRotation | rotation of hexagon (point/flat top) |
Returns
| Type | Description |
|---|---|
| float2 | pixel coordinate (point in world) |
AxialToPixel(in int2, in float, in bool)
Converts axial coordinates to pixel coordinates (point in world)
Declaration
public static float2 AxialToPixel(in int2 axial, in float sideSize, in bool isFlatTop)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | axial | axial coordinate |
| float | sideSize | side length (radius) of hexagon |
| bool | isFlatTop | true = flat top, false = point top |
Returns
| Type | Description |
|---|---|
| float2 | pixel coordinate (point in world) |
CellCoordAdd_Cube(int3, int3)
Adds offset to the cell coordinates (cube coordinates)
Declaration
public static int3 CellCoordAdd_Cube(int3 origin, int3 offset)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | origin | origin cube coordinates |
| int3 | offset | target cube coordinates |
Returns
| Type | Description |
|---|---|
| int3 | cube coordinate |
CubeToAxial(in int3)
Converts cube coordinates to axial coordinates
Declaration
public static int2 CubeToAxial(in int3 cubeCoord)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | cubeCoord | Cube coordinate to be converted |
Returns
| Type | Description |
|---|---|
| int2 | Converted axial coordinates |
CubeToOffset(in int3, in HexRotations, in HexLayouts)
Converts cube coordinates to offset coordinates
Declaration
public static int2 CubeToOffset(in int3 cubeCoord, in HexRotations hexRotation, in HexLayouts hexLayout)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | cubeCoord | cube coordinate |
| HexRotations | hexRotation | rotation of hexagon (point/flat top) |
| HexLayouts | hexLayout | layout of hexagons (odd/even) |
Returns
| Type | Description |
|---|---|
| int2 | offset coordinate |
CubeToOffset(in int3, in bool, in bool)
Converts cube coordinates to offset coordinates
Declaration
public static int2 CubeToOffset(in int3 cubeCoord, in bool isFlatTop, in bool isOddLayout)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | cubeCoord | Cube coordinate |
| bool | isFlatTop | if true - flat top, false - point top |
| bool | isOddLayout | true - odd layout, flase - even layout |
Returns
| Type | Description |
|---|---|
| int2 | offset coordinate |
CubeToPixel(in int3, in float, in HexRotations)
Converts cube coordinates to pixel coordinates (point in world)
Declaration
public static float2 CubeToPixel(in int3 cube, in float sideSize, in HexRotations hexRotation)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | cube | cube coordinate |
| float | sideSize | side length (radius) of hexagon |
| HexRotations | hexRotation | rotation of hexagon (point/flat top) |
Returns
| Type | Description |
|---|---|
| float2 | pixel coordinate (point in world) |
DistanceBetweenCells_Axial(in int2, in int2)
Calculates distance between two cells in axial coordinates
Declaration
public static int DistanceBetweenCells_Axial(in int2 from_axial, in int2 to_axial)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | from_axial | origin axial coordinate |
| int2 | to_axial | target axial coordinate |
Returns
| Type | Description |
|---|---|
| int | Axial coordinate |
DistanceBetweenCells_Cube(in int3, in int3)
Calculates distance between two cells in cube coordinates
Declaration
public static int DistanceBetweenCells_Cube(in int3 from_cube, in int3 to_cube)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | from_cube | origin cube coordinate |
| int3 | to_cube | target cube coordinate |
Returns
| Type | Description |
|---|---|
| int | cube coordinate |
DistanceBetweenCells_Offset(in int2, in int2, in HexRotations, in HexLayouts)
Calculates distance between two cells in offset coordinates
Declaration
public static int DistanceBetweenCells_Offset(in int2 from_offset, in int2 to_offset, in HexRotations hexRotation, in HexLayouts hexLayout)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | from_offset | origin offset coordinate |
| int2 | to_offset | target offset coordinate |
| HexRotations | hexRotation | hexagon rotation (flat/point top) |
| HexLayouts | hexLayout | laout rotation (odd/even) |
Returns
| Type | Description |
|---|---|
| int | offset coordinate |
DistanceBetweenCells_Offset_F(in int2, in int2, in HexRotations, in HexLayouts)
Calculates distance between two cells in offset coordinates
Declaration
public static float DistanceBetweenCells_Offset_F(in int2 from_offset, in int2 to_offset, in HexRotations hexRotation, in HexLayouts hexLayout)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | from_offset | origin offset coordinate |
| int2 | to_offset | target offset coordinate |
| HexRotations | hexRotation | hexagon rotation (point/flat top) |
| HexLayouts | hexLayout | laoyout rotation (odd/even) |
Returns
| Type | Description |
|---|---|
| float | offset coordinate |
GetAdjacentCell_Axial(in int2, in int)
Returns adjacent cell for given cell and neighbour index
Declaration
public static int2 GetAdjacentCell_Axial(in int2 cell, in int neighbourIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | cell | axial coordinate of the cell to find neighbour for |
| int | neighbourIndex | index of neighbour (6 neighbors) |
Returns
| Type | Description |
|---|---|
| int2 | axial coordinate of the neighbour |
GetAdjacentCell_Cube(in int3, in int)
Returns adjacent cell for given cell and neighbour index
Declaration
public static int3 GetAdjacentCell_Cube(in int3 cell, in int neighbourIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | cell | cube coordinate of the cell to find neighbour for |
| int | neighbourIndex | index of neighbour (6 neighbors) |
Returns
| Type | Description |
|---|---|
| int3 | cube coordinate of the neighbour |
GetAdjacentCell_Offset(in int2, in int, in HexRotations, in HexLayouts)
Returns adjacent cell for given cell and neighbour index
Declaration
public static int2 GetAdjacentCell_Offset(in int2 cell, in int neighbourIndex, in HexRotations hexRotation, in HexLayouts hexLayout)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | cell | offset coordinate of the cell to get neighbour for |
| int | neighbourIndex | index of neighbour (6 neighbors) |
| HexRotations | hexRotation | hexagon rotation (point/flat top) |
| HexLayouts | hexLayout | layout of hexagons (odd/even) |
Returns
| Type | Description |
|---|---|
| int2 | offset coordinate of adjacent cell |
GetAdjacentCell_Offset(in int2, in int, in bool, in bool)
Returns adjacent cell for given cell and neighbour index
Declaration
public static int2 GetAdjacentCell_Offset(in int2 cell, in int neighbourIndex, in bool isOddLayout, in bool isFlatTop)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | cell | offset coordinate of the cell to get neighbour for |
| int | neighbourIndex | index of neighbour (6 neighbors) |
| bool | isOddLayout | true = odd layout, false = even layout |
| bool | isFlatTop | true = flat top, false = point top |
Returns
| Type | Description |
|---|---|
| int2 | offset coordinate of adjacent cell |
GetCellsRange_Axial(in int2, int, Allocator)
Creates a results list with given allocator, Calculates range and fills results list with them
Declaration
public static NativeList<int2> GetCellsRange_Axial(in int2 center, int rangeSize, Allocator allocator)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | center | |
| int | rangeSize | |
| Allocator | allocator |
Returns
| Type | Description |
|---|---|
| NativeList<int2> | axial coordinate |
GetCellsRange_Axial(in int2, int, NativeList<int2>)
Calculates range and fills given list with coordinates. Returns filled list
Declaration
public static NativeList<int2> GetCellsRange_Axial(in int2 center, int rangeSize, NativeList<int2> results)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | center | |
| int | rangeSize | |
| NativeList<int2> | results |
Returns
| Type | Description |
|---|---|
| NativeList<int2> | axial coordinate |
GetCellsRange_Cube(in int3, int, Allocator)
Creates a results list with given allocator, Calculates range and fills results list with them
Declaration
public static NativeList<int3> GetCellsRange_Cube(in int3 center, int rangeSize, Allocator allocator)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | center | |
| int | rangeSize | |
| Allocator | allocator |
Returns
| Type | Description |
|---|---|
| NativeList<int3> | cube coordinate |
GetCellsRange_Cube(in int3, int, NativeList<int3>)
Calculates range and fills given list with coordinates. Returns filled list
Declaration
public static NativeList<int3> GetCellsRange_Cube(in int3 center, int rangeSize, NativeList<int3> results)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | center | |
| int | rangeSize | |
| NativeList<int3> | results |
Returns
| Type | Description |
|---|---|
| NativeList<int3> | cube coordinate |
GetCellsRange_Offset(in int2, int, in HexRotations, in HexLayouts, Allocator)
Creates a results list with given allocator, Calculates range and fills results list with them
Declaration
public static NativeList<int2> GetCellsRange_Offset(in int2 center, int rangeSize, in HexRotations hexRotation, in HexLayouts hexLayout, Allocator allocator)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | center | |
| int | rangeSize | |
| HexRotations | hexRotation | |
| HexLayouts | hexLayout | |
| Allocator | allocator |
Returns
| Type | Description |
|---|---|
| NativeList<int2> | offset coordinate |
GetCellsRange_Offset(in int2, int, in HexRotations, in HexLayouts, NativeList<int2>)
Calculates range and fills given list with coordinates. Returns filled list
Declaration
public static NativeList<int2> GetCellsRange_Offset(in int2 center, int rangeSize, in HexRotations hexRotation, in HexLayouts hexLayout, NativeList<int2> results)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | center | |
| int | rangeSize | |
| HexRotations | hexRotation | |
| HexLayouts | hexLayout | |
| NativeList<int2> | results |
Returns
| Type | Description |
|---|---|
| NativeList<int2> | offset coordinate |
GetDirection_Axial(in int)
Returns direction for given cell and neighbour index
Declaration
public static int2 GetDirection_Axial(in int directionIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | directionIndex | index of neihbour to find direction towards (6 neigbours) |
Returns
| Type | Description |
|---|---|
| int2 | direction towards the adjacent cell |
GetDirection_Cube(in int)
Returns direction for given cell and neighbour index
Declaration
public static int3 GetDirection_Cube(in int directionIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | directionIndex | index of neighbour to find direction towards (6 neigbours) |
Returns
| Type | Description |
|---|---|
| int3 | cube coordinate |
GetDirection_Offset(in int2, in int, in HexRotations, in HexLayouts)
Returns direction for given cell and neighbour index
Declaration
public static int2 GetDirection_Offset(in int2 cell, in int neighbourIndex, in HexRotations hexRotation, in HexLayouts hexLayout)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | cell | offset coordinate of the cell to get neighbour for |
| int | neighbourIndex | index of neighbour (6 neighbors) |
| HexRotations | hexRotation | hexagon rotation (point/flat top) |
| HexLayouts | hexLayout | layout of hexagon (odd/even) |
Returns
| Type | Description |
|---|---|
| int2 | direction towards the adjacent cell |
GetDirection_Offset(in int2, in int, in bool, in bool)
Returns direction for given cell and neighbour index
Declaration
public static int2 GetDirection_Offset(in int2 cell, in int neighbourIndex, in bool isOddLayout, in bool isFlatTop)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | cell | cell to find a direction from |
| int | neighbourIndex | index of neighbour to find a direction towards (6 neigbours) |
| bool | isOddLayout | true = odd layout, false = even layout |
| bool | isFlatTop | true = flat top, false = point top |
Returns
| Type | Description |
|---|---|
| int2 | direction towards the adjacent cell |
GetHexGridSize(in float, in int2, in HexRotations)
Returns size of the rectangle for hexagon grid with given sideLength and hexCount
Declaration
public static float2 GetHexGridSize(in float sideLength, in int2 hexCount, in HexRotations hexRotation)
Parameters
| Type | Name | Description |
|---|---|---|
| float | sideLength | Side length (radius) of the hexagon |
| int2 | hexCount | Size of the grid of hexagons |
| HexRotations | hexRotation | hexagon rotation (point/flat top) |
Returns
| Type | Description |
|---|---|
| float2 | size of the hexagons grid |
GetHexGridSize(in float, in int2, in bool)
Returns size of the rectangle for hexagon grid with given sideLength and hexCount
Declaration
public static float2 GetHexGridSize(in float sideLength, in int2 hexCount, in bool isFlatTop)
Parameters
| Type | Name | Description |
|---|---|---|
| float | sideLength | Side length (radius) of the hexagon |
| int2 | hexCount | Size of the grid of hexagons |
| bool | isFlatTop | if true - flat top, false - point top |
Returns
| Type | Description |
|---|---|
| float2 | size of the hexagons grid |
GetHexagonSize(in float, in HexRotations)
Returns size of the rectangle for hexagon with provided sideLength
Declaration
public static float2 GetHexagonSize(in float sideLength, in HexRotations hexRotation)
Parameters
| Type | Name | Description |
|---|---|---|
| float | sideLength | Side length (radius) of the hexagon |
| HexRotations | hexRotation | Rotation of the hexagon (point/flat top) |
Returns
| Type | Description |
|---|---|
| float2 | Size of the hexagon |
GetHexagonSize(in float, in bool)
Returns size of the rectangle for hexagon with gived sideLength
Declaration
public static float2 GetHexagonSize(in float sideLenght, in bool isFlatTop)
Parameters
| Type | Name | Description |
|---|---|---|
| float | sideLenght | |
| bool | isFlatTop |
Returns
| Type | Description |
|---|---|
| float2 | size of the hexagon |
HexCornerPosition(in float2, in float, in int, in HexRotations)
returns position of hexagon's corner of given index for flat top and pointy top hexagon orientation
Declaration
public static float2 HexCornerPosition(in float2 center, in float sideSize, in int cornerIndex, in HexRotations hexRotation)
Parameters
| Type | Name | Description |
|---|---|---|
| float2 | center | center of the hexagon |
| float | sideSize | size of the side of the hexagon |
| int | cornerIndex | every next corner adds 60 degrees from starting point |
| HexRotations | hexRotation | rotation of the hexagon (point/flat top) |
Returns
| Type | Description |
|---|---|
| float2 | corner position |
HexCornerPosition(in float2, in float, in int, in bool)
returns position of hexagon's corner of given index for flat top and pointy top hexagon orientation
Declaration
public static float2 HexCornerPosition(in float2 center, in float sideSize, in int cornerIndex, in bool isFlatTop)
Parameters
| Type | Name | Description |
|---|---|---|
| float2 | center | center of the hexagon |
| float | sideSize | size of the side of the hexagon |
| int | cornerIndex | every next corner adds 60 degrees from starting point |
| bool | isFlatTop | true - hexagon with flat top; false - hexagon with pointy top |
Returns
| Type | Description |
|---|---|
| float2 | corner position |
HexCornerPosition(in float2, in float, in float)
Gives position for hexagon corner (point on circle around center) with given angle in radians
Declaration
public static float2 HexCornerPosition(in float2 center, in float size, in float angle_rad)
Parameters
| Type | Name | Description |
|---|---|---|
| float2 | center | center of the circle |
| float | size | radius of the circle |
| float | angle_rad | angle of the point in radians |
Returns
| Type | Description |
|---|---|
| float2 | corner position |
OffsetToAxial(in int2, in HexRotations, in HexLayouts)
Converts offset coordinates to axial coordinates
Declaration
public static int2 OffsetToAxial(in int2 offset, in HexRotations hexRotation, in HexLayouts hexLayout)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | offset | offset coordinate |
| HexRotations | hexRotation | hexagon rotation (point/flat top) |
| HexLayouts | hexLayout |
Returns
| Type | Description |
|---|---|
| int2 | offset coordinate |
OffsetToAxial(in int2, in bool, in bool)
Converts offset coordinates to axial coordinates
Declaration
public static int2 OffsetToAxial(in int2 offset, in bool isFlatTop, in bool isOddLayout)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | offset | |
| bool | isFlatTop | |
| bool | isOddLayout |
Returns
| Type | Description |
|---|---|
| int2 | offset coordinate |
OffsetToCube(in int2, in HexRotations, in HexLayouts)
Converts offset coordinates to cube coordinates
Declaration
public static int3 OffsetToCube(in int2 offsetCoord, in HexRotations hexRotation, in HexLayouts hexLayout)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | offsetCoord | offset coordinate |
| HexRotations | hexRotation | rotation of hexagons (point/flat top) |
| HexLayouts | hexLayout | layout of hexagons (odd/even) |
Returns
| Type | Description |
|---|---|
| int3 | cube coordinate |
OffsetToCube(in int2, in bool, in bool)
Converts offset coordinates to cube coordinates
Declaration
public static int3 OffsetToCube(in int2 offsetCoord, in bool isFlatTop, in bool isOddLayout)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | offsetCoord | offset coordinate |
| bool | isFlatTop | true = flat top, false = point top |
| bool | isOddLayout | true = odd layout, false = even layout |
Returns
| Type | Description |
|---|---|
| int3 | cube coordinate |
OffsetToPixel(in int2, in float, in HexRotations, in HexLayouts)
Converts offset coordinates to pixel coordinates (point in world)
Declaration
public static float2 OffsetToPixel(in int2 offset, in float sideSize, in HexRotations hexRotation, in HexLayouts hexLayout)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | offset | offset coordinate |
| float | sideSize | side length (radius) of hexagon |
| HexRotations | hexRotation | rotation of hexagon (point/flat top) |
| HexLayouts | hexLayout | layout of hexagon (odd/even) |
Returns
| Type | Description |
|---|---|
| float2 | pixel coordinate (point in world) |
PixelToAxial(in float2, in float, in HexRotations)
Converts pixel coordinates to axial coordinates
Declaration
public static int2 PixelToAxial(in float2 pixel, in float sideSize, in HexRotations hexRotation)
Parameters
| Type | Name | Description |
|---|---|---|
| float2 | pixel | pixel coordinate (point in the world) |
| float | sideSize | side length (radius) of hexagon |
| HexRotations | hexRotation | rotation of hexagon (point/flat top) |
Returns
| Type | Description |
|---|---|
| int2 | axial coordinate |
PixelToAxial(in float2, in float, in bool)
Converts pixel coordinates to axial coordinates
Declaration
public static int2 PixelToAxial(in float2 pixel, in float sideSize, in bool isFlatTop)
Parameters
| Type | Name | Description |
|---|---|---|
| float2 | pixel | pixel coordinate (point in the world) |
| float | sideSize | side length (radius) of hexagon |
| bool | isFlatTop | true = flat top, false = point top |
Returns
| Type | Description |
|---|---|
| int2 | axial coordinate |
PixelToCube(in float2, in float, in HexRotations)
Converts pixel coordinates to cube coordinates
Declaration
public static int3 PixelToCube(in float2 pixel, in float sideSize, in HexRotations hexRotation)
Parameters
| Type | Name | Description |
|---|---|---|
| float2 | pixel | pixel coordinate (point in the world) |
| float | sideSize | side length (radius) of hexagon |
| HexRotations | hexRotation | rotation of hexagon (point/flat top) |
Returns
| Type | Description |
|---|---|
| int3 | cube coordinate |
PixelToOffset(in float2, in float, in HexRotations, in HexLayouts)
Converts pixel coordinates to offset coordinates
Declaration
public static int2 PixelToOffset(in float2 pixel, in float sideSize, in HexRotations hexRotation, in HexLayouts hexLayout)
Parameters
| Type | Name | Description |
|---|---|---|
| float2 | pixel | pixel coordinate (point in the world) |
| float | sideSize | side length (radius) of hexagon |
| HexRotations | hexRotation | rotation of hexagon (point/flat top) |
| HexLayouts | hexLayout | layout of hexagons (odd/even) |
Returns
| Type | Description |
|---|---|
| int2 | offset coordinate |