grid

package
v0.0.0-...-9c88ebf Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2022 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TwoMask sets the one bit to zero, the result is always divisible by 2.
	TwoMask int = (^int(0)) ^ 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Aspect

type Aspect float64
const (
	Widescreen Aspect = 9.0 / 16.0
	Fullscreen Aspect = 3.0 / 4.0
	Square     Aspect = 1.0
)

Common aspect rations

func (Aspect) Pt

func (a Aspect) Pt(w int) Pt

Pt is created from an aspect ratio and a width.

type BaseIterator

type BaseIterator interface {
	IteratorFactory
	Next() (done bool)
	Pt() Pt
	Idx() int
	Done() bool
	Reset() (done bool)
	PtIdx(Pt) int
	Size() Pt
	Contains(Pt) bool
	Scale() Scale
}

BaseIterator contains the methods needed for BaseIteratorWrapper to create an Iterator

type BaseIteratorWrapper

type BaseIteratorWrapper struct {
	BaseIterator
}

BaseIteratorWrapper takes a BaseIterator and extends it's functionality.

func (BaseIteratorWrapper) Chan

func (base BaseIteratorWrapper) Chan() <-chan Pt

Chan runs a go routine that will return the points of the iterator. When all the points are consumed the channel is closed. Failing to consume all the points will cause a Go routine leak.

func (BaseIteratorWrapper) Each

func (base BaseIteratorWrapper) Each(fn func(int, Pt))

Each calls the fn for each point in the iterator.

func (BaseIteratorWrapper) Slice

func (base BaseIteratorWrapper) Slice() []Pt

Slice returns all the points in the iterator as a slice.

func (BaseIteratorWrapper) Start

func (base BaseIteratorWrapper) Start() (Iterator, bool)

Start is helper that can be as the first portion of a classic for loop

func (BaseIteratorWrapper) Until

func (base BaseIteratorWrapper) Until(fn func(int, Pt) bool) bool

Until calls fn against each point in the iterator until a point returns true. The bool indicates if a value returned true. The iterator will be left at the point that returned true.

type DenseFloatGrid

type DenseFloatGrid struct {
	Iterator
	Data []float64
}

DenseFloatGrid stores a fixed size grid of float64s.

func NewDenseFloatGrid

func NewDenseFloatGrid(i IteratorFactory, generator FloatGenerator) DenseFloatGrid

NewDenseFloatGrid with a size determined by the Iterator generated by the IteratorFactory. If generator is not nil, the values will be prepopulated.

func (DenseFloatGrid) Get

func (g DenseFloatGrid) Get(pt Pt) (interface{}, error)

Get a value, fulfills Grid. The underlying type will always be float64

func (DenseFloatGrid) GetFloat

func (g DenseFloatGrid) GetFloat(pt Pt) (float64, error)

GetFloat from DenseFloatGrid

func (DenseFloatGrid) Normalize

func (g DenseFloatGrid) Normalize()

Normalize sets the lowest value to 0 and the highest value to 1 and distributes all other value proportionally.

func (DenseFloatGrid) Set

func (g DenseFloatGrid) Set(pt Pt, val interface{}) error

Set a value, fulfills Grid. Any numeric value can be passed in and will be cast to float64.

func (DenseFloatGrid) SetFloat

func (g DenseFloatGrid) SetFloat(pt Pt, val float64) error

SetFloat in DenseFloatGrid

type DenseGrid

type DenseGrid struct {
	Iterator
	Data []interface{}
}

DenseGrid is backed by a slice and should have an entry for every node in the grid.

func NewDenseGrid

func NewDenseGrid(i IteratorFactory, generator Generator) *DenseGrid

NewDenseGrid creates a DenseGrid with Data allocated at the correct size. If a Generator is given, the data will be populated.

func (*DenseGrid) Get

func (g *DenseGrid) Get(pt Pt) (interface{}, error)

Get a value, fulfills Grid

func (*DenseGrid) Set

func (g *DenseGrid) Set(pt Pt, val interface{}) error

Set a value, fulfills Grid

type FloatGenerator

type FloatGenerator func(pt Pt) float64

FloatGenerator returns a float64 given a pt

type Generator

type Generator func(pt Pt) interface{}

Generator is used to fill grids from their point values.

type Grid

type Grid interface {
	Get(Pt) (interface{}, error)
	Set(Pt, interface{}) error
	Iterator
}

Grid is used to store data in a 2 dimensional grid.

type Iterator

type Iterator interface {
	BaseIterator
	Start() (iter Iterator, done bool)
	Slice() []Pt
	Chan() <-chan Pt
	Each(fn func(int, Pt))
	Until(fn func(int, Pt) bool) bool
}

Iterator is used to iterate over the cells in a grid.

type IteratorFactory

type IteratorFactory interface {
	Iter() Iterator
}

IteratorFactory is anything that can produce an iterator. An InteratorFactory should return a new iterator, not one that may already be in use.

type Pt

type Pt struct {
	X, Y int
}

Pt is a cell in a grid

func (Pt) Abs

func (pt Pt) Abs() Pt

Abs returns a Pt where both X and Y are positive

func (Pt) Add

func (pt Pt) Add(pt2 Pt) Pt

Add two Pts

func (Pt) Area

func (pt Pt) Area() int

Area always returns a positive value.

func (Pt) AssertEqual

func (pt Pt) AssertEqual(actual interface{}, t cmpr.Tolerance) error

AssertEqual fulfils geomtest.AssertEqualizer

func (Pt) D2

func (pt Pt) D2() d2.D2

D2 converts a Pt to a d2.D2.

func (Pt) Index

func (pt Pt) Index(idx int) Pt

Index returns the point reached at idx when incrementing by X and wrapping Y.

func (Pt) Iter

func (pt Pt) Iter() Iterator

Iter creates an Iterator from the origin to this Pt

func (Pt) Mask

func (pt Pt) Mask(and int) Pt

Mask performs and AND operation with the mask on the X and Y value of the given point.

func (Pt) Multiply

func (pt Pt) Multiply(scale float64) Pt

Multiply a Pt by a scale value

func (Pt) Subtract

func (pt Pt) Subtract(pt2 Pt) Pt

Subtract two points

func (Pt) To

func (pt Pt) To(pt2 Pt) Iterator

To creates an Iterator between two points

type Range

type Range [2]Pt

Range represents a grid with the first point being inclusive and the second point exclusive.

func (Range) Contains

func (r Range) Contains(pt Pt) bool

Contains checks if the point is in the Range with the first point being inclusive and the second point being exclusive.

func (Range) Iter

func (r Range) Iter() Iterator

Iter fulfills IteratorFactory and returns a Scanner using the Range.

func (Range) Max

func (r Range) Max() Pt

Max returns the point with the largest X and Y value that is contained in the range.

func (Range) Min

func (r Range) Min() Pt

Min returns the point with the lowest X and Y value that is contained in the range.

func (Range) Scale

func (r Range) Scale() Scale

Scale the range so that r[0] returns t0=0, t1=0 and r[1] returns t0=1, t1=1.

func (Range) Size

func (r Range) Size() Pt

Size returns a Pt that indicates the width and height of the Range.

func (Range) Start

func (r Range) Start() (i Iterator, done bool)

Start creates and starts an iterator over the range.

type Scale

type Scale struct {
	X, Y, DX, DY float64
}

Scale is used to convert a Grid Pt to two float64 values, often

func (Scale) T

func (s Scale) T(pt Pt) (float64, float64)

T returns the scaled values corresponding to the point. Typically these are used as parametric values.

type ScanOption

type ScanOption byte

ScanOption allows the scanner operation to be configured.

const (
	// ScanVertical will scan columns then rows
	ScanVertical ScanOption = 1
	// ScanPrimaryReversed causes the scan in the primary direction to proceed
	// backward. The primary direction is X unless ScanVertical is used.
	ScanPrimaryReversed ScanOption = 2
	// ScanSecondaryReversed causes the scan in the secondary direction to
	// proceed backward. The secondary direction is Y unless ScanVertical is
	// used.
	ScanSecondaryReversed ScanOption = 4
)

type Scanner

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

Scanner scans a rectangular region.

func NewScanner

func NewScanner(r Range, opts ...ScanOption) *Scanner

NewScanner creates a scanner based on a Range. The r[0] value is incluse and the r[1] value is exclusive.

func (*Scanner) Contains

func (s *Scanner) Contains(pt Pt) bool

Contains checks if the Pt is inside the scanner region.

func (*Scanner) Done

func (s *Scanner) Done() bool

Done returns true when there is no more to scan

func (*Scanner) Idx

func (s *Scanner) Idx() int

Idx is the Index value the scanner is currently at.

func (*Scanner) Iter

func (s *Scanner) Iter() Iterator

Iter makes a copy of the scanner and returns it as an Iterator.

func (*Scanner) Next

func (s *Scanner) Next() bool

Next moves the scanner to the next position

func (*Scanner) Pt

func (s *Scanner) Pt() Pt

Pt the scanner is currently at

func (*Scanner) PtIdx

func (s *Scanner) PtIdx(pt Pt) int

PtIdx takes a Pt and returns the Idx value when the scanner would reach that point. If the point is not inside the scan region, the response will not be meaningful.

func (*Scanner) Reset

func (s *Scanner) Reset() (done bool)

Reset moves the scanner back to the starting position.

func (*Scanner) Scale

func (s *Scanner) Scale() Scale

Scale returns the Scale that corresponds with the Range that defined the scanner.

func (*Scanner) Size

func (s *Scanner) Size() Pt

Size returns a Pt that indicates the length and width of the scan area.

type SparseGrid

type SparseGrid struct {
	Iterator
	Data map[Pt]interface{}
	Generator
}

SparseGrid is backed by a map so it can hold an arbitrary amount of data. If a generator is defined, it will be invoked lazily.

func NewSparseGrid

func NewSparseGrid(i IteratorFactory, generator Generator) *SparseGrid

NewSparseGrid creates a SparseGrid. If an IteratorFactory is defined, that will be used as an iterator. This is for convience only as a sparse grid is not actually limited to any range. If a generator is defined, it will be invoked lazily when accessing points that have not been set.

func (*SparseGrid) Get

func (g *SparseGrid) Get(pt Pt) (interface{}, error)

Get the value at the defined Pt in the grid. If the point value has not been set and a generator has been set the value will be generated and stored.

func (*SparseGrid) Set

func (g *SparseGrid) Set(pt Pt, val interface{}) error

Set the value at the point. Setting a value to nil will remove it from the underlying map. This means that if there is a generator, the next Get will update that point with the value from the generator.

Jump to

Keyboard shortcuts

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