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
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!;
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.1.3"