hexgrid v2.0.0 HexGrid.Hex
Hex Tile module. See this excellent article for reference:
http://www.redblobgames.com/grids/hexagons/implementation.html
Summary
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
Hex Tile
Functions
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}
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
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
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
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}
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}]
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} ]
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”}