Voronchuk / hexagons / Hexagons.Layout

The next major piece of functionality we need is a way to convert between hex coordinates and screen coordinates. There’s a pointy top layout and a flat top hex layout. The conversion uses a matrix as well as the inverse of the matrix, so we need a way to store those. Also, for drawing the corners, pointy top starts at 30° and flat top starts at 0°, so we need a place to store that too.

See http://www.redblobgames.com/grids/hexagons/implementation.html for reference.

Types


type alias Point =
( Basics.Float, Basics.Float )

Point on screen (pixel)


type alias Orientation =
{ forward_matrix : Square2Matrix
, inverse_matrix : Square2Matrix
, start_angle : Basics.Float 
}

Orientation helper type to store these: the 2×2 forward matrix, the 2×2 inverse matrix, and the starting angle


type alias Layout =
{ orientation : Orientation
, size : Point
, origin : Point 
}

Composite layout definition

Constants

orientationLayoutPointy : Orientation

Constant definition of pointy hexagon orientation

orientationLayoutFlat : Orientation

Constant definition for flat-top hexagon orientation

Hex to point and point to hex conversions

hexToPoint : Layout -> Hexagons.Hex.Hex -> Point

Turn Hex coordinates into a Point location on a Layout

pointToHex : Layout -> Point -> Hexagons.Hex.Hex

Turn Point coordinates on a Layout into a Hex coordinates

Hex to offset and offset to hex coordinate conversions

hexToOffset : Hexagons.Hex.Hex -> OffsetCoord

Convert Hex coordinates to offset

offsetToHex : OffsetCoord -> Hexagons.Hex.Hex

Convert offset coordinates to hex

Hex corner coordinates

polygonCorners : Layout -> Hexagons.Hex.Hex -> List Point

Once we know where the corners are relative to the center, we can calculate the corners in screen locations by adding the center to each corner, and putting the coordinates into a list.

Drawing

drawLine : Hexagons.Hex.Hex -> Hexagons.Hex.Hex -> List Hexagons.Hex.Hex

Draw the line between hexes using the linear interpolation

drawCircle : Hexagons.Hex.Hex -> Basics.Int -> List Hexagons.Hex.Hex

Draw the circle of a defined redius with the hex in a center