Hexagonal grid geometry foundation for MoonBit: axial/cube/offset coordinate systems, distance, lines, rings, spirals, rotation, shadowcasting field of view and A* pathfinding behind pure, deterministic APIs
| Area | Capabilities |
|---|---|
| Coordinate systems | axial Hex, cube coordinates, OddR/EvenR/OddQ/EvenQ offset layouts, round-trip conversion |
| Grid mathematics | hex distance, 6-direction neighbours, straight lines (lerp + cube rounding), rings, spirals, filled ranges, 60° rotation, reflection |
| Pixel layouts | pointy-top and flat-top layouts, pixel ↔ axial conversion, bounds-driven sizing |
| Pathfinding | Dijkstra cost flood with custom step costs and max_cost bounds, A* with admissible heuristics, path reconstruction |
| Field of view | line-of-sight FOV with transparency callbacks, wall-blocking semantics |
moon add wbgxiaosu/hexgridimport {
"wbgxiaosu/hexgrid/hex",
"wbgxiaosu/hexgrid/path",
"wbgxiaosu/hexgrid/fov",
}///|
fn main raise {
let start = @hex.Hex::make(0, 0)
let goal = @hex.Hex::make(4, -2)
// straight-line geometry
println("distance: \{start.distance(goal)}") // 4
// pathfinding around a wall on column q = 1
fn passable(c : @hex.Hex) -> Bool {
!(c.q == 1 && c.r != -1)
}
match @path.find_path(start, goal, passable, fn(_f, _t) { 1.0 }) {
Some(steps) => println("detour: \{steps.length() - 1} steps") // 4
None => println("unreachable")
}
}$ moon run cmd/main
distance 0;0 -> 4;-2 = 4
line 0;0 -> 4;-2: 0;0 1;0 2;-1 3;-1 4;-2
range 2 has 19 cells; ring 2 has 12
A* path (4 steps, detouring the wall): 0;0 -> 1;-1 -> 2;-1 -> 3;-1 -> 4;-2
visible cells (16): -2;0, -2;1, -2;2, -1;-1, -1;0, -1;1, -1;2, 0;-2, 0;-1, 0;0, 0;1, 0;2, 1;-2, 1;-1, 1;0, 2;-2$ cd examples/consumer && moon run .
consumer A* steps: 4
consumer fov cells: 19
consumer example OKmoon check --deny-warn
moon test --deny-warn
moon run cmd/main
cd examples/consumer && moon run .Install
Download zipHexagonal grid geometry foundation for MoonBit: axial/cube/offset coordinate systems, distance, lines, rings, spirals, rotation, shadowcasting field of view and A* pathfinding behind pure, deterministic APIs