Skip to main content

A type-hinted, object-oriented Python implementation for working with hex grids

Project description

Hexpex

A type-hinted, object-oriented Python implementation for working with hex grids

About the Project

This is a type-hinted, object-oriented implementation in Python of hexagonal grids as described on Red Blob Games. This package allows you to easily work with hexagonal grids in Python. All of its classes, attributes and methods are type-hinted which allows your editor to autocomplete signatures and catch bugs and mistakes early.

(back to top)

Prerequisites

Because this package uses type hints, keyword-only and positional-only arguments you must have Python 3.9 or greater installed.

(back to top)

Installation

Using PIP

pip install hexpex

Using Poetry

poetry add hexpex

Manually (for testing or development)

git clone https://github.com/solbero/hexpex.git
cd hexpex
poetry install

(back to top)

Usage

Classes

Hexpex provides classes for working with hexagonal grids in both the cube and axial coordinate systems. For more information about the difference between these two coordinate systems see the writeup on Red Blob Games.

from hexpex import Axial, Cube

Cube(q=1, r=0, s=-1)
Axial(q=1, r=0)

Object Methods

adjacent()
Returns the hex coordinate in adjacent direction from self
diagonal()
Returns the hex coordinate in diagonal direction from self
distance()
Returns the distance between passed hex coordinate and self
range()
Returns a set of hex coordinates within passed distance of self
ring()
Returns a set of hex coordinates on a ring passed distance from self
rotation()
Returns a set of rotated hex coordinates rotated around self
spiral()
Yields hex positions in a spiral from self out to passed distance from self

Operations

Objects can be added or subtracted from each other, and multiplied or divided by integers.

from hexpex import Axial, Cube

cube_1 = Cube(q=2, r=0, s=-2)
cube_2 = Cube(q=-1, r=0, s=1)

cube_1 + cube_2
#> Cube(1, 0, -1)
cube_1 - cube_2
#> Cube(3, 0, -3)
cube_1 * 2
#> Cube(4, 0, -4)
cube_1 // 2
#> Cube(1, 0, -1)

axial_1 = Axial(q=2, r=0)
axial_2 = Axial(q=-1, r=0)

axial_1 + axial_2
#> Axial(1, 0)
axial_1 - axial_2
#> Axial(3, 0)
axial_1 * 2
#> Axial(4, 0)
axial_1 // 2
#> Axial(1, 0)

Direction Vectors

Hexpex provides some helper enums for giving direction vectors to the methods adjacent(), diagonal() and spiral(). To use them import the enums for your coordinate system and hex orientation (pointy or flat). For more information on the difference between the two hex orientations see Red Blob Games.

from hexpex import Cube, CubeFlatAdjacentDirection as AdjacentDirection, CubeFlatDiagonalDirection as DiagonalDirection

cube = Cube(0, 0, 0)

cube.adjacent(AdjacentDirection.SE)
#> Cube(1, 0, -1)
cube.diagonal(DiagonalDirection.E)
#> Cube(2, -1, -1)

Conversion

A cube object can be converted to an axial object using the to_axial() method. The reverse is true for an axial object using the to_cube() method.

Both representations can also be converted to a tuple using the to_tuple() method and to a dict using the to_dict()method.

from hexpex import Axial, Cube

cube = Cube(1, 0, -1)
axial = Axial(1, 0)

cube.to_axial()
#> Axial(1, 0)
axial.to_cube()
#> Cube(1, 0, -1)

cube.to_tuple()
#> (1, 0, -1)
axial.to_tuple()
#> (1, 0)

cube.to_dict()
#> {"q": 1, "r": 0, "s": -1}
axial.to_dict()
#> {"q": 1, "r": 0}

(back to top)

Roadmap

Coordinate systems

  • Cube coordinates
  • Axial coordinates
  • Double offset coordinates

Methods

  • Distances
  • Neighbors
  • Range
  • Rings
  • Rotation
  • Spiral
  • Line drawing
  • Reflection
  • Rounding
  • Hex to pixel
  • Pixel to hex

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

If you have a suggestion that would make this project better, please fork the repo and create a pull request. You can also simply open an issue with the label "enhancement".

  1. Fork the project
  2. Create your feature branch
git checkout -b feature/AmazingFeature
  1. Commit your changes
git commit -m 'Add some AmazingFeature'
  1. Push to the branch
git push origin feature/AmazingFeature
  1. Open a pull request

(back to top)

License

Distributed under the GPLv3 License. See LICENSE.txt for more information.

(back to top)

Contact

(back to top)

Project Links

(back to top)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hexpex-0.2.0.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hexpex-0.2.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file hexpex-0.2.0.tar.gz.

File metadata

  • Download URL: hexpex-0.2.0.tar.gz
  • Upload date:
  • Size: 20.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.13 Linux/5.15.0-1014-azure

File hashes

Hashes for hexpex-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ab88d8373c9b68d481f456169d98f57774cf87f6aecd43d96443718ee1307d40
MD5 e47810e425d6ce00da3f721fc566713e
BLAKE2b-256 42a357cefc2b41251bef16f822d0470a443ea1b5b29cde1e655b80d15f3f6cb6

See more details on using hashes here.

File details

Details for the file hexpex-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: hexpex-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.13 Linux/5.15.0-1014-azure

File hashes

Hashes for hexpex-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3be474c715dd8763679a3e90b2456fb7dd3ed689350cb5417584de368faa9a13
MD5 9dd89114fd65196e8023feb6f6a3bd77
BLAKE2b-256 7ac6b1a93e298e8cfcdc1f3ec5d20c4ddd82f030a4349984f89aad7646e3dcc9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page