Package: hexpex
A type-hinted, object-oriented Python implementation for working with hex gridsAbout the ProjectThis is a type-hinted, object-oriented implementation in Python ofhexagonal gridsas described onRed 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.PrerequisitesBecause this package uses type hints, keyword-only and positional-only arguments you must have Python 3.9 or greater installed.InstallationUsing PIPpipinstallhexpexUsing PoetrypoetryaddhexpexManually (for testing or development)gitclonehttps://github.com/solbero/hexpex.gitcdhexpex
poetryinstallUsageClassesHexpex 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 onRed Blob Games.fromhexpeximportAxial,CubeCube(q=1,r=0,s=-1)Axial(q=1,r=0)Object Methodsadjacent()Returns the hex coordinate in adjacent direction from selfdiagonal()Returns the hex coordinate in diagonal direction from selfdistance()Returns the distance between passed hex coordinate and selfrange()Returns a set of hex coordinates within passed distance of selfring()Returns a set of hex coordinates on a ring passed distance from selfrotation()Returns a set of rotated hex coordinates rotated around selfspiral()Yields hex positions in a spiral from self out to passed distance from selfOperationsObjects can be added or subtracted from each other, and multiplied or divided by integers.fromhexpeximportAxial,Cubecube_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 VectorsHexpex provides some helper enums for giving direction vectors to the methodsadjacent(),diagonal()andspiral().
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 seeRed Blob Games.fromhexpeximportCube,CubeFlatAdjacentDirectionasAdjacentDirection,CubeFlatDiagonalDirectionasDiagonalDirectioncube=Cube(0,0,0)cube.adjacent(AdjacentDirection.SE)#> Cube(1, 0, -1)cube.diagonal(DiagonalDirection.E)#> Cube(2, -1, -1)ConversionA cube object can be converted to an axial object using theto_axial()method.
The reverse is true for an axial object using theto_cube()method.Both representations can also be converted to a tuple using theto_tuple()method and to a dict using theto_dict()method.fromhexpeximportAxial,Cubecube=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}RoadmapCoordinate systemsCube coordinatesAxial coordinatesDouble offset coordinatesMethodsDistancesNeighborsRangeRingsRotationSpiralLine drawingReflectionRoundingHex to pixelPixel to hexSee theopen issuesfor a full list of proposed features (and known issues).ContributingIf you have a suggestion that would make this project better, pleasefork the repoand create a pull request.
You can also simplyopen an issuewith the label "enhancement".Fork the projectCreate your feature branchgitcheckout-bfeature/AmazingFeatureCommit your changesgitcommit-m'Add some AmazingFeature'Push to the branchgitpushoriginfeature/AmazingFeatureOpen a pull requestLicenseDistributed under the GPLv3 License.
SeeLICENSE.txtfor more information.ContactEmail:njord.solberg@gmail.comProject LinksGithub:https://github.com/solbero/hexpexPyPI:https://pypi.org/project/hexpex/