hexgrid v2.0.0 HexGrid.Hex

Hex Tile module. See this excellent article for reference:

http://www.redblobgames.com/grids/hexagons/implementation.html

Summary

Types

t()

Hex Tile

Functions

Adds two hexes together

Gets a hex with a given direction. Allowed values are 0-5, inclusive

Calculates the distance between two hexes

Gets the length of a hex

Multiples hex by scalar

Gets the neighbour of the hex

Gets all hexes within a certain distance of the given hex

Gets all hexes neighbours

Creates a new hex tile

Creates a new hex tile Throws an ArgumentError if q + r + s != 0

Subtracts two hexes

Types

t()
t

Hex Tile

Functions

add(first, second)
add(t, t) :: t

Adds two hexes together

Examples

iex> Hex.add(Hex.new!(0, 1, -1), Hex.new!(0, 1, -1)) %Hex{q: 0, r: 2, s: -2}

cube_direction(dir)
cube_direction(integer) :: t | :error

Gets a hex with a given direction. Allowed values are 0-5, inclusive.

0 is hex immediately to the right. As the value increases the direction vector rotates counter-clockwise.

Examples

iex> Hex.cube_direction(0) %Hex{q: 1, r: -1, s: 0}

iex> Hex.cube_direction(1) %Hex{q: 1, r: 0, s: -1}

iex> Hex.cube_direction(6) :error

distance(first, second)
distance(t, t) :: integer

Calculates the distance between two hexes

Examples

iex> Hex.distance(Hex.new!(0, 0, 0), Hex.new!(0, 1, -1)) 1

iex> Hex.distance(Hex.new!(0, 0, 0), Hex.new!(1, 1, -2)) 2

iex> Hex.distance(Hex.new!(0, 0, 0), Hex.new!(-1, 5, -4)) 5

length(hex)
length(t) :: integer

Gets the length of a hex

Examples

iex> Hex.length(Hex.new!(0, 0, 0)) 0

iex> Hex.length(Hex.new!(0, 1, -1)) 1

mul(hex, scalar)
mul(t, integer) :: t

Multiples hex by scalar

Examples

iex> Hex.mul(Hex.new!(0, 1, -1), 2) %Hex{q: 0, r: 2, s: -2}

neighbour(hex, dir)
neighbour(t, integer) :: t

Gets the neighbour of the hex

Examples

iex> Hex.neighbour(Hex.new!(0, 0, 0), 0) %Hex{q: 1, r: -1, s: 0}

iex> Hex.neighbour(Hex.new!(3, -3, 0), 1) %Hex{q: 4, r: -3, s: -1}

neighbourhood(hex, distance)
neighbourhood(t, integer) :: [t]

Gets all hexes within a certain distance of the given hex

Examples

iex> Hex.neighbourhood(Hex.new!(0, 1, -1), 0) [ %Hex{q: 0, r: 1, s: -1}, ]

iex> Hex.neighbourhood(Hex.new!(0, 1, -1), 2) [ %HexGrid.Hex{q: -2, r: 3, s: -1}, %HexGrid.Hex{q: -1, r: 0, s: 1}, %HexGrid.Hex{q: -1, r: 1, s: 0}, %HexGrid.Hex{q: -1, r: 2, s: -1}, %HexGrid.Hex{q: -1, r: 3, s: -2}, %HexGrid.Hex{q: 0, r: -1, s: 1}, %HexGrid.Hex{q: 0, r: 0, s: 0}, %HexGrid.Hex{q: 0, r: 1, s: -1}, %HexGrid.Hex{q: 0, r: 2, s: -2}, %HexGrid.Hex{q: 0, r: 3, s: -3}, %HexGrid.Hex{q: 1, r: -1, s: 0}, %HexGrid.Hex{q: 1, r: 0, s: -1}, %HexGrid.Hex{q: 1, r: 1, s: -2}, %HexGrid.Hex{q: 1, r: 2, s: -3}, %HexGrid.Hex{q: 2, r: -1, s: -1}, %HexGrid.Hex{q: 2, r: 0, s: -2}, %HexGrid.Hex{q: 2, r: 1, s: -3}]HexGrid.Hex{q: 2, r: 0, s: -2}, %HexGrid.Hex{q: 2, r: 1, s: -3}]

neighbours(hex)
neighbours(t) :: [t]

Gets all hexes neighbours

Examples

iex> Hex.neighbours(Hex.new!(0, 0, 0)) [ %Hex{q: 1, r: 0, s: -1}, %Hex{q: 0, r: 1, s: -1}, %Hex{q: -1, r: 1, s: 0}, %Hex{q: -1, r: 0, s: 1}, %Hex{q: 0, r: -1, s: 1} ]

new(q, r, s)
new(number, number, number) :: t
new(number, number, number) :: {:ok, t} | {:error, String.t}

Creates a new hex tile

Examples

iex> Hex.new(0, 1, -1) {:ok, %Hex{q: 0, r: 1, s: -1}}

iex> Hex.new(0, 1, 1) {:error, “Invalid coordinates in hex given, coordinate scalars q, r and s in %Hex{q:0, r:1, s:1} do not sum to 0”}

new!(q, r, s)

Creates a new hex tile Throws an ArgumentError if q + r + s != 0

Examples

iex> Hex.new!(0, 1, -1) %Hex{q: 0, r: 1, s: -1}

iex> Hex.new!(0, 1, 1) ** (ArgumentError) Invalid coordinates in hex given, coordinate scalars q, r and s in %Hex{q:0, r:1, s:1} do not sum to 0

sub(first, second)

Subtracts two hexes

Examples

iex> Hex.sub(Hex.new!(0, 0, 0), Hex.new!(0, 1, -1)) %Hex{q: 0, r: -1, s: 1}