Voronchuk/hexagons/1.0.3

NOTE: this package is not compatible with Elm 0.19.1

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 =
( Float, Float )

Point on screen (pixel)

type alias Orientation =
{ forward_matrix : ( Float, Float, Float, Float )
, inverse_matrix : ( Float, Float, Float, Float )
, start_angle : 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

Contants

Contant definition of pointy hexagon orientation

Hex to point and point to hex conversions

hexToPoint : Layout -> Hex -> Point

Turn Hex coordinates into a Point location on a Layout

pointToHex : Layout -> Point -> Hex

Turn Point coordinates on a Layout into a Hex coordinates

Hex corner coordinates

polygonCorners : Layout -> 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 : Hex -> Hex -> List Hex

Drawing the line between hexes using the linear interpolation