Hexagons

Module for the Hexagons.

class polyhex.objects.hexagons.Hexagon(hex_coord_system: str = 'axial', hex_coord: ~typing.Tuple[int] = (0, 0), top: str = 'pointy', radius: int | float = 1, vertex_orientation: str = 'clockwise', assets: ~typing.Dict = <factory>, hexagon_feature: ~collections.abc.Buffer | ~numpy._typing._array_like._SupportsArray[~numpy.dtype[~typing.Any]] | ~numpy._typing._nested_sequence._NestedSequence[~numpy._typing._array_like._SupportsArray[~numpy.dtype[~typing.Any]]] | complex | bytes | str | ~numpy._typing._nested_sequence._NestedSequence[complex | bytes | str] = 'placeholder', vertex_feature: ~collections.abc.Buffer | ~numpy._typing._array_like._SupportsArray[~numpy.dtype[~typing.Any]] | ~numpy._typing._nested_sequence._NestedSequence[~numpy._typing._array_like._SupportsArray[~numpy.dtype[~typing.Any]]] | complex | bytes | str | ~numpy._typing._nested_sequence._NestedSequence[complex | bytes | str] = 'placeholder', edge_feature: ~collections.abc.Buffer | ~numpy._typing._array_like._SupportsArray[~numpy.dtype[~typing.Any]] | ~numpy._typing._nested_sequence._NestedSequence[~numpy._typing._array_like._SupportsArray[~numpy.dtype[~typing.Any]]] | complex | bytes | str | ~numpy._typing._nested_sequence._NestedSequence[complex | bytes | str] = 'placeholder')

Bases: object

Base class for creating a Hexagon. See https://www.redblobgames.com/grids/hexagons/ for a great explanation

Parameters:
  • hex_coord_system (str) – The hexagonal coordinate system. It can be offset, cube, axial or doubled`Defaults to `axial.

  • hex_coord (List[int]) – The coordinates of the Hexagon in the given hexagonal coordinate system. Defaults to the centre coordinate [0,0].

  • top (str) – The top of the hexagon. Can only be pointy of flat. Defaults to pointy.

  • radius (int|float) – The radius’ value. For PolyHex, the radius refers to the radius of the circle to which all the hexagon’s vertices belong.Defaults to 1.

  • vertex_orientation (str) – The vertex orientation. It is can be clockwise or couterclockwise. Important note: For hexagons with pointy top, We start counting the vertices by starting with the one at 12.00. For hexagons with flat top, We start counting the vertices by starting with the one at 3.00 Defaults to clockwise.

  • assets (Dict) – A big dictionnary holding all the information about rendering, token compatibility, etc, etc. Defaults to the defaults_assets.json file.

  • hexagon_feature (ArrayLike) – The feature of the hexagon as an entity. Defaults to 0.

  • vertex_feature (ArrayLike) – The feature of the vertices Note: there is a bit of a misnomer here. There are 6 vertices per hexagon, so the attribute name should be ‘vertices_feature’ and an ArrayLike of size 6 should be the default. For ease of use, we deliberately offer to define all the vertices’ feature by providing a single default argument, replicated accross all edges. However, if an ArrayLike is provided, the features will be allocated in the order defined by vertex_orientation. It also MUST be hashable by a frozenset. Defaults to 0.

  • edge_feature (ArrayLike) – The feature of the edges Note: there is a bit of a misnomer here. There are 6 edges per hexagon, so the attribute name should be ‘edges_feature’ and an ArrayLike of size 6 should be the default. For ease of use, we deliberately offer to define all the edges’ feature by providing a single default argument, replicated accross all edges. However, if an ArrayLike is provided, the features will be allocated in the order defined by vertex_orientation. It also MUST be hashable by a frozenset. Defaults to 0.

add_token(new_token: str)

Method to add a token on a Hexagon. Under the hood, it adds the token to the HexagonCentre’s

Parameters:

new_token (str) – str identifier of the token.

property adjency: List[Tuple[int]]

Get a hexagon adjency, i.e the coordinates of neighbouring hexagons.

Raises:
  • NotImplementedError – is only implemented for the axial coordinates system.

  • NotImplementedError – is only implemented for the clockwise vertex ordering.

Returns:

[(coord_hex_0), …, (coord_hex_5)]

Return type:

List[Tuple[int]]

assets: Dict
distance(other, kwd='euclidian')

Method to compute the distance between two Hexagon

Parameters:
  • other (_type_) – other Hexagon

  • kwd (str, optional) – Identifier of the distance. Defaults to “euclidian”.

Raises:

NotImplementedError – This method is currently only implemented for the euclidian distance.

Returns:

float distance value

Return type:

np.ndarray

draw(buffer_object)

Method to draw a hexagon.

Creates a matplotlib.axis and calls render on it.

Parameters:

buffer_object – savepath or buffer for image saving

edge_feature: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str] = 'placeholder'
property encoding

Returns the Hexagon’s encoding. The default version returns the HexagonCentre’s encoding

Returns:

vector representation of the Hexagon’s attribute

Return type:

List

get_edge_adjency(edge)

Method to get the edge adjency, i.e the neighbouring edges, in and outside of the Hexagon

Parameters:

vertex (HexagonEdge) – edge to get the adjency of.

Returns:

length-4 size adjency coordinates

Return type:

List[Tuple[int]]

get_vertex_adjency(vertex)

Method to get the vertex adjency, i.e the neighbouring vertices, in and outside of the Hexagon

Parameters:

vertex (HexagonVertex) – vertex to get the adjency of.

Returns:

length-3 size adjency coordinates

Return type:

List[Tuple[int]]

hex_coord: Tuple[int] = (0, 0)
hex_coord_system: str = 'axial'
hexagon_feature: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str] = 'placeholder'
is_compatible(other) bool

Method to assess if the properties of a Hexagon are compatible with the properties of another.

Parameters:

other (Hexagon) – Other Hexagon to compare it with

Returns:

Boolean result of the compatibility assessment

Return type:

bool

radius: int | float = 1
render(axes)

Method to render a hexagon.

Unlike draw, it requires a matplotlib axis object.

property token

Returns the hexagon’s token Under the hood, it returns the HexagonCentre’s token

Returns:

vector representation of the Hexagon’s attribute

Return type:

List

top: str = 'pointy'
vertex_feature: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str] = 'placeholder'
vertex_orientation: str = 'clockwise'