hex

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2021 License: BSD-3-Clause Imports: 4 Imported by: 2

README

hex logo

Go Report Card Travis CI GoDoc

hex is a Go implementation of hexagonal grid math based on amitp's Hexagonal Grids articles. This package focuses on hexagonal grid math, including:

  • Generating sets of hexes programmatically in common patterns.
  • Compositing sets of hexes with unions, intersections, and subtractions (constructive solid geometry).
  • Multithreaded A* pathing in a hex grid.
  • Fast intersection testing.
  • Super naive drawing package! This isn't performant; it's to help you visualize what's going on.
import (
    // Base library
    "github.com/erinpentecost/hex"
    // For pathfinding
    "github.com/erinpentecost/hex/path"
    // For constructive solid geometry
    "github.com/erinpentecost/hex/area"
)

hex map

References

Documentation

Overview

package hex contains discrete and real positional values for coordinates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoundFacing

func BoundFacing(facing int) int

BoundFacing maps the whole number set to 0-5.

Types

type Hex

type Hex struct {
	Q int64
	R int64
}

Hex is a coordinate defined axially.

[Q,R,S]

func Direction

func Direction(direction int) Hex

Direction returns a new hex coord offset from the origin in the given direction, which is a number from 0 to 5, inclusive. Positive Q axis is in the 0 direction. Positive R axis is in the 5 direction.

func LerpHex

func LerpHex(a Hex, b Hex, t float64) Hex

LerpHex finds a point between a and b weighted by t. See https://en.wikipedia.org/wiki/Linear_interpolation

func Origin

func Origin() Hex

Origin returns a new hex with origin (0,0) coordinates.

func (Hex) Add

func (h Hex) Add(x Hex) Hex

Add combines two hexes.

func (Hex) DistanceTo

func (h Hex) DistanceTo(x Hex) int64

DistanceTo returns the distance between two hexes.

This is the Manhattan Distance.

func (Hex) Length

func (h Hex) Length() int64

Length gets the length of the hex to the grid origin.

This is the Manhattan Distance.

func (Hex) LineTo

func (h Hex) LineTo(x Hex) []Hex

LineTo returns all hexes in a line from point x to point b, inclusive. The order of elements is a line as you would expect.

func (Hex) Multiply

func (h Hex) Multiply(k int64) Hex

Multiply scales a hex by a scalar value.

func (Hex) Neighbor

func (h Hex) Neighbor(direction int) Hex

Neighbor returns the neighbor in the given directon.

func (Hex) Neighbors

func (h Hex) Neighbors() []Hex

Neighbors returns the neighbors.

func (Hex) Rotate

func (h Hex) Rotate(pivot Hex, direction int) Hex

func (Hex) S

func (h Hex) S() int64

S is the implicit additional coordinate when using cubic coordinate system.

func (Hex) String

func (h Hex) String() string

ToString converts the hex to a string.

func (Hex) Subtract

func (h Hex) Subtract(x Hex) Hex

Subtract combines two hexes.

func (Hex) ToHexFractional

func (h Hex) ToHexFractional() HexFractional

ToHexFractional returns the fractional hex that is the center of this hex.

func (Hex) Transform

func (h Hex) Transform(t [4][4]int64) Hex

Transform applies a matrix transformation on the hex.

Translation by tr,tq,ts:

[[1,0,0,tr]

[0,1,0,tq]

[0,0,1,ts]

[0,0,0,1]] // homogenous coords. ignored.

type HexFractional

type HexFractional struct {
	Q float64
	R float64
}

HexFractional is fractional hex coordinates in cubic coordinate system.

func Center

func Center(h ...Hex) HexFractional

Center returns the hex at the center of mass of the given points.

func HexFractionalFromCartesian

func HexFractionalFromCartesian(x, y float64) HexFractional

HexFractionalFromCartesian returns the hex in Cartesian Coordinates.

func LerpHexFractional

func LerpHexFractional(a HexFractional, b HexFractional, t float64) HexFractional

LerpHexFractional finds a point between a and b weighted by t. See https://en.wikipedia.org/wiki/Linear_interpolation

func OriginFractional

func OriginFractional() HexFractional

OriginFractional returns a new hex with origin (0,0) coordinates.

func (HexFractional) Add

Add combines two hexes.

func (HexFractional) AlmostEquals

func (h HexFractional) AlmostEquals(x HexFractional) bool

AlmostEquals returns true when h and x are equal or close enough to equal for practical matters.

func (HexFractional) AngleTo

func (h HexFractional) AngleTo(x HexFractional) float64

AngleTo returns the angle to x in radians. Will always return the inner (smaller) angle.

func (HexFractional) DistanceTo

func (h HexFractional) DistanceTo(x HexFractional) float64

DistanceTo returns the distance between two hexes.

This is the Euclidean Distance, but NOT in orthogonal space.

func (HexFractional) Length

func (h HexFractional) Length() float64

Length gets the length of the hex to the grid origin.

This is the Euclidean Distance, but NOT in orthogonal space.

func (HexFractional) Multiply

func (h HexFractional) Multiply(k float64) HexFractional

Multiply scales a hex by a scalar value.

func (HexFractional) Normalize

func (h HexFractional) Normalize() HexFractional

Normalize returns a vector that points in the same direction but has a length of 1.

func (HexFractional) ProjectOn

func (h HexFractional) ProjectOn(x HexFractional) HexFractional

ProjectOn projects h onto x. It returns a vector parallel to x.

func (HexFractional) Rotate

func (h HexFractional) Rotate(center HexFractional, radians float64) HexFractional

Rotate should move a hex about a center point counterclockwise by some number of radians.

func (HexFractional) S

func (h HexFractional) S() float64

S is the implicit additional coordinate when using cubic coordinate system.

func (HexFractional) String

func (h HexFractional) String() string

String converts the hex to a string.

func (HexFractional) Subtract

Subtract combines two hexes.

func (HexFractional) ToCartesian

func (h HexFractional) ToCartesian() (x, y float64)

ToCartesian returns the hex in Cartesian Coordinates.

func (HexFractional) ToHex

func (h HexFractional) ToHex() Hex

ToHex takes in fractional hex coordinates in cubic coordinates and rounds them to the nearest actual hex coordinate. This is all in normal coordinate space, not screen space.

type Sort

type Sort []Hex

func (Sort) Len

func (s Sort) Len() int

func (Sort) Less

func (s Sort) Less(i int, j int) bool

func (Sort) Swap

func (s Sort) Swap(i int, j int)

Directories

Path Synopsis
package area provides constructive solid geometry-style methods for building collections of hexes.
package area provides constructive solid geometry-style methods for building collections of hexes.
examples
drawhx Module
mesh Module
Package path has a pathfinding algorithm that operates on hexcoords.
Package path has a pathfinding algorithm that operates on hexcoords.

Jump to

Keyboard shortcuts

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