Documentation
¶
Overview ¶
Package hex provides utilities for working with hexagonal grids.
It implements axial (also called cube) coordinates and several common integer offset systems (odd-r, even-r, odd-q, even-q) along with double- width and double-height variants. The package also contains helpers for neighbor lookup, distances, ranges, and converting between axial and offset coordinate systems.
In addition, Layout converts between hex coordinates and pixel space for both flat-top and pointy-top hex orientations, and can generate basic shapes (like a regular hexagon) for rendering.
See README and unit tests for more examples.
Index ¶
- Constants
- Variables
- func AssertFracHex(t *testing.T, h FractionalHex, q, r float64, messages ...string)
- func AssertHex(t *testing.T, h Hex, q, r int, messages ...string)
- func NeighborOffset(index ints.Point, system CoordinateSystem, direction Direction) ints.Vector
- func NeighborOffsets(index ints.Point, system CoordinateSystem) []ints.Vector
- func NeighborOffsetsAxial() []ints.Vector
- func NeighborOffsetsDoubleHeight() []ints.Vector
- func NeighborOffsetsDoubleWidth() []ints.Vector
- func NeighborOffsetsOffsetEvenQ(index ints.Point) []ints.Vector
- func NeighborOffsetsOffsetEvenR(index ints.Point) []ints.Vector
- func NeighborOffsetsOffsetOddQ(index ints.Point) []ints.Vector
- func NeighborOffsetsOffsetOddR(index ints.Point) []ints.Vector
- func To(hex Hex, system CoordinateSystem) ints.Point
- func ToAxial(hex Hex) ints.Point
- func ToDoubleHeight(hex Hex) ints.Point
- func ToDoubleWidth(hex Hex) ints.Point
- func ToOffsetEvenQ(hex Hex) ints.Point
- func ToOffsetEvenR(hex Hex) ints.Point
- func ToOffsetOddQ(hex Hex) ints.Point
- func ToOffsetOddR(hex Hex) ints.Point
- type CoordinateSystem
- type Direction
- type FractionalHex
- func (h FractionalHex) Lerp(hex FractionalHex, t float64) FractionalHex
- func (h FractionalHex) Point() floats.Point
- func (h FractionalHex) QR() (float64, float64)
- func (h FractionalHex) QRS() (float64, float64, float64)
- func (h FractionalHex) Round() Hex
- func (h FractionalHex) S() float64
- func (h FractionalHex) String() string
- type Hex
- func From(index ints.Point, system CoordinateSystem) Hex
- func FromAxial(index ints.Point) Hex
- func FromDoubleHeight(index ints.Point) Hex
- func FromDoubleWidth(index ints.Point) Hex
- func FromOffsetEvenQ(index ints.Point) Hex
- func FromOffsetEvenR(index ints.Point) Hex
- func FromOffsetOddQ(index ints.Point) Hex
- func FromOffsetOddR(index ints.Point) Hex
- func FromPoint(index ints.Point) Hex
- func H(q, r int) Hex
- func (h Hex) Add(hex Hex) Hex
- func (h Hex) DistanceTo(hex Hex) int
- func (h Hex) FieldOfView(candidates []Hex, blocking []Hex) []Hex
- func (h Hex) HasLineOfSight(target Hex, blocking []Hex) bool
- func (h Hex) Length() int
- func (h Hex) Line(target Hex) []Hex
- func (h Hex) Multiply(factor int) Hex
- func (h Hex) Neighbor(direction Direction) Hex
- func (h Hex) Neighbors() []Hex
- func (h Hex) Point() ints.Point
- func (h Hex) QR() (int, int)
- func (h Hex) QRS() (int, int, int)
- func (h Hex) Range(n int) []Hex
- func (h Hex) S() int
- func (h Hex) String() string
- func (h Hex) Subtract(hex Hex) Hex
- func (h Hex) To(system CoordinateSystem) ints.Point
- type Layout
Constants ¶
const ( DirectionFlatTopSE = DirectionSMinus DirectionFlatTopNE = DirectionQPlus DirectionFlatTopN = DirectionRMinus DirectionFlatTopNW = DirectionSPlus DirectionFlatTopSW = DirectionQMinus DirectionFlatTopS = DirectionRPlus )
Direction aliases for flat-top hexes (Axial, OffsetOddQ, OffsetEvenQ, DoubleHeight)
const ( DirectionPointyTopE = DirectionSMinus DirectionPointyTopNE = DirectionQPlus DirectionPointyTopNW = DirectionRMinus DirectionPointyTopW = DirectionSPlus DirectionPointyTopSW = DirectionQMinus DirectionPointyTopSE = DirectionRPlus )
Direction aliases for pointy-top hexes (Axial, OffsetOddR, OffsetEvenR, DoubleWidth)
Variables ¶
var Directions = [6]ints.Vector{ geom.Vec(1, 0), geom.Vec(1, -1), geom.Vec(0, -1), geom.Vec(-1, 0), geom.Vec(-1, 1), geom.Vec(0, 1), }
Directions lists neighbor vectors for axial coordinates in counter-clockwise, starting at (south)-east direction.
var DirectionsDoubleHeight = [6]ints.Vector{ geom.Vec(1, 1), geom.Vec(1, -1), geom.Vec(0, -2), geom.Vec(-1, -1), geom.Vec(-1, 1), geom.Vec(0, 2), }
DirectionsDoubleHeight lists neighbor vectors for double-height coordinates.
var DirectionsDoubleWidth = [6]ints.Vector{ geom.Vec(2, 0), geom.Vec(1, -1), geom.Vec(-1, -1), geom.Vec(-2, 0), geom.Vec(-1, 1), geom.Vec(1, 1), }
DirectionsDoubleWidth lists neighbor vectors for double-width coordinates.
var DirectionsOffsetEvenQ = [2][6]ints.Vector{ { geom.Vec(1, 1), geom.Vec(1, 0), geom.Vec(0, -1), geom.Vec(-1, 0), geom.Vec(-1, 1), geom.Vec(0, 1), }, { geom.Vec(1, 0), geom.Vec(1, -1), geom.Vec(0, -1), geom.Vec(-1, -1), geom.Vec(-1, 0), geom.Vec(0, 1), }, }
DirectionsOffsetEvenQ lists neighbor vectors for even-q offset coordinates as [parityCol][direction], where parityCol=0 for even columns and 1 for odd columns.
var DirectionsOffsetEvenR = [2][6]ints.Vector{ { geom.Vec(1, 0), geom.Vec(1, -1), geom.Vec(0, -1), geom.Vec(-1, 0), geom.Vec(0, 1), geom.Vec(1, 1), }, { geom.Vec(1, 0), geom.Vec(0, -1), geom.Vec(-1, -1), geom.Vec(-1, 0), geom.Vec(-1, 1), geom.Vec(0, 1), }, }
DirectionsOffsetEvenR lists neighbor vectors for even-r offset coordinates as [parityRow][direction], where parityRow=0 for even rows and 1 for odd rows.
var DirectionsOffsetOddQ = [2][6]ints.Vector{ { geom.Vec(1, 0), geom.Vec(1, -1), geom.Vec(0, -1), geom.Vec(-1, -1), geom.Vec(-1, 0), geom.Vec(0, 1), }, { geom.Vec(1, 1), geom.Vec(1, 0), geom.Vec(0, -1), geom.Vec(-1, 0), geom.Vec(-1, 1), geom.Vec(0, 1), }, }
DirectionsOffsetOddQ lists neighbor vectors for odd-q offset coordinates as [parityCol][direction], where parityCol=0 for even columns and 1 for odd columns.
var DirectionsOffsetOddR = [2][6]ints.Vector{ { geom.Vec(1, 0), geom.Vec(0, -1), geom.Vec(-1, -1), geom.Vec(-1, 0), geom.Vec(-1, 1), geom.Vec(0, 1), }, { geom.Vec(1, 0), geom.Vec(1, -1), geom.Vec(0, -1), geom.Vec(-1, 0), geom.Vec(0, 1), geom.Vec(1, 1), }, }
DirectionsOffsetOddR lists neighbor vectors for odd-r offset coordinates as [parityRow][direction], where parityRow=0 for even rows and 1 for odd rows.
Functions ¶
func AssertFracHex ¶ added in v1.0.0
func AssertFracHex(t *testing.T, h FractionalHex, q, r float64, messages ...string)
func NeighborOffset ¶
NeighborOffset returns the neighbor offset vector for the given coordinate in the specified direction and coordinate system.
func NeighborOffsets ¶
func NeighborOffsets(index ints.Point, system CoordinateSystem) []ints.Vector
NeighborOffsets returns the 6 neighbor offsets for the given coordinate index in the specified coordinate system. For offset systems this accounts for row/column parity when determining neighbor vectors.
func NeighborOffsetsAxial ¶
NeighborOffsetsAxial returns the neighbor offsets in axial (cube) coordinate system.
func NeighborOffsetsDoubleHeight ¶
NeighborOffsetsDoubleHeight returns the neighbor offsets in double-height coordinate system.
func NeighborOffsetsDoubleWidth ¶
NeighborOffsetsDoubleWidth returns the neighbor offsets in double-width coordinate system.
func NeighborOffsetsOffsetEvenQ ¶
NeighborOffsetsOffsetEvenQ returns the neighbor offsets in even-q offset coordinate system.
func NeighborOffsetsOffsetEvenR ¶
NeighborOffsetsOffsetEvenR returns the neighbor offsets in even-r offset coordinate system.
func NeighborOffsetsOffsetOddQ ¶
NeighborOffsetsOffsetOddQ returns the neighbor offsets in odd-q offset coordinate system.
func NeighborOffsetsOffsetOddR ¶
NeighborOffsetsOffsetOddR returns the neighbor offsets in odd-r offset coordinate system.
func To ¶
func To(hex Hex, system CoordinateSystem) ints.Point
To converts an axial hex to the given coordinate system as an ints.Point.
func ToDoubleHeight ¶
ToDoubleHeight converts axial to double-height coordinates (doubling the r axis).
func ToDoubleWidth ¶
ToDoubleWidth converts axial to double-width coordinates (doubling the q axis).
func ToOffsetEvenQ ¶
ToOffsetEvenQ converts axial to even-q offset coordinates. Even columns are shifted down by +1/2 row.
func ToOffsetEvenR ¶
ToOffsetEvenR converts axial to even-r offset coordinates. Even rows are shifted right by +1/2 column.
func ToOffsetOddQ ¶
ToOffsetOddQ converts axial to odd-q offset coordinates. Odd columns are shifted down by +1/2 row.
func ToOffsetOddR ¶
ToOffsetOddR converts axial to odd-r offset coordinates. Odd rows are shifted right by +1/2 column.
Types ¶
type CoordinateSystem ¶
type CoordinateSystem int
CoordinateSystem enumerates supported hexagonal grid coordinate systems.
const ( Axial CoordinateSystem = iota // Axial (Cube) coordinates (q, r, s) OffsetOddR // Odd rows are offset (pointy-top hexes) OffsetEvenR // Even rows are offset (pointy-top hexes) OffsetOddQ // Odd columns are offset (flat-top hexes) OffsetEvenQ // Even columns are offset (flat-top hexes) DoubleWidth // Double cols (pointy-top hexes) DoubleHeight // Double rows (flat-top hexes) )
type Direction ¶
type Direction int
Direction represents one of the six neighbor directions around a hex.
Constants for the directions from a Hex. - Q+ increments q and compensates by decrementing r. - R+ increments r and compensates by decrementing s (-q-r). - S+ increments s (-q-r) compensates by decrementing q.
const ( DirectionSMinus Direction = iota // -S, flat-top SE, pointy-top E DirectionQPlus // +Q, flat-top NE, pointy-top NE DirectionRMinus // -R, flat-top N, pointy-top NW DirectionSPlus // +S, flat-top NW, pointy-top W DirectionQMinus // -Q, flat-top SW, pointy-top SW DirectionRPlus // +R, flat-top S, pointy-top SE )
func (Direction) NeighborOffset ¶
NeighborOffset returns the neighbor offset vector for the given direction.
type FractionalHex ¶
type FractionalHex struct {
Q, R float64
}
FractionalHex represents a hex with floating-point axial coordinates. Useful for interpolation and conversions from pixel space before rounding.
func (FractionalHex) Lerp ¶
func (h FractionalHex) Lerp(hex FractionalHex, t float64) FractionalHex
Lerp creates a new FractionalHex in linear interpolation towards given hex.
func (FractionalHex) Point ¶ added in v1.0.0
func (h FractionalHex) Point() floats.Point
Point returns the axial (q,r) as a floats.Point.
func (FractionalHex) QR ¶
func (h FractionalHex) QR() (float64, float64)
QR returns the (q, r) coordinates.
func (FractionalHex) QRS ¶
func (h FractionalHex) QRS() (float64, float64, float64)
QRS returns the (q, r, s) coordinates where s is implied.
func (FractionalHex) Round ¶
func (h FractionalHex) Round() Hex
Round converts a FractionalHex to the nearest Hex while preserving q+r+s=0.
func (FractionalHex) S ¶
func (h FractionalHex) S() float64
S returns the implied s coordinate (-q - r).
func (FractionalHex) String ¶
func (h FractionalHex) String() string
String returns a compact representation of the fractional hex as (q,r) with 2 decimals.
type Hex ¶
type Hex struct {
Q, R int
}
Hex represents a hexagon in axial (cube) coordinates using integer q and r. The third coordinate s is implied by s = -q - r.
func From ¶
func From(index ints.Point, system CoordinateSystem) Hex
From converts a coordinate in the given system into an axial Hex.
func FromDoubleHeight ¶
FromDoubleHeight converts a double-height coordinate to axial.
func FromDoubleWidth ¶
FromDoubleWidth converts a double-width coordinate to axial.
func FromOffsetEvenQ ¶
FromOffsetEvenQ converts an even-q offset coordinate to axial.
func FromOffsetEvenR ¶
FromOffsetEvenR converts an even-r offset coordinate to axial.
func FromOffsetOddQ ¶
FromOffsetOddQ converts an odd-q offset coordinate to axial.
func FromOffsetOddR ¶
FromOffsetOddR converts an odd-r offset coordinate to axial.
func (Hex) DistanceTo ¶
DistanceTo returns the hex distance between h and the given hex.
func (Hex) FieldOfView ¶
FieldOfView returns the subset of candidate hexes visible from this hex, taking into account a set of blocking hexagons.
func (Hex) HasLineOfSight ¶
HasLineOfSight checks if the target hex is visible from this hex, taking into account a set of blocking hexagons.
func (Hex) Line ¶
Line returns the sequence of hexes that connects this hex to target in a straight line.
func (Hex) Range ¶
Range returns the set of hexes within radius n around h, inclusive of h. When n < 0, it returns nil, when n == 0, it returns itself.
type Layout ¶
type Layout struct {
Size floats.Size // multiplication factor relative to the canonical hexagon
Origin floats.Point // center Point for hexagon with coordinates (0,0)
// contains filtered or unexported fields
}
Layout describes the mapping between hex coordinates and pixel space. It holds the hex orientation (flat-top or pointy-top), the hex Size (used as a scaling factor relative to a canonical hex), and the pixel Origin that corresponds to the axial hex (0,0) center.
func LayoutFlatTop ¶
LayoutFlatTop constructs a Layout for flat-top hexes with the given size and origin.
func LayoutPointyTop ¶
LayoutPointyTop constructs a Layout for pointy-top hexes with the given size and origin.
func (Layout) FromPoint ¶
func (l Layout) FromPoint(point floats.Point) FractionalHex
FromPoint converts a pixel point to a fractional hex in the layout.
func (Layout) Hexagon ¶
func (l Layout) Hexagon(h Hex) floats.RegularPolygon
Hexagon creates a floats.RegularPolygon representing the hex cell in pixel space.