grid

package
v0.0.0-...-9687f63 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Freq

func Freq[T comparable](g Dense[T]) map[T]int

Freq produces a summary of the number of different values in a grid.

Types

type Dense

type Dense[T any] [][]T

Dense is a dense grid - a "2D array" generic type.

func BytesFromStrings

func BytesFromStrings(s []string) Dense[byte]

BytesFromStrings produces a byte grid from a slice of strings.

func FromStringsFunc

func FromStringsFunc[T any](s []string, parse func(string) ([]T, error)) (Dense[T], error)

FromStringsFunc produces a grid from a slice of source strings s, containing data for one row per element, and a function for parsing each string into a `[]T` row. Uneven length rows are backfilled on the right with the zero value for T, to make all rows equal ength.

func Make

func Make[T any](h, w int) Dense[T]

Make makes a dense grid of width w and height h.

(If you want a sparse grid, use `make(Sparse[T])`.)

func Map

func Map[S, T any](g Dense[S], tf func(S) T) Dense[T]

Map converts a Dense[S] into a Dense[T] by using a transformation function tf.

func MapOrError

func MapOrError[S, T any](g Dense[S], tf func(S) (T, error)) (Dense[T], error)

MapOrError converts a Dense[S] into a Dense[T] by using a transformation function tf. On the first error returned by tf, MapOrError returns an error.

func RunesFromStrings

func RunesFromStrings(s []string) Dense[rune]

RunesFromStrings produces a byte grid from a slice of strings.

func (Dense[T]) Area

func (g Dense[T]) Area() int

Area returns the area of the grid (width * height).

func (Dense[T]) At

func (g Dense[T]) At(p image.Point) T

At returns g[p.Y][p.X].

func (Dense[T]) Bounds

func (g Dense[T]) Bounds() image.Rectangle

Bounds returns a rectangle the size of the grid.

func (Dense[T]) Clone

func (g Dense[T]) Clone() Dense[T]

Clone makes a copy of the grid.

func (Dense[T]) Fill

func (g Dense[T]) Fill(v T)

Fill fills the grid with the value v.

func (Dense[T]) FillRect

func (g Dense[T]) FillRect(r image.Rectangle, v T)

FillRect fills a sub-rectangle of the grid with the value v.

func (Dense[T]) FlipH

func (g Dense[T]) FlipH() Dense[T]

FlipH returns a new grid flipped horizontally (left becomes right).

func (Dense[T]) FlipV

func (g Dense[T]) FlipV() Dense[T]

FlipV returns a new grid flipped vertically (top becomes bottom).

func (Dense[T]) Map

func (g Dense[T]) Map(f func(T) T)

Map applies a transformation function to each element in the grid in-place.

func (Dense[T]) MapRect

func (g Dense[T]) MapRect(r image.Rectangle, f func(T) T)

MapRect applies a transformation function to a sub-rectangle of the grid.

func (Dense[T]) Resize

func (g Dense[T]) Resize(r image.Rectangle) Dense[T]

Resize returns a new grid that is r.Dx * r.Dy in size, containing values from g. Resize can be used for producing subgrids and supergrids.

func (Dense[T]) RotateACW

func (g Dense[T]) RotateACW() Dense[T]

RotateACW returns a new grid with entries rotated anticlockwise by 90 degrees.

func (Dense[T]) RotateCW

func (g Dense[T]) RotateCW() Dense[T]

RotateCW returns a new grid with entries rotated clockwise by 90 degrees.

func (Dense[T]) Size

func (g Dense[T]) Size() (h, w int)

Size returns the width and height of the Dense. If the height is zero the width will also be zero.

func (Dense[T]) String

func (g Dense[T]) String() string

func (Dense[T]) ToRGBA

func (g Dense[T]) ToRGBA(cf func(T) color.Color) *image.RGBA

ToRGBA converts a dense grid into an RGBA image using a colouring function.

func (Dense[T]) ToSparse

func (g Dense[T]) ToSparse() Sparse[T]

ToSparse converts a dense grid into a sparse grid.

func (Dense[T]) Transpose

func (g Dense[T]) Transpose() Dense[T]

Transpose returns a new grid reflected about the diagonal.

type Sparse

type Sparse[T any] map[image.Point]T

Sparse is a sparse grid - a map from points in 2D to values.

func MapToSparse

func MapToSparse[S, T any](g Dense[S], tf func(S) (T, bool)) Sparse[T]

MapToSparse converts a Dense[S] into a Sparse[T] by using a transformation function tf, which should also report if the element should be kept.

func (Sparse[T]) Bounds

func (g Sparse[T]) Bounds() image.Rectangle

Bounds returns a rectangle containing all points in the sparse grid. It operates in O(len(g)) time.

func (Sparse[T]) Clone

func (g Sparse[T]) Clone() Sparse[T]

Clone makes a copy of the grid.

func (Sparse[T]) Subgrid

func (g Sparse[T]) Subgrid(r image.Rectangle) Sparse[T]

Subgrid returns a copy of a portion of the grid.

func (Sparse[T]) ToDense

func (g Sparse[T]) ToDense() (Dense[T], image.Point)

ToDense converts a sparse grid into a dense grid and an offset (of the dense grid relative to the original sparse grid).

Jump to

Keyboard shortcuts

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