hexing is a Rust library designed for manipulation and calculations on hexagonal grids. It provides tools for working with hexagonal positions and directions, as well as iterators for exploring hexagonal rings and spirals.
Features
- Hexagonal Coordinate Manipulation: Represent and manipulate positions in a hexagonal grid using axial coordinates.
- Distance Calculations: Compute the distance between two hexagonal positions.
- Pixel Coordinate Conversion: Convert hexagonal positions to pixel coordinates for graphical use.
- Reflection and Rotation: Apply reflection and rotation to hexagonal positions.
- Ring and Spiral Iterators: Obtain positions in a ring or spiral around a central position.
- Line Iterators: Obtain positions along a line between two hexagonal positions.
- Number Trait: Allow generic calculations with various numeric types.
- Serialization and Deserialization: Serialize and deserialize hexagonal positions and directions with the
serdefeature.
Number Trait
The library uses the Number trait to allow generic calculations with various numeric types. This trait is implemented for several types, including integers and floating-point numbers.
Main Types
HexPosition<T>
Represents a position in a hexagonal grid with coordinates T. Coordinates are in axial format (x, y).
-
Creation: Creates a new [HexPosition] with the given
qandrcoordinates in an axial format. Coordinates are explained in this documentation.let pos = new; let pos2 = HexPosition; // Constant: The origin of the hexagonal grid. let origin = ORIGIN; -
Conversion to Pixel Coordinates: Converts the current [HexPosition] into a pixel coordinate. Basically, it converts a position in a hexagonal grid to a position in a orthogonal grid.
let pixel_coords = pos.to_pixel_coordinates; let pixel_coords2 = from_pixel_coordinates; -
Distance Calculation: Calculates the distance between two hexagonal positions, using the Manhattan distance.
let distance = pos.distance; -
Rotation: Will apply a rotation of 2 x 60 degrees around the origin.
let rotated_pos = pos.rotation; -
Reflection: Will apply a central symmetric reflection around the origin.
let reflected_pos = pos.reflect; -
Ring Iterators: A iterator that returns positions in a ring around a central position. The iterator will return positions in a ring with the given radius.
for ring_pos in pos.ring -
Spiral Iterators: A iterator that returns positions in a spiral around a central position. The iterator will return positions in a spiral with the given radius.
for spiral_pos in pos.spiral -
Line Iterators A iterator that returns positions along a line between two hexagonal positions.
let a = HexPosition; let b = HexPosition; for line_pos in a.line
HexDirection
Enum representing the six possible directions in a hexagonal grid.
-
Available Directions:
Right(1, 0)UpRight(1, -1)UpLeft(0, -1)Left(-1, 0)DownLeft(-1, 1)DownRight(0, 1)
-
Convert to Vector: You can convert a [HexDirection] to a [HexPosition] by using the
to_vectormethod.let direction = Right; let vector = direction.to_vector;
Usage Examples
Here are some examples to illustrate the features of hexing.
Creating Hexagonal Positions
use HexPosition;
let pos = new;
let pos2 = HexPosition;
let origin = ORIGIN;
println!;
println!;
println!;
Conversion to Pixel Coordinates
use HexPosition;
let position = new;
let pixel_coords = position.to_pixel_coordinates;
println!;
let new_position: = from_pixel_coordinates;
println!;
assert!;
Calculating Distance Between Positions
use HexPosition;
let pos1 = new;
let pos2 = new;
let dist = pos1.distance;
println!;
Iterating Over Rings and Spirals
use ;
let center = new;
// Ring of radius 1
let ring = center.ring;
for pos in ring
// Spiral of radius 2
let spiral = center.spiral;
for pos in spiral
Rotation of Hexagonal Position
use HexPosition;
let rotation = 120;
let pos = new;
let rotated_pos = pos.rotation; // Rotates 120 degrees
println!;
Reflection of Hexagonal Position
use HexPosition;
let pos = new;
let reflected_pos = pos.reflect;
println!;
Line Iterator
use HexPosition;
let start = new;
let end = new;
for pos in start.line
Using HexDirection
use HexDirection;
let direction = UpRight;
let vector = direction.to_vector;
println!;
let new_position = new + vector * 3;
println!;
Feature serde
The hexing library allows for the serialization and deserialization of hexagonal data structures through the serde feature. This feature is enabled by the serde feature of the library.
Enabling the serde Feature
Simply run the command cargo add hexing --features=serde in your project or manually add it to your Cargo.toml.
[]
= { = "0.2.1", = ["serde"] }
Serialization and Deserialization with serde
When the serde feature is enabled, the following data structures automatically implement the Serialize and Deserialize traits:
- HexPosition
- HexDirection
- HexRing
- HexSpiral
- HexLine
Note: To use all the examples below, you need to add the serde_json library to your Cargo.toml.
HexPosition
HexPosition<T> represents a position in a hexagonal grid. With the serde feature, this structure can be serialized and deserialized, making it easier to save and retrieve hexagonal positions in formats like JSON, BSON, etc.
Example: Serialization and Deserialization
use HexPosition;
HexDirection
HexDirection is an enumeration representing all possible directions in a hexagonal grid. With the serde feature, this enumeration can be serialized and deserialized.
Example: Serialization and Deserialization
use HexDirection;
HexRing
HexRing<T> is a structure that allows iteration over positions in a hexagonal ring. This structure can also be serialized and deserialized with the serde feature.
Example: Serialization and Deserialization
use ;
HexSpiral
HexSpiral<T> allows iteration over positions in a hexagonal spiral. Like the other structures, HexSpiral<T> is serializable and deserializable with serde.
Example: Serialization and Deserialization
use ;
HexLine
HexLine<T> allows iteration over positions on a hexagonal line between two points. This structure is also serializable and deserializable.
Example: Serialization and Deserialization
use ;
Full Documentation
For more detailed documentation and additional explanations about hexagonal grids, please refer to the Red Blob Games hexagonal grid documentation.
Installation
Add hexing to your Cargo.toml:
[]
= "0.2.1"