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.
- Ring and Spiral Iterators: Obtain positions in a ring or spiral around a central position.
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:
let pos = new; -
Conversion to Pixel Coordinates:
let pixel_coords = pos.to_pixel_coordinates; -
Distance Calculation:
let distance = pos.distance; -
Ring Iterators:
for ring_pos in pos.ring -
Spiral Iterators:
for spiral_pos in pos.spiral
HexDirection
Enum representing the six possible directions in a hexagonal grid.
-
Available Directions:
RightUpRightUpLeftLeftDownLeftDownRight
-
Convert to Vector:
let direction = Right; let vector = direction.to_vector;
Usage Examples
Here are some examples to illustrate the features of hexing.
Conversion to Pixel Coordinates
use HexPosition;
let position = new;
let pixel_coords = position.to_pixel_coordinates;
println!;
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
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.1"