Documentation
¶
Overview ¶
Package hexg implements hex grids in the style of the Red Blob Games blog https://www.redblobgames.com/grids/hexagons/ and https://www.redblobgames.com/grids/hexagons/implementation.html.
Index ¶
- func Version() semver.Version
- type FractionalHex
- func (h FractionalHex) Lerp(b FractionalHex, t float64) FractionalHex
- func (h FractionalHex) ReflectQ() FractionalHex
- func (h FractionalHex) ReflectR() FractionalHex
- func (h FractionalHex) ReflectS() FractionalHex
- func (h FractionalHex) Round() Hex
- func (h FractionalHex) Scale(factor float64) FractionalHex
- type Hex
- func (h Hex) Add(b Hex) Hex
- func (h Hex) ConciseString() string
- func (h Hex) CubeToQOffset(even bool) OffsetCoord
- func (h Hex) CubeToROffset(even bool) OffsetCoord
- func (h Hex) DiagonalNeighbor(direction int) Hex
- func (h Hex) Distance(b Hex) int
- func (h Hex) Equals(b Hex) bool
- func (h Hex) Length() int
- func (h Hex) Lerp(b Hex, t float64) FractionalHex
- func (h Hex) LineDraw(b Hex) []Hex
- func (h Hex) LineDrawNudged(b Hex) []Hex
- func (h Hex) MarshalJSON() ([]byte, error)
- func (h Hex) Multiply(k int) Hex
- func (h Hex) Neighbor(direction int) Hex
- func (h Hex) NotEquals(b Hex) bool
- func (h Hex) Q() int
- func (h Hex) QRS() (int, int, int)
- func (h Hex) R() int
- func (h Hex) ReflectQ() Hex
- func (h Hex) ReflectR() Hex
- func (h Hex) ReflectS() Hex
- func (h Hex) Ring(radius int) []Hex
- func (h Hex) RotateLeft() Hex
- func (h Hex) RotateRight() Hex
- func (h Hex) S() int
- func (h Hex) Scale(factor int) Hex
- func (h *Hex) Scan(src any) (err error)
- func (h Hex) Spiral(radius int) []Hex
- func (h Hex) String() string
- func (h Hex) Subtract(b Hex) Hex
- func (h *Hex) UnmarshalJSON(data []byte) error
- func (h Hex) Value() (driver.Value, error)
- type HexSet
- type Layout
- func (l *Layout) BoundingBox(hexes ...Hex) (upperLeft, lowerRight Hex)
- func (l *Layout) CubeToOffset(h Hex) OffsetCoord
- func (l *Layout) HexCornerOffset(corner int) Point
- func (l *Layout) HexToPixel(h Hex) Point
- func (l *Layout) Hexagon(radius int) HexSet
- func (l *Layout) IsEven() bool
- func (l *Layout) IsEvenQ() bool
- func (l *Layout) IsEvenR() bool
- func (l *Layout) IsFlat() bool
- func (l *Layout) IsHorizontal() bool
- func (l *Layout) IsOdd() bool
- func (l *Layout) IsOddQ() bool
- func (l *Layout) IsOddR() bool
- func (l *Layout) IsPointy() bool
- func (l *Layout) IsVertical() bool
- func (l *Layout) OffsetToCube(oc OffsetCoord) Hex
- func (l *Layout) ParallelogramQR(q1, r1 int, q2, r2 int) HexSet
- func (l *Layout) ParallelogramQS(q1, s1 int, q2, s2 int) HexSet
- func (l *Layout) ParallelogramRS(r1, s1 int, r2, s2 int) HexSet
- func (l *Layout) PixelToFractionalHex(p Point) FractionalHex
- func (l *Layout) PixelToHexRounded(p Point) Hex
- func (l *Layout) PolygonCorners(h Hex) [6]Point
- func (l *Layout) Rectangle(left, right, top, bottom int) HexSet
- func (l *Layout) RotateLeft(h Hex) Hex
- func (l *Layout) RotateRight(h Hex) Hex
- func (l *Layout) TriangleLeftRight(mapSize int) HexSet
- func (l *Layout) TriangleUpDown(mapSize int) HexSet
- type LayoutOffset
- type OffsetCoord
- type Point
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type FractionalHex ¶
type FractionalHex struct {
// contains filtered or unexported fields
}
FractionalHex implements a hex with floating point coordinates. Used for linear interpolation and rounding.
func NewFractionalHex ¶
func NewFractionalHex(q, r, s float64) FractionalHex
NewFractionalHex returns a FractionalHex initialized from cube coordinates.
func (FractionalHex) Lerp ¶
func (h FractionalHex) Lerp(b FractionalHex, t float64) FractionalHex
Lerp returns the linear interpolation of points on the line between two hexes
func (FractionalHex) ReflectQ ¶
func (h FractionalHex) ReflectQ() FractionalHex
ReflectQ reflects the fractional hex across the Q axis.
func (FractionalHex) ReflectR ¶
func (h FractionalHex) ReflectR() FractionalHex
ReflectR reflects the fractional hex across the R axis.
func (FractionalHex) ReflectS ¶
func (h FractionalHex) ReflectS() FractionalHex
ReflectS reflects the fractional hex across the S axis.
func (FractionalHex) Round ¶
func (h FractionalHex) Round() Hex
Round turns a fractional hex coordinate into the nearest integer hex coordinate.
func (FractionalHex) Scale ¶
func (h FractionalHex) Scale(factor float64) FractionalHex
Scale returns a FractionalHex scaled by a float64 factor.
type Hex ¶
type Hex struct {
// contains filtered or unexported fields
}
Hex stores the `q`, `r`, and `s` coordinates.
func DiagonalDirectionVector ¶
DiagonalDirectionVector returns the unit vector for a diagonal hex direction. Direction is coerced to the range 0..5.
func DirectionVector ¶
DirectionVector returns the unit vector for a hex direction. Direction must be in the range 0..5. Panics on invalid input.
func (Hex) ConciseString ¶
ConciseString returns the coordinates with signs. It returns the coordinates formatted as "+q+r+s".
func (Hex) CubeToQOffset ¶
func (h Hex) CubeToQOffset(even bool) OffsetCoord
CubeToQOffset converts cube coordinates to q-offset coordinates. If even is true, uses even-q offset; otherwise uses odd-q offset.
func (Hex) CubeToROffset ¶
func (h Hex) CubeToROffset(even bool) OffsetCoord
CubeToROffset converts cube coordinates to r-offset coordinates. If even is true, uses even-r offset; otherwise uses odd-r offset.
func (Hex) DiagonalNeighbor ¶
DiagonalNeighbor returns the hex that is one diagonal step away in the given direction. Direction is coerced to the range 0..5.
func (Hex) Lerp ¶
func (h Hex) Lerp(b Hex, t float64) FractionalHex
Lerp returns the linear interpolation of points on the line between two hexes
func (Hex) LineDrawNudged ¶
LineDrawNudged returns the set of hexes on the line between two hexes, with nudging to push points on an edge in a consistent direction.
func (Hex) MarshalJSON ¶
MarshalJSON implements json.Marshaler for Hex.
func (Hex) Neighbor ¶
Neighbor returns the hex that is one step away in the given direction. Direction is coerced to the range 0..5.
func (Hex) Ring ¶
Ring returns all hexes at exactly the given radius from the center hex. Returns a single-element slice containing the center if radius is 0. Panics if radius is negative.
func (Hex) RotateLeft ¶
RotateLeft shifts the q, r, and s coordinates. The effect depends on the layout.
func (Hex) RotateRight ¶
RotateRight shifts the q, r, and s coordinates. The effect depends on the layout.
func (Hex) Scale ¶
Scale returns a Hex scaled by an integer factor. This is an alias for Multiply for API consistency.
func (Hex) Spiral ¶
Spiral returns all hexes within the given radius from the center hex, starting from the center and spiraling outward. Panics if radius is negative.
func (*Hex) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler for Hex.
type Layout ¶
type Layout struct {
// contains filtered or unexported fields
}
Layout implements the conversion between hex (q,r,s) and screen (x,y) coordinates. The conversion uses a matrix as well as the inverse of the matrix for each orientation.
func NewLayout ¶
func NewLayout(offset LayoutOffset, size, origin Point) Layout
NewLayout returns a layout with either flat-top or pointy-top hexes.
Flat-top layouts have vertical columns and staggered rows. Drawing corners start at 0°.
Pointy-top layouts have staggered columns and horizontal rows. Drawing corners start at 30°.
func (*Layout) BoundingBox ¶
BoundingBox returns the bounding box of a list of hexes. If the list is empty, returns (0,0,0), (0,0,0)
func (*Layout) CubeToOffset ¶
func (l *Layout) CubeToOffset(h Hex) OffsetCoord
CubeToOffset converts cube coordinates to offset coordinates based on the layout.
func (*Layout) HexCornerOffset ¶
HexCornerOffset returns the screen location (pixel) of a corner of a hex on the grid.
func (*Layout) HexToPixel ¶
HexToPixel returns the origin of the hex on the grid as a Point.
func (*Layout) Hexagon ¶
Hexagon returns a grid centered about (0,0,0). Does not depend on the orientation of the grid.
func (*Layout) IsEvenQ ¶
IsEvenQ returns true if the layout supports even-q offset coordinates (vertical layout, shoves even columns down).
func (*Layout) IsEvenR ¶
IsEvenR returns true if the layout supports even-r offset coordinates (horizontal layout, shoves even rows right).
func (*Layout) IsHorizontal ¶
IsHorizontal returns true if the layout has horizontal rows.
func (*Layout) IsOddQ ¶
IsOddQ returns true if the layout supports odd-q offset coordinates (vertical layout, shoves odd columns down).
func (*Layout) IsOddR ¶
IsOddR returns true if the layout supports odd-r offset coordinates (horizontal layout, shoves odd rows right).
func (*Layout) IsVertical ¶
IsVertical returns true if the layout has vertical columns.
func (*Layout) OffsetToCube ¶
func (l *Layout) OffsetToCube(oc OffsetCoord) Hex
OffsetToCube converts offset coordinates to cube coordinates based on the layout.
func (*Layout) ParallelogramQR ¶
ParallelogramQR returns a parallelogram-shaped grid using q and r axes.
func (*Layout) ParallelogramQS ¶
ParallelogramQS returns a parallelogram-shaped grid using q and s axes.
func (*Layout) ParallelogramRS ¶
ParallelogramRS returns a parallelogram-shaped grid using r and s axes.
func (*Layout) PixelToFractionalHex ¶
func (l *Layout) PixelToFractionalHex(p Point) FractionalHex
PixelToFractionalHex returns the fractional hex that encloses the pixel. In theory, the origin of that fractional hex will be the pixel.
func (*Layout) PixelToHexRounded ¶
PixelToHexRounded converts a screen pixel to the nearest hex coordinate.
func (*Layout) PolygonCorners ¶
PolygonCorners returns the location of the six corners of the hex on the grid.
func (*Layout) Rectangle ¶
Rectangle returns a grid centered about (0,0,0). The internal logic depends on the orientation of the grid.
func (*Layout) RotateLeft ¶
RotateLeft rotates the hex 60° counter-clockwise around the origin. The underlying operation depends on the layout orientation.
func (*Layout) RotateRight ¶
RotateRight rotates the hex 60° clockwise around the origin. The underlying operation depends on the layout orientation.
func (*Layout) TriangleLeftRight ¶
TriangleLeftRight returns a grid originating at (0,0,0). mapSize is the length of a side.
func (*Layout) TriangleUpDown ¶
TriangleUpDown returns a grid originating at (0,0,0). mapSize is the length of a side.
type LayoutOffset ¶
type LayoutOffset int
LayoutOffset describes the layout (horizontal row or vertical columns) and which way odd rows and columns are pushed or pulled.
const ( // OddR is a pointy-top layout with horizontal rows that shoves odd rows right OddR LayoutOffset = iota // EvenR is a pointy-top layout with horizontal rows that shoves even rows right EvenR // OddQ is a flat-top layout with vertical columns that shoves odd columns down OddQ // EvenQ is a flat-top layout with vertical columns that shoves even columns down EvenQ )
type OffsetCoord ¶
type OffsetCoord struct {
Col, Row int
}
OffsetCoord represents offset coordinates (col, row) for hex grids.
func NewOffsetCoord ¶
func NewOffsetCoord(col, row int) OffsetCoord
NewOffsetCoord returns an OffsetCoord initialized with the given column and row.
func (OffsetCoord) ConciseString ¶
func (oc OffsetCoord) ConciseString() string
ConciseString returns the coordinates formatted as "+col+row".
func (OffsetCoord) QOffsetToCube ¶
func (oc OffsetCoord) QOffsetToCube(even bool) Hex
QOffsetToCube converts q-offset coordinates to cube coordinates. If even is true, uses even-q offset; otherwise uses odd-q offset.
func (OffsetCoord) ROffsetToCube ¶
func (oc OffsetCoord) ROffsetToCube(even bool) Hex
ROffsetToCube converts r-offset coordinates to cube coordinates. If even is true, uses even-r offset; otherwise uses odd-r offset.
func (OffsetCoord) String ¶
func (oc OffsetCoord) String() string
String returns the coordinates formatted as "(col,row)".