grid

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 4 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package grid is a board's topology: a Grid answers which cells neighbour which and in which direction, where a cell lies, how far apart two are, which cells a box covers and every cell in turn; DefaultGrids makes a square or a hex one. A grid knows its own directions, and nothing outside it spells them out: Way and Link name the direction to a neighbour, Direction and Nearest the one of an offset or a vector, Exit and Rim where it leaves a cell, Straight the one links run straight through by. ShapeOf tells a default grid's Shape.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Direction

func Direction(g Grid, da, db int) (int, bool)

Direction is the direction whose Offset is da, db; false when none is.

func Exit

func Exit(g Grid, c cell.ID, i int) geom.Vec

Exit is where direction i leaves c: the middle of the edge it crosses, or the corner it passes.

func Link(g Grid, from, to cell.ID) (cell.Links, bool)

Link is the bit of cell.Links a Way runs on from from to its neighbour to by; false when to is not its neighbour.

func Nearest

func Nearest(g Grid, v geom.Vec) int

Nearest is the direction whose Step runs nearest the way v points.

func Rim

func Rim(g Grid, c cell.ID, i int) (a, b int)

Rim is which of c's CellOutline corners direction i leaves by: the two ends of the edge it crosses, or the one corner it passes (a == b).

func Straight

func Straight(g Grid, links cell.Links) (int, bool)

Straight is the direction links run straight through a cell by, out of it and back: false for a bend, a fork or an end.

func Way

func Way(g Grid, from, to cell.ID) (int, bool)

Way is the direction from from to its neighbour to; false when to is not its neighbour.

Types

type DefaultGrids

type DefaultGrids struct{}

DefaultGrids makes the grids gram ships; a board wraps them along the axes its world's edges wrap.

func (DefaultGrids) Hex

func (DefaultGrids) Hex(width, height uint32, size float32) Grid

Hex is a grid of width x height pointy-top hexagonal cells of the given size, every other row half a cell east of the one over it.

func (DefaultGrids) Square

func (DefaultGrids) Square(width, height, cellSize uint32) Grid

Square is a grid of width x height square cells cellSize wide, eight neighbours each.

type Grid

type Grid interface {
	Neighbors(c cell.ID) []cell.ID
	// Toward is c's neighbour in the grid's i-th direction, across a seam where the grid wraps;
	// false off the grid or past its directions.
	Toward(c cell.ID, i int) (cell.ID, bool)
	// Directions is how many directions a cell has neighbours in, Toward's i below it.
	Directions() int
	// Opposite is the direction back from the neighbour in direction i.
	Opposite(i int) int
	// Offset is direction i as a pair the same wherever it starts: a square grid's step in
	// At's coordinates, a hex one's along its axes (grid.Direction finds i by it).
	Offset(i int) (da, db int)
	// Step is the way from a cell's middle to its neighbour's in direction i, in world units.
	Step(i int) geom.Vec
	// Corner reports whether direction i leaves the cell through a corner, not across an edge.
	Corner(i int) bool
	// Flanks are the edge directions either side of corner direction i; false for an edge one.
	Flanks(i int) (a, b int, ok bool)
	CellCenter(c cell.ID) geom.Vec
	CellAt(pos geom.Vec) (cell.ID, bool)
	// At is the cell at grid coordinates (a column and a row), wrapped where the grid
	// wraps; one off a closed axis panics by its coordinates.
	At(a, b uint32) cell.ID
	// NeighborCost is the geometric step cost from a to its neighbor b.
	NeighborCost(a, b cell.ID) float64
	// DiagonalNeighbors returns the two cells flanking the corner between a and its diagonal b.
	DiagonalNeighbors(a, b cell.ID) (c1, c2 cell.ID, ok bool)
	// Distance must never overestimate the true cost — it's the pathfinding heuristic.
	Distance(a, b cell.ID) float64
	// CellSpan is the world-space side length of one cell.
	CellSpan() float32
	// CellBounds is the width and height of the rectangle round one cell.
	CellBounds() (w, h float32)
	// CellOutline appends to dst the corners of c, in order round the cell.
	CellOutline(c cell.ID, dst []geom.Vec) []geom.Vec
	// CellBoxes appends to dst boxes that together cover c, exactly or from outside.
	CellBoxes(c cell.ID, dst []geom.AABB) []geom.AABB
	// CellsUnder calls fn for every cell the box touches, each once.
	CellsUnder(box geom.AABB, fn func(c cell.ID))
	// EachCell calls fn for every cell of the grid.
	EachCell(fn func(c cell.ID))
	// Ordinal is c's index in a table with one slot per cell, below CellCount; false for no cell.
	Ordinal(c cell.ID) (int, bool)
	// Coords inverts At: c's grid coordinates (a column and a row).
	Coords(c cell.ID) (a, b uint32, ok bool)
	// CellCount is how many cells the grid has.
	CellCount() int
}

Grid abstracts a board's topology (square, hex, ...) behind neighbor, coordinate, and distance queries.

type Shape

type Shape struct {
	Hex          bool
	Cols, Rows   uint32
	Cell         float32
	WrapX, WrapY bool
}

Shape is the shape of a grid of DefaultGrids: square or hex, how many columns and rows of cells, how large — a square's side, a hex's size — and whether each axis wraps.

func ShapeOf

func ShapeOf(g Grid) (Shape, bool)

ShapeOf is g's shape when it is one of DefaultGrids; false for any other.

Jump to

Keyboard shortcuts

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