hexing 0.1.1

A basic Rust library to manipulate hexagonal grids.
Documentation

Contributors Forks Stargazers Issues License


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 = HexPosition::new(1, 2);
    
  • Conversion to Pixel Coordinates:

    let pixel_coords = pos.to_pixel_coordinates();
    
  • Distance Calculation:

    let distance = pos.distance(HexPosition::new(3, 4));
    
  • Ring Iterators:

    for ring_pos in pos.ring(2) {
        println!("{:?}", ring_pos);
    }
    
  • Spiral Iterators:

    for spiral_pos in pos.spiral(2) {
        println!("{:?}", spiral_pos);
    }
    

HexDirection

Enum representing the six possible directions in a hexagonal grid.

  • Available Directions:

    • Right
    • UpRight
    • UpLeft
    • Left
    • DownLeft
    • DownRight
  • Convert to Vector:

    let direction = HexDirection::Right;
    let vector = direction.to_vector();
    

Usage Examples

Here are some examples to illustrate the features of hexing.

Conversion to Pixel Coordinates

use hexing::HexPosition;

let position = HexPosition::new(1, 0);
let pixel_coords = position.to_pixel_coordinates();
println!("Pixel coordinates: {:?}", pixel_coords);

Calculating Distance Between Positions

use hexing::HexPosition;

let pos1 = HexPosition::new(0, 0);
let pos2 = HexPosition::new(-2, -1);
let dist = pos1.distance(pos2);
println!("Distance: {:?}", dist);

Iterating Over Rings and Spirals

use hexing::{HexPosition, HexRing, HexSpiral};

let center = HexPosition::new(0, 0);

// Ring of radius 1
let ring = center.ring(1);
for pos in ring {
    println!("Ring position: {:?}", pos);
}

// Spiral of radius 2
let spiral = center.spiral(2);
for pos in spiral {
    println!("Spiral position: {:?}", pos);
}

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:

[dependencies]

hexing = "0.1.1"