Skip to content

gravitton/hexagon

Repository files navigation

Hexagon

Latest Stable Version Build Status Coverage Status Go Report Card Go Dev Reference Software License

Hexagon library for game development.

  • Axial (cube) coordinates with arithmetic, distance, and neighbor lookup
  • Area and perimeter traversal: range, ring, and spiral
  • Line drawing, line-of-sight, and field-of-view
  • Rotation and reflection in cube space
  • Direction enum with opposite and rotation helpers
  • Seven coordinate systems with lossless round-trip conversion

Installation

go get github.com/gravitton/hexagon

Usage

import (
	hex "github.com/gravitton/hexagon"
	geom "github.com/gravitton/geometry"
)

// create hexagon in axial coordinates (q,r)
a := hex.Pt(1, -2)
b := hex.Pt(0, 3)

// arithmetic and distance
c := a.Add(b)
d := a.Subtract(b)
e := a.Multiply(2)
distance := a.DistanceTo(b)

// neighbors
neighbors := a.Neighbors()
neighbor := a.Neighbor(hex.QPlus)

// area traversal
area := b.Range(2) // all hexes within radius 2 (filled)
ring := b.Ring(2) // hexes at exactly distance 2 (perimeter)
spiral := b.Spiral(2) // same as Range but ordered center-outward

// line drawing and visibility
line := a.Line(b)
visible := a.HasLineOfSight(b, blocking)
fov := a.FieldOfView(candidates, blocking)

// rotation and reflection (cube space)
rotated := a.Rotate(2) // 2×60° clockwise around origin
rotatedAround := a.RotateAround(b, -1) // 1×60° counterclockwise around b
reflectedQ := a.ReflectQ()
reflectedR := a.ReflectR()
reflectedS := a.ReflectS()

// directions
dir := hex.QPlus
opp := dir.Opposite() // SPlus
next := dir.Rotate(1) // RMinus (one step counterclockwise)
offset := dir.NeighborOffset() // axial vector for this direction

// coordinate system conversions
pOddR := a.To(hex.OffsetOddR)
pEvenQ := a.To(hex.OffsetEvenQ)
dw := a.To(hex.DoubleWidth)
dh := a.To(hex.DoubleHeight)
back := hex.From(pOddR, hex.OffsetOddR)

Credits

License

The MIT License (MIT). Please see License File for more information.