hex

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2021 License: MIT Imports: 2 Imported by: 0

README

hex

GoDoc

Realization of hexagonal grids

This library is representation of hexagonal grids that describe in article from Red Blob Games

How to use

package main

import (
	"fmt"
	
	"github.com/VKoptev/hex"
)

func main() {
	h := hex.New(1, 2)

	fmt.Printf("equal: %v", h.Equal(hex.ZE))
	fmt.Printf("to east: %v", h.Add(hex.EE))
	fmt.Printf("to west: %v", h.Sub(hex.EE))
	fmt.Printf("no way: %v", hex.ZE.Mul(10))
	// ...
}

Documentation

Overview

Package hex contains geometry of hexagonal world.

Index

Constants

View Source
const (
	// DirectionCount is const to be used for arrays and calculations.
	DirectionCount = 6
)

Variables

View Source
var (
	ZE = Hex{0, 0, 0}
	EE = Hex{1, 0, -1}
	NE = Hex{1, -1, 0}
	NW = Hex{0, -1, 1}
	WW = Hex{-1, 0, 1}
	SW = Hex{-1, 1, 0}
	SE = Hex{0, 1, -1}
)

Directions.

Functions

func AreaAtDistance

func AreaAtDistance(r int) int

AreaAtDistance returns amount of all hexes limited by ring at specified distance including center hex and ring.

func CompareSlices

func CompareSlices(a, b []Hex) bool

CompareSlices returns true if slices a and b have equal length and elements at same positions.

func InverseDirection

func InverseDirection(d int) int

InverseDirection returns inverter direction for specified.

func NormalizeDirection

func NormalizeDirection(d int) int

NormalizeDirection returns normalized value direction for specified.

func PrettyDirection

func PrettyDirection(d int) string

PrettyDirection returns pretty representation of direction.

func RingLenAtDistance

func RingLenAtDistance(r int) int

RingLenAtDistance returns length of ring at specified distance.

Types

type Hex

type Hex struct {
	// contains filtered or unexported fields
}

Hex is coordinates at Hexagonal Grid.

func Direction

func Direction(d int) Hex

Direction returns hex by direction.

func New

func New(q, r int) Hex

New returns Hex object. It's convenient to use object but neither pointer.

func NewWithArray

func NewWithArray(a [2]int) Hex

NewWithArray returns new Hex object represented as array.

func (Hex) Add

func (h Hex) Add(t Hex) Hex

Add returns Hex that equals sum of h and t.

func (Hex) Array

func (h Hex) Array() [2]int

Array returns base type representation of Hex.

func (Hex) Direction

func (h Hex) Direction(t Hex, sign LineSign) int

Direction returns index of direction.

func (Hex) Distance

func (h Hex) Distance(t Hex) int

Distance returns distance between h and t.

func (Hex) Equal

func (h Hex) Equal(t Hex) bool

Equal returns true if Hexes h and t are equal.

func (Hex) Len

func (h Hex) Len() int

Len returns radius-vector of Hex.

func (Hex) Line

func (h Hex) Line(t Hex, sign LineSign) (res []Hex)

Line returns Hexes from h to t.

func (Hex) Mul

func (h Hex) Mul(k int) Hex

Mul returns Hex that equals h multiplied by k.

func (Hex) Neighbor

func (h Hex) Neighbor(d int) Hex

Neighbor returns neighbor Hex of h to direction d.

func (Hex) NeighborsAtDistance

func (h Hex) NeighborsAtDistance(d int, res []Hex)

NeighborsAtDistance returns all neighbors Hex of h on distance d.

func (Hex) Q

func (h Hex) Q() int

Q returns Hex.q coordinate.

func (Hex) R

func (h Hex) R() int

R returns Hex.r coordinate.

func (Hex) RingAtDistance

func (h Hex) RingAtDistance(d int, res []Hex)

RingAtDistance returns ring.

func (Hex) S

func (h Hex) S() int

S returns Hex.s coordinate.

func (Hex) String

func (h Hex) String() string

String returns string representation of Hex. It implements Stringer interface.

func (Hex) Sub

func (h Hex) Sub(t Hex) Hex

Sub returns Hex that equals difference of h and t.

func (Hex) Title

func (h Hex) Title() string

Title returns string representation of Hex.

type LineSign added in v1.1.0

type LineSign float64

LineSign is alias for float64 to determine it from other real values.

const (
	LSPlus  LineSign = 1
	LSMinus LineSign = -1
	LSZero  LineSign = 0
)

Line signs. LSPlus means counterclockwise path with ray connecting start hex with end one being traced on edge. LSMinus means clockwise path with ray connecting start hex with end one being traced on edge. LSZero means that there are no such ray or unknown direction.

func (LineSign) String added in v1.1.0

func (ls LineSign) String() string

String implements fmt.Stringer interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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