Sternhalma.Hex (Sternhalma v0.1.1) View Source

Link to this section Summary

Types

t()

Represents x z y.

Functions

Return Hex coordinate for a given pixel coordinate {x, y}.

Return the next Hex coordinate based on a provided direction.

Return the surrounding Hex coordinates.

Return a new Hex struct with coordinates x, z, and y. Return nil if the coordinates provided do not add to 0.

Return {x, y} pixel coordinates for a given Hex coordinate.

Link to this section Types

Specs

direction() ::
  :top_left | :top_right | :left | :right | :bottom_left | :bottom_right

Specs

t() :: %Sternhalma.Hex{x: number(), y: number(), z: number()}

Represents x z y.

The coordinates x, z, and y must add to 0 in some way.

See https://www.redblobgames.com/grids/hexagons/#coordinates-cube for more info.

Link to this section Functions

Specs

from_pixel({number(), number()}) :: t()

Return Hex coordinate for a given pixel coordinate {x, y}.

Examples

iex> from_pixel({8.267949192431123, 4.0})
%Sternhalma.Hex{x: 1, y: 3, z: -4}

Specs

neighbor(t(), direction()) :: t()

Return the next Hex coordinate based on a provided direction.

Examples

iex> neighbor(Sternhalma.Hex.new({1, -4, 3}), :top_left)
%Sternhalma.Hex{x: 0, y: 3, z: -3}

Specs

neighbors(t()) :: [{direction(), t()}]

Return the surrounding Hex coordinates.

Examples

iex> neighbors(Sternhalma.Hex.new({1, -4, 3}))
[
  top_left: %Sternhalma.Hex{x: 0, y: 3, z: -3},
  top_right: %Sternhalma.Hex{x: 1, y: 2, z: -3},
  left: %Sternhalma.Hex{x: 0, y: 4, z: -4},
  right: %Sternhalma.Hex{x: 2, y: 2, z: -4},
  bottom_left: %Sternhalma.Hex{x: 1, y: 4, z: -5},
  bottom_right: %Sternhalma.Hex{x: 2, y: 3, z: -5}
]

Specs

new({number(), number(), number()}) :: nil | t()

Return a new Hex struct with coordinates x, z, and y. Return nil if the coordinates provided do not add to 0.

Specs

to_pixel(t()) :: {number(), number()}

Return {x, y} pixel coordinates for a given Hex coordinate.

Examples

iex> to_pixel(Sternhalma.Hex.new({1, -4, 3}))
{8.267949192431123, 4.0}