# Hexx
Hexagonal tools lib in rust.
Inspired by this
RedBlobGames
article.
This lib allows you to: - Manipulate hexagon coordinates - Generate hexagonal maps with custom layouts and orientation - Generate hexagon meshes (planes or columns)
I made the choice to use Axial Coordinates for performance and utility reasons,
but the [Hex
] type has conversion utilities with cubic, doubled and offset coordinates.
See the hexagonal coordinate systems
## Features
hexx
provides the [Hex
] coordinates with:
- Distances
- Neighbors and directions
- Lines
- Ranges
- Rings
- Edges
- Wedges
- Spirals
- Rotation
- Symmetry
- Vector operations
- Conversions to other coordinate systems
And the [HexMap
] utility, for wraparound (seamless) hexagonal maps
## Basic usage
```rust use hexx::*;
// Declare points in hexagonal spaces
let pointa = Hex::new(10, -5);
let pointb = Hex::new(-8, 15);
// Find distance between them
let dist = pointa.unsigneddistanceto(pointb);
// Compute a line between points
let line: Vecpoint_a
containing point_b
let ring: Vecpoint_b
around point_a
by 2 times 60 degrees clockwise
let rotated = pointb.rotaterightaround(pointa, 2);
// Find the direction between the two points
let dira = pointa.directionto(pointb);
let dirb = pointb.directionto(pointa);
assert!(dira == -dirb);
// Compute a wedge from point_a
to point_b
let wedge = pointa.wedgeto(point_b);
// Get the average value of the wedge
let avg = wedge.average();
```
## Layout usage
[HexLayout
] is the bridge between your world/screen/pixel coordinate system and the hexagonal
coordinates system.
```rust use hexx::*;
// Define your layout
let layout = HexLayout {
hexsize: Vec2::new(1.0, 1.0),
orientation: HexOrientation::flat(),
..Default::default()
};
// Get the hex coordinate at the world position world_pos
.
let worldpos = Vec2::new(53.52, 189.28);
let hex = layout.worldpostohex(worldpos);
// Get the world position of hex
let hex = Hex::new(123, 45);
let worldpos = layout.hextoworldpos(hex);
```
## Usage in Bevy
If you want to generate 3D hexagonal mesh and use it in bevy you may do it this way:
```rust use bevy::prelude::Mesh; use bevy::render::{mesh::Indices, render_resource::PrimitiveTopology}; use hexx::{HexLayout, Hex, MeshInfo};
pub fn hexagonalplane(hexlayout: &HexLayout) -> Mesh { // Compute hex plane data for at the origin let meshinfo = MeshInfo::hexagonalplane(hexlayout, Hex::ZERO); // Compute the bevy mesh let mut mesh = Mesh::new(PrimitiveTopology::TriangleList); mesh.insertattribute(Mesh::ATTRIBUTEPOSITION, meshinfo.vertices.tovec()); mesh.insertattribute(Mesh::ATTRIBUTENORMAL, meshinfo.normals.tovec()); mesh.insertattribute(Mesh::ATTRIBUTEUV0, meshinfo.uvs.tovec()); mesh.setindices(Some(Indices::U16(meshinfo.indices))); mesh } ```
The [MeshInfo
] type provides the following mesh generations:
- [MeshInfo::hexagonal_plane
] (7 vertices) useful for 2D games
- [MeshInfo::cheap_hexagonal_column
] (13 vertices) with merged vertices and useful only for
unlit games
- [MeshInfo::partial_hexagonal_column
] (31 vertices) without the bottom face
- [MeshInfo::hexagonal_column
] (38 vertices) with the bottom face
See the examples for bevy usage
## Example
cargo run --example hex_grid
cargo run --example 3d_columns