hexgrid

package module
v0.0.0-...-2d2ed6b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2025 License: MIT Imports: 3 Imported by: 0

README

Hexgrid

This is a fork of https://github.com/Laminator42/hexgrid which in turn was forked from https://github.com/pmcxs/hexgrid. It's based on the algorithms described at http://www.redblobgames.com/grids/hexagons/implementation.html.

Installation

go get github.com/bkhl/hexgrid

Usage

Importing
import "github.com/bkhl/hexgrid"
Examples
Creating hexagons
hexagonA := NewHex(1,2) //at axial coordinates Q=1 R=2
hexagonB := NewHex(2,3) //at axial coordinates Q=2 R=3
Measuring the distance (in hexagons) between two hexagons
distance := hexagonA.Distance(hexagonB)
Getting the array of hexagons on the path between two hexagons
origin := NewHex(10,20)
destination := NewHex(30,40)
path := origin.LineDraw(destination)
Creating a layout
origin := point {0,0}     // The coordinate that corresponds to the center of hexagon 0,0
size := point {100, 100}  // The length of an hexagon side => 100
layout: = layout{size, origin, orientationFlat}
Obtaining the pixel that corresponds to a given hexagon
hex := NewHex(1,0)
pixel := HexToPixel(layout,hex)  // Pixel that corresponds to the center of hex 1,0 (in the given layout)
Obtaining the hexagon that contains the given pixel (and rounding it)
point := point {10,20}
hex := PixelToHex(layout, point).Round()

Credits

License

MIT

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

View Source
const (
	DirectionSE = iota
	DirectionNE
	DirectionN
	DirectionNW
	DirectionSW
	DirectionS
)

Variables

View Source
var OrientationFlat orientation = orientation{3. / 2., 0., math.Sqrt(3.) / 2., math.Sqrt(3.), 2. / 3., 0., -1. / 3., math.Sqrt(3.) / 3., 0.}
View Source
var OrientationPointy orientation = orientation{math.Sqrt(3.), math.Sqrt(3.) / 2., 0., 3. / 2., math.Sqrt(3.) / 3., -1. / 3., 0., 2. / 3., 0.5}

Functions

This section is empty.

Types

type Direction

type Direction int

Direction represents a one-step move in some direction. The direction names assumes a flat-top layout.

type FractionalHex

type FractionalHex struct {
	Q float64
	R float64
	S float64
}

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

Rounds a FractionalHex to a Regular Hex

type Hex

type Hex struct {
	Q, R, S int
}

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):

For additional reference on these coordinate systems: http://www.redblobgames.com/grids/hexagons/#coordinates

func NewHex

func NewHex(q, r int) Hex

func RectangleGrid

func RectangleGrid(width, height int) []Hex

Returns the set of hexagons that form a rectangle with the specified width and height

func (Hex) Add

func (h Hex) Add(o Hex) Hex

Adds another hexagon

func (Hex) Distance

func (h Hex) Distance(o Hex) int

func (Hex) FieldOfView

func (h Hex) FieldOfView(candidates []Hex, blocking []Hex) []Hex

Returns the list of hexagons that are visible from the hexagon

func (Hex) HasLineOfSight

func (h Hex) HasLineOfSight(target Hex, blocking []Hex) bool

Determines if a given hexagon is visible from the hexagon, taking into consideration a set of blocking hexagons

func (Hex) Length

func (h Hex) Length() int

func (Hex) LineDraw

func (h Hex) LineDraw(o Hex) []Hex

Returns the slice of hexagons that exist on a line that goes from the hexagon to another TODO: Name doesn't seem to fit

func (Hex) Neighbor

func (h Hex) Neighbor(direction Direction) Hex

Returns the neighbor hexagon at a certain direction TODO: Not sure if I like this approach

func (Hex) Neighbors

func (h Hex) Neighbors() []Hex

Returns all neighboring hexagons

func (Hex) Range

func (h Hex) Range(r int) []Hex

Returns the set of hexagons around the hexagon for a given radius

func (Hex) Scale

func (h Hex) Scale(k int) Hex

Scales an hexagon by a k factor. If factor k is 1 there's no change

func (Hex) String

func (h Hex) String() string

func (Hex) Subtract

func (h Hex) Subtract(o Hex) Hex

Subtracts another hexagon

type Layout

type Layout struct {
	Orientation orientation
	Size        Point // multiplication factor relative to the canonical hexagon, where the points are on a unit circle
	Origin      Point // center Point for hexagon 0,0
}

type Point

type Point struct {
	X float64
	Y float64
}

func HexCornerOffset

func HexCornerOffset(l Layout, c int) Point

func HexToPixel

func HexToPixel(l Layout, h Hex) Point

HexToPixel returns the center pixel for a given hexagon an a certain Layout

func HexagonCorners

func HexagonCorners(l Layout, h Hex) []Point

Gets the corners of the hexagon for the given Layout, starting at the E vertex and proceeding in a CCW order

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL