package module
Version: v0.0.0-...-42796ac Latest Latest

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

Go to latest
Published: Jan 26, 2019 License: MIT Imports: 2 Imported by: 0



This is a GO (Golang) library used to handle regular hexagons. It's based on the algorithms described at http://www.redblobgames.com/grids/hexagons/implementation.html


go get github.com/pmcxs/hexgrid


import "github.com/pmcxs/hexgrid"
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 := HexDistance(hexagonA, hexagonB)
Getting the array of hexagons on the path between two hexagons
origin := NewHex(10,20)
destination := NewHex(30,40)
path := HexLineDraw(origin, 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()


  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request


0.1. First version






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



This section is empty.


This section is empty.


func HexAdd

func HexAdd(a, b hex) hex

Adds two hexagons

func HexCornerOffset

func HexCornerOffset(l layout, c int) point

func HexDistance

func HexDistance(a, b hex) int

func HexFieldOfView

func HexFieldOfView(source hex, candidates []hex, blocking []hex) []hex

Returns the list of hexagons that are visible from a given hexagon

func HexHasLineOfSight

func HexHasLineOfSight(center hex, target hex, blocking []hex) bool

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

func HexLength

func HexLength(hex hex) int

func HexLineDraw

func HexLineDraw(a, b hex) []hex

Returns the slice of hexagons that exist on a line that goes from hexagon a to hexagon b

func HexNeighbor

func HexNeighbor(h hex, direction direction) hex

Returns the neighbor hexagon at a certain direction

func HexRange

func HexRange(center hex, radius int) []hex

Returns the set of hexagons around a certain center for a given radius

func HexRectangleGrid

func HexRectangleGrid(width, height int) []hex

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

func HexScale

func HexScale(a hex, k int) hex

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

func HexSubtract

func HexSubtract(a, b hex) hex

Subtracts two hexagons

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

func NewFractionalHex

func NewFractionalHex(q, r float64) fractionalHex

func NewHex

func NewHex(q, r int) hex

func PixelToHex

func PixelToHex(l layout, p point) fractionalHex

PixelToHex returns the corresponding hexagon axial coordinates for a given pixel on a certain layout


This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL