hexagolang

package module
v0.0.0-...-ac7a7d0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2020 License: BSD-3-Clause Imports: 2 Imported by: 0

README

hexagolang

hexagolang is a hexagon library for golang based on https://www.redblobgames.com/grids/hexagons/.

My typical usage looks like this:

package main

import (
	"fmt"
	"image"

	hex "github.com/hautenessa/hexagolang"
)

func main() {
	hexagonRadius := 32           // The radius of the hexagon, also the length of each side.
	screenOrigin := image.Point{} // origin to use when converting between screen and hexagon coordinates.

	// The layout object is responsible for converting between screen and hexagon coordinates.
	layout := hex.MakeLayout(hexagonRadius, screenOrigin, hex.OrientationFlat)

	myFirstHex := hex.H{1, 0}                   // Uses axial coordinates.
	screenPoint := layout.CenterFor(myFirstHex) // convert the hexagon center into screen coordinates.
	fmt.Printf("screen point %+v\n", screenPoint)

	// Create a line between two points and list the hexagon steps.
	mySecondHex := hex.H{4, 2}
	for h, gon := range hex.Line(myFirstHex, mySecondHex) {
		fmt.Printf("%d: %v", h, gon)
	}
	fmt.Printf("\n")

    // Convert a random screen position into a hexagon coordinate. 
	mousePoint := image.Point{800, 600}
	myThirdHex := layout.HexFor(mousePoint)
	fmt.Printf("hex for mousepoint %v\n", myThirdHex)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsPoint

func AsPoint(f F) image.Point

AsPoint makes a point from an F value

func Length

func Length(d D) int

Length returns the manhattan distance for a delta

func Range

func Range(h H, rad int) map[H]bool

Range returns the slice of all points in a distance from a point.

func Ring

func Ring(h H, rad int) map[H]bool

Ring returns the ring of hex points specific manhattan distance from h.

Types

type D

type D struct {
	Q, R, S int
}

D is the amount of change between two hexagons.

func DiagonalDelta

func DiagonalDelta(d DirectionEnum) D

DiagonalDelta returns the delta required to move a single hex in a direction.

func Multiply

func Multiply(d D, k int) D

Multiply a delta by a fixed amount (x(a))

func NeighborDelta

func NeighborDelta(d DirectionEnum) D

NeighborDelta returns the delta required to move a single hex in a direction.

func Subtract

func Subtract(a, b H) D

Subtract the coordinates of the second hexagon from the first hexagon. (a - b)

func (D) Abs

func (d D) Abs() D

Abs returns the delta as absolute values. Cmath.Abs(delta)

func (D) Hex

func (d D) Hex() H

Hex converts the delta to a hex.

type Diagonal

type Diagonal int

Diagonal represents the direction of each point on a hex.

const (
	DiagonalPosQ Diagonal = iota
	DiagonalNegR
	DiagonalPosS
	DiagonalNegQ
	DiagonalPosR
	DiagonalNegS
	DiagonalUndefined
)

Constants for the ddiagonal from a Hex

func (Diagonal) String

func (d Diagonal) String() string

String returns the string name of the direction.

type DirectionEnum

type DirectionEnum int

DirectionEnum represents the directions of each of the sides of a hex.

const (
	DirectionPosQ DirectionEnum = iota
	DirectionNegR
	DirectionPosS
	DirectionNegQ
	DirectionPosR
	DirectionNegS
	DirectionUndefined
)

Constants for the directions from a Hex.

func Direction

func Direction(d D) DirectionEnum

Direction returns the Direction one point is in comparison to another point.

func (DirectionEnum) String

func (d DirectionEnum) String() string

String returns the string name of the direction.

type F

type F struct {
	X, Y float64
}

F represents a floating point point, used for polygon drawing functions.

func FromPoint

func FromPoint(p image.Point) F

FromPoint makes an F from a point value

func (F) Add

func (a F) Add(b F) F

Add adds b to F.

func (F) Divide

func (a F) Divide(b F) F

Divide divides F by b.

func (F) Multiply

func (a F) Multiply(b F) F

Multiply multiplies F by b

func (F) Subtract

func (a F) Subtract(b F) F

Subtract subtracts b from F

type H

type H struct {
	Q, R int
}

H is a single hexagon in the grid.

func Add

func Add(a H, b D) H

Add is (a + b)

func Line

func Line(a, b H) []H

Line gets the hexagons on a line between two hex.

func RotateClockwise

func RotateClockwise(origin, moving H) H

RotateClockwise rotates one point around another point clockwise

func RotateCounterClockwise

func RotateCounterClockwise(origin, moving H) H

RotateCounterClockwise rotates one point around another point counter clockwise

func (H) Delta

func (h H) Delta() D

Delta converts the hex to a delta.

func (H) Float

func (h H) Float() (float64, float64, float64)

Float returns the cube coordinates as float values.

func (H) Neighbor

func (h H) Neighbor(d DirectionEnum) H

Neighbor one step in a specific direction.

type Layout

type Layout struct {
	Radius F // Radius is the radius of the hexagon; supports stretching on X or Y.
	Origin F // Origin is the where the center of H{0, 0} will be displayed.
	// contains filtered or unexported fields
}

Layout is the layout of the hex grid.

func MakeLayout

func MakeLayout(hexSize F, originCenter F, orientation Orientation) Layout

MakeLayout for rendering on the screen.

func (Layout) AreaFor

func (l Layout) AreaFor(center H, rad float64) map[H]bool

AreaFor returns all hex in the area of a screen circle.

func (Layout) CenterFor

func (l Layout) CenterFor(h H) F

CenterFor returns the point at the center (as a float) of the hex based on the layout.

func (Layout) HexFor

func (l Layout) HexFor(f F) H

HexFor for a hex.F that represents a point where things are laid out.

func (Layout) RingFor

func (l Layout) RingFor(center H, rad float64) map[H]bool

RingFor returns a set of hex within rad pixel distance of center.

func (Layout) Vertices

func (l Layout) Vertices(h H) []F

Vertices returns the location of all verticies for a given hexagon.

type Orientation

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

Orientation is the orientation of the hexagon map

var (
	OrientationPointy Orientation = Orientation{
						// contains filtered or unexported fields
	}
	OrientationFlat Orientation = Orientation{
					// contains filtered or unexported fields
	}
)

Define the default set of orientations.

Jump to

Keyboard shortcuts

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