Skip to content
This repository was archived by the owner on Dec 19, 2025. It is now read-only.

bkhl/hexgrid

 
 

Repository files navigation

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

About

Go library to manage hex grids

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages

  • Go 94.5%
  • Makefile 2.9%
  • Emacs Lisp 2.6%