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 ¶
- func Direction(g Grid, da, db int) (int, bool)
- func Exit(g Grid, c cell.ID, i int) geom.Vec
- func Link(g Grid, from, to cell.ID) (cell.Links, bool)
- func Nearest(g Grid, v geom.Vec) int
- func Rim(g Grid, c cell.ID, i int) (a, b int)
- func Straight(g Grid, links cell.Links) (int, bool)
- func Way(g Grid, from, to cell.ID) (int, bool)
- type DefaultGrids
- type Grid
- type Shape
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Exit ¶
Exit is where direction i leaves c: the middle of the edge it crosses, or the corner it passes.
func Link ¶
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 Rim ¶
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).
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, addressed by axial coordinates.
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 in At's coordinates.
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 (col,row or axial q,r), 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 (col, row or axial q, r).
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.