Documentation
¶
Overview ¶
Package hexgrid provides various utilities to handle regular hexagons math This is a Go implementation of the algorithms described at http://www.redblobgames.com/grids/hexagons/implementation.html
Index ¶
- Constants
- Variables
- type Direction
- type FractionalHex
- type Hex
- func (h Hex) Add(b Hex) Hex
- func (h Hex) DistanceTo(b 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) LineDraw(b Hex) []Hex
- func (h Hex) Neighbor(dir Direction) Hex
- func (h Hex) Range(radius int) []Hex
- func (h Hex) Scale(k int) Hex
- func (h Hex) String() string
- func (h Hex) Subtract(b Hex) Hex
- type Layout
- type Orientation
- type Point
Constants ¶
const ( // DirectionSE is the Southeast side of a hex DirectionSE = iota // DirectionNE is the Northeast side of a hex DirectionNE // DirectionN is the North side of a hex DirectionN // DirectionNW is the Northwest side of a hex DirectionNW // DirectionSW is the Southwest side of a hex DirectionSW // DirectionS is the South side of a hex DirectionS )
Variables ¶
var ( // OrientationPointy ... OrientationPointy = Orientation{ math.Sqrt(3.), math.Sqrt(3.) / 2., 0., 3. / 2., math.Sqrt(3.) / 3., -1. / 3., 0., 2. / 3., 0.5} // OrientationFlat ... OrientationFlat = Orientation{ 3. / 2., 0., math.Sqrt(3.) / 2., math.Sqrt(3.), 2. / 3., 0., -1. / 3., math.Sqrt(3.) / 3., 0.} )
Functions ¶
This section is empty.
Types ¶
type FractionalHex ¶
FractionHex provides a more precise representation for hexagons when precision is required. It's also represented in Cube Coordinates
func NewFractionalHex ¶
func NewFractionalHex(q, r float64) FractionalHex
func PixelToHex ¶
func PixelToHex(l Layout, p Point) FractionalHex
PixelToHex returns the corresponding hexagon axial coordinates for a given pixel on a certain layout
func (FractionalHex) Round ¶
func (h FractionalHex) Round() Hex
Round returns a 'rounded' FractionalHex, returning a Regular Hex
type Hex ¶
Hex describes a regular hexagon with Cube Coordinates (although the S coordinate is computed on the constructor)
It's also easy to reference them as axial (trapezoidal coordinates): - R represents the vertical axis - Q the diagonal one - S can be ignored
For additional reference on these coordinate systems, see http://www.redblobgames.com/grids/hexagons/#coordinates
_ _ / \ _ _ /(0,-1) \ _ _ / \ -R / \
/(-1,0) \ _ _ /(1,-1) \ \ -Q / \ /
\ _ _ / (0,0) \ _ _ / / \ / \
/(-1,1) \ _ _ / (1,0) \ \ / \ +Q /
\ _ _ / (0,1) \ _ _ / \ +R / \ _ _ /
func HexagonalGrid ¶
HexagonalGrid returns a slice of hexagons that form a hexagon with the specified radius.
func RectangleGrid ¶
RectangleGrid returns the set of hexagons that form a rectangle with the specified width and height
func RectangularGrid ¶
RectangularGrid returns the set of hexagons that form a rectangle with the specified width and height
func (Hex) DistanceTo ¶
DistanceTo returns the distance from the Hex 'h' to the Hex 'b'
func (Hex) FieldOfView ¶
FieldOfView feturns the list of hexagons that are visible from this Hex
func (Hex) HasLineOfSight ¶
HasLineOfSight determines if a given hexagon is visible from another hexagon, taking into consideration a set of blocking hexagons
func (Hex) LineDraw ¶
LineDraw returns the slice of hexagons that exist on a line that goes from hexagon a to hexagon b
type Layout ¶
type Layout struct { Orientation Orientation // Size defines the multiplication factor relative to the canonical // hexagon, where the points are on a unit circle Size Point // Origin defines the center point for hexagon at 0,0 Origin Point }
Layout defines a data struct that holds the parameters of a layout of hex tiles
type Orientation ¶
type Orientation struct {
// contains filtered or unexported fields
}
Orientation defines the orientation of a layout of hex; pointy or flat
type Point ¶
Point defines a geometric point with coordinates (x,y)
func EdgeOffset ¶
EdgeOffset returns the edge offset of the hexago for the given layout and edge number, starting at the E vertex and proceeding in a counter-clockwise order
func Edges ¶
Edges returns the corners of the hexagon for the given layout, starting at the E vertex and proceeding in a CCW order
func HexToPixel ¶
HexToPixel returns the center pixel for a given hexagon an a certain layout