Skip to main content

Crate hexal

Crate hexal 

Source
Expand description

Axial hex coordinates for no_std and embedded targets.

§Coordinate system

hexal uses axial coordinates (q, r) as the primary representation. Axial is the cleanest system for arithmetic: neighbors, distance, rings, and lines are all simple expressions with no lookup tables.

The third cube axis s satisfies q + r + s = 0 and is derived on demand.

       +r
      /
-q --●-- +q
      \
       -r     (pointy-top orientation)

§Offset coordinates

For screen/storage mapping, use OffsetHex with one of the four standard schemes: OddR, EvenR, OddQ, EvenQ. Convert with Hex::to_offset and OffsetHex::to_hex.

§Type aliases

HexI (i32), HexI16 (i16), and HexI8 (i8) cover the most common embedded and desktop use cases.

§Features

  • serde — derive Serialize/Deserialize on Hex and OffsetHex.
  • ixy — enable From/Into conversions with ixy::Pos<T> via OddR offset (the natural screen mapping).

§Examples

use hexal::{hex, Hex, Direction};

let a = hex!(1, 0);
let b = hex!(0, 1);
assert_eq!(a.distance(b), 1);
assert_eq!(a.neighbor(Direction::W), hex!(0, 0));

Modules§

int
Signed integer bound used throughout hexal.

Macros§

hex
Creates a Hex from (q, r) literals.

Structs§

EvenQ
Flat-top orientation, even columns shifted up.
EvenR
Pointy-top orientation, even rows shifted right.
Hex
An axial hex coordinate (q, r).
HexLine
Iterator over the hex line from one hex to another.
HexRange
Iterator over all hexes within radius steps of a center (inclusive).
HexRing
Iterator over the ring of hexes at exactly radius steps from a center.
OddQ
Flat-top orientation, odd columns shifted down.
OddR
Pointy-top orientation, odd rows shifted right.
OffsetHex
A hex coordinate stored as (col, row) under a specific offset scheme.

Enums§

Direction
One of the six axial directions on a pointy-top hex grid.

Traits§

OffsetScheme
Conversion scheme between axial and offset coordinates.

Type Aliases§

HexI
Hex with i32 coordinates — the default for most use cases.
HexI8
Hex with i8 coordinates — for tiny grids (±127 per axis).
HexI16
Hex with i16 coordinates — suitable for large maps on embedded.