Skip to content

A library for hex coordinates in Rust.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

hdpotter/sturdy_hex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SturdyHex

Yet another hex library for Rust! I've made it available in an early state with partially completed documentation and tests and no published crate, but these things are in the works.

Coordinates and Basic Usage

SturdyHex uses the axial coordinate system (and several of the algorithms) from Amit Patel's excellent resource on hex grids. The HexField struct relates a worldspace coordinate system with a hex coordinate system:

let hex_field = HexField::new(
	Vector3::<f32>::zero(), //center of hex (0, 0, 0)
	Vector3::<f32>::new(0.0, 0.0, 1.0), //up direction
	Vector3::<f32>::new(0.0, 1.0, 0.0), //center of hex with coord (0, 1, -1)
);

let hex_containing_origin = hex_field.get_hex_coord(Vector3::<f32>::zero());
let position_of_center = hex_field.get_position(HexCoord::ZERO);

You can choose between Amit Patel's "flat-top" and "pointy-top" orientations by your choice of the center for hex (0, -1, 1); in the example above, it's on the y-axis, providing flat-top coordinates.

SturdyHex currently uses cgmath for vector math, but I intend to switch to glam and provide mint compatibility.

Overview of Advanced Features

In this section I'll provide a brief overview of other features and tell you where to look in the source code to understand them better. Further documentation forthcoming.

Grid traversal

SturdyHex provides methods to interact with a hex grid as a doubly-connected edge list. Because hex grids are regular, all connectivity information is implicit; we can calculate how hexes, vertices, and edges connect, so we don't need to store any connection information.

See HexCoord, HexVertex, and HexHalfEdge for the relevant methods.

Chunking

Sometimes it's useful to partition a hex grid into hex-shaped chunks. Unfortunately, this is not as straightforward as it is with squares and square-shaped chunks. You can create a HexChunker instance with a specified chunk radius to perform conversions from hex coordinate to chunk coordinate and back. SturdyHex uses Sander Ever's algorithm for converting in the hex-to-chunk direction.

Transforms

The HexTransform struct represents a translation and rotation in hex coordinates. You can apply them to hexes, vertices, edges, and other transforms via the * operator.

Shapes

The HexShape class stores a list of hex coordinates and supports the contains operation. You can create a HexShapeView that references a HexShape and encodes a transformation on it; this allows you to query against a shape without copying its data.

Range Iteration

You can create a HexRangeIterator instance with a center coordinate and radius to iterate over the corresponding hexagonal range of hexes.

License

Licensed under either of Apache License, Version 2.0 or MIT License at your option.

About

A library for hex coordinates in Rust.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages