Hexago

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2022 License: GPL-3.0 Imports: 6 Imported by: 0

README

Hexago

A hexagonal grid package using Go Graphics (gg)

Installation

go get -u https://github.com/SHA65536/Hexago

Usage

This is an example showing simple usage of the library:

package main

import (
	"fmt"

	"github.com/SHA65536/Hexago"
)

func main() {
	// Create a 4*5 cell grid that is 800x600 pixels
	grid := Hexago.MakeHexGrid(800, 600, 4, 5)
	// Filling all cells with blue
	grid.SetFillAll(0, 0, 1, 1)
	// Giving all cells a 10 wide black border
	grid.SetStrokeAll(0, 0, 0, 1, 10)

	// Applying the  function for each cell
	grid.DrawFunc(func(i, j, _, _ int, _ *Hexago.Hexagon) error {
		// Writing the position on the cell
		pos := fmt.Sprintf("(%v,%v)", i, j)
		return grid.SetText(i, j, 0, 0, 0, 1, pos, 50)
	})

	// Getting all the neighbors of cell 3, 2
	neighbors, _ := grid.GetNeighbors(3, 2)
	for _, h := range neighbors {
		// Filling them with red
		grid.SetFill(h.X, h.Y, 1, 0, 0, 1)
	}

	// Getting all the neighbors of cell 1, 3
	neighbors, _ = grid.GetNeighbors(1, 3)
	for _, h := range neighbors {
		// Filling them with green
		grid.SetFill(h.X, h.Y, 0, 1, 0, 1)
	}

	// Saving grid to disk
	grid.SavePNG("example.png")
}

Example

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HexGrid

type HexGrid struct {
	Context       *gg.Context
	Tiles         [][]*Hexagon
	Width, Height int
	Rows, Cols    int
	Radius        float64
	// contains filtered or unexported fields
}

func MakeHexGrid

func MakeHexGrid(w, h, r, c float64) *HexGrid

MakeHexGrid creates a <w> wide and <h> high grid with <r> rows and <c> columns

func MakeHexGridWithContext

func MakeHexGridWithContext(ctx *gg.Context, r, c float64) *HexGrid

MakeHexGridWithContext creates a grid with external context Uses context width and height

func (*HexGrid) DrawFunc

func (grid *HexGrid) DrawFunc(function func(x, y, h, w int, cell *Hexagon) error) error

DrawFunc recieves a function and applies that function to each cell of the grid the function recieves coordinates of each cell (x,y), the rows and cols (h,w) and a pointer to the cell itself

func (*HexGrid) DrawGrid

func (grid *HexGrid) DrawGrid()

func (*HexGrid) GetNeighbors

func (grid *HexGrid) GetNeighbors(x, y int) ([]*Hexagon, error)

GetNeighbors returns the direct neighbors of a given cell

func (*HexGrid) SavePNG

func (grid *HexGrid) SavePNG(path string) error

SavePNG saves the grid as a PNG

func (*HexGrid) SetFill

func (grid *HexGrid) SetFill(x, y int, r, g, b, a float64) error

SetFill sets the fill of a cell

func (*HexGrid) SetFillAll

func (grid *HexGrid) SetFillAll(r, g, b, a float64)

SetFillAll sets all of the tiles' fill colour

func (*HexGrid) SetStroke

func (grid *HexGrid) SetStroke(x, y int, r, g, b, a, w float64) error

SetStroke sets the stroke of a cell

func (*HexGrid) SetStrokeAll

func (grid *HexGrid) SetStrokeAll(r, g, b, a, w float64)

SetStrokeAll sets all of the tiles' stroke colour and width

func (*HexGrid) SetText

func (grid *HexGrid) SetText(x, y int, r, g, b, a float64, txt string, fontSize float64) error

SetText sets the colour, size, and content of a cell's text

type Hexagon

type Hexagon struct {
	// Fill Colours
	FR, FG, FB, FA float64
	//Stroke Colours and width
	SR, SG, SB, SA, SW float64
	//Font Colours font and text
	TR, TG, TB, TA float64
	Font           font.Face
	Text           string
	//Coords in the grid
	X, Y int
}

func (*Hexagon) SetFill

func (h *Hexagon) SetFill(r, g, b, a float64)

func (*Hexagon) SetStroke

func (h *Hexagon) SetStroke(r, g, b, a, w float64)

func (*Hexagon) SetText

func (h *Hexagon) SetText(r, g, b, a float64, text string, font font.Face)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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