Create hexagonal grids in Elixir. Provides basic computational and mapping support.
Work is based on this excellent article from redblobgames.
See hex package and docs for more details.
The package can be installed by listing it as a hex
dependency:
def deps do
[{:hexgrid, "~> 2.1"}]
end
alias HexGrid.Map, as: HexMap
alias HexGrid.Hex, as: Hex
Create a tile in a cube coordinate system.
Example:
Hex.new(0, 0, 0)
Gets the neighbuoring hex. Neighbours are just offsets on the given hex:
0 -> Hex.new(+1, -1, 0)
1 -> Hex.new(+1, 0, -1)
2 -> Hex.new(0, +1, -1)
3 -> Hex.new(-1, +1, 0)
4 -> Hex.new(-1, 0, +1)
5 -> Hex.new(0, -1, +1)
Example:
Hex.neighbours(Hex.new(0, 0, 0))
For a given hex tile, get all adjacent tiles.
Example:
Hex.neighbours(Hex.new(0, 0, 0))
Provides support for creating and maintaining tile structures, as well as containing tile data.
Note: Examples alias HexGrid.Map
as Map
Creates an empty map.
Example:
Map.new()
Utility to create a hexagonal-shaped map with a given radius.
Example:
Map.new_hex(5)
Adds tile to the map.
Example:
{:ok, map} = HexMap.new()
{result, map} = HexMap.insert(map, Hex.new(0, 0, 0))
Sets an arbitrary value on a map, for a given tile.
Example:
hex = Hex.new(0, 0, 0)
{_, map} = HexMap.new()
{_, map} = HexMap.insert(map, hex)
{_, map} = HexMap.set(map, hex, :hello, :world)
Gets the value from the map, for a given tile.
Example:
hex = Hex.new(0, 0, 0)
{_, map} = HexMap.new()
{_, map} = HexMap.insert(map, hex)
{_, map} = HexMap.set(map, hex, :hello, :world)
assert HexMap.get(map, hex, :hello) == {:ok, :world}