vec

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2025 License: MIT Imports: 5 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CardinalDirections []Direction = []Direction{DIR_UP, DIR_RIGHT, DIR_DOWN, DIR_LEFT}

Functions

func ArcGenerator

func ArcGenerator(radius int) func() Coord

returns a generator that computes successive coordinates representing 1/8th of a circle. rotate the arc to draw circles. gives back the ZERO_COORD when it is done.

func Circle

func Circle(center Coord, radius int, fn func(pos Coord))

Computes a circle, calling fn on each point of the circle. fn can be a drawing function or whatever.

func EachCoordInArea

func EachCoordInArea(b Bounded) iter.Seq[Coord]

Returns an iterator producing a sequence of all Coords within the Rect r, starting in the top-left corner and proceeding to the right, going line by line (like how you'd read)

func EachCoordInIntersection added in v0.2.0

func EachCoordInIntersection(areas ...Bounded) iter.Seq[Coord]

Returns an iterator producing a sequence of all Coords that are contained within the intersection of all provided bounded areas.

func EachCoordInPerimeter

func EachCoordInPerimeter(b Bounded) iter.Seq[Coord]

Returns an iterator producing a sequence of all coords in the perimeter of a bounded area.

func Intersects

func Intersects(b1, b2 Bounded) bool

Intersects returns true if the two provided Bounded areas intersect

Types

type Bounded

type Bounded interface {
	Bounds() Rect
}

Bounded defines objects that can report a bounding box of some kind.

type Coord

type Coord Vec2i

Coord is an (X, Y) pair that represents a spot on some 2d grid.

var (
	ZERO_COORD Coord = Coord{0, 0}
)

func IndexToCoord

func IndexToCoord(index, stride int) Coord

IndexToCoord returns a coord representing an index from a 1D array representing a 2D grid with the given stride

func RandomCoordInArea added in v0.2.0

func RandomCoordInArea(area Bounded) (c Coord)

func (Coord) Add

func (c1 Coord) Add(c2 Coord) Coord

func (Coord) DistanceSqTo added in v0.2.0

func (c1 Coord) DistanceSqTo(c2 Coord) int

DistanceSqTo returns the euclidean distance between c1 and c2, squared. This is useful for comparing distances in cases where the actual distance is not important, because this is much faster than calculating the real distance.

func (Coord) DistanceTo added in v0.2.0

func (c1 Coord) DistanceTo(c2 Coord) float64

DistanceTo returns the euclidean distance bewteen c1 and c2. For comparisons, consider using DistanceSqTo instead, it will be much faster.

func (Coord) IsInPerimeter added in v0.2.0

func (c Coord) IsInPerimeter(b Bounded) bool

IsInPerimeter check if the coord lies in the perimeter of the bounded object b.

func (Coord) IsInside

func (c Coord) IsInside(b Bounded) bool

IsInside checks if the coord is within the bounds of object b.

func (Coord) ManhattanDistanceTo added in v0.2.0

func (c1 Coord) ManhattanDistanceTo(c2 Coord) int

ManhattanDistance calculates the manhattan (or taxicab) distance on a square grid.

func (*Coord) Move

func (c *Coord) Move(dx, dy int)

func (*Coord) MoveTo

func (c *Coord) MoveTo(x, y int)

func (Coord) Scale

func (c Coord) Scale(scale int) Coord

Returns the coordinate with both X and Y multiplied by scale

func (Coord) Step

func (c Coord) Step(d Direction) Coord

Returns the coordinate stepped once in the direction d

func (Coord) StepN

func (c Coord) StepN(d Direction, n int) Coord

Returns the coordinate stepped N times in the direction d

func (Coord) String

func (c Coord) String() string

func (Coord) Subtract

func (c1 Coord) Subtract(c2 Coord) Coord

func (Coord) ToIndex

func (c Coord) ToIndex(stride int) int

ToIndex converts a Coord to a 1D index in a 2D array with the given stride.

type Dims

type Dims struct {
	W, H int
}

Dims represents a set of dimensions in 2D.

func (Dims) Area

func (d Dims) Area() int

func (Dims) Bounds

func (d Dims) Bounds() Rect

Returns a rect with dimensions d, positioned at (0, 0)

func (Dims) Grow

func (d Dims) Grow(dw, dh int) Dims

func (Dims) Shrink

func (d Dims) Shrink(dw, dh int) Dims

func (Dims) String

func (d Dims) String() string

type Direction

type Direction int
const (
	DIR_UP Direction = iota
	DIR_UPRIGHT
	DIR_RIGHT
	DIR_DOWNRIGHT
	DIR_DOWN
	DIR_DOWNLEFT
	DIR_LEFT
	DIR_UPLEFT
	DIR_NONE
)

func RandomCardinalDirection added in v0.2.0

func RandomCardinalDirection() Direction

func RandomDirection added in v0.2.0

func RandomDirection() Direction

func (Direction) Coord added in v0.2.0

func (d Direction) Coord() Coord

func (Direction) Inverted

func (d Direction) Inverted() Direction

func (Direction) RotateCCW

func (d Direction) RotateCCW() Direction

func (Direction) RotateCCW90 added in v0.2.0

func (d Direction) RotateCCW90() Direction

func (Direction) RotateCW

func (d Direction) RotateCW() Direction

func (Direction) RotateCW90 added in v0.2.0

func (d Direction) RotateCW90() Direction

type Line

type Line struct {
	Start Coord
	End   Coord
}

Line represents a line between 2 points on a square grid.

func (Line) EachCoord

func (l Line) EachCoord() iter.Seq[Coord]

EachCoord returns an iterator that produces Coords representing the line from start to end inclusive.

func (Line) Length

func (l Line) Length() float64

Length computes the precise cartesian length of the line. Consider using LengthSq() if you don't actually need the absolute value and are just doing comparisons.

func (Line) LengthSq

func (l Line) LengthSq() int

LengthSq returns the length of the line squared. Good for comparison purposes in loops where a nasty sqrt() makes you go :(

type Rect

type Rect struct {
	Coord //position (X, Y)
	Dims  //size (W, H)
}

Rect is your standard rectangle object, with position (X,Y) in the top left corner.

func FindIntersectionRect

func FindIntersectionRect(b1, b2 Bounded) (r Rect)

FindIntersectionRect calculates the intersection of two rectangularly-bound objects. if no intersection returns Rect{0,0,0,0}

func (Rect) Bounds

func (r Rect) Bounds() Rect

Goofy... but needed to satisfy Bounded interface.

func (Rect) Center added in v0.2.0

func (r Rect) Center() Coord

Returns the center of the rect. Since we're all integers 'round these parts this won't be exact unless both width and height are odd numbers, so be aware.

func (Rect) Corners

func (r Rect) Corners() (corners [4]Coord)

Returns the coordinates of the 4 corners of the rect, starting in the top left and going clockwise.

func (Rect) String

func (r Rect) String() string

func (Rect) Translated

func (r Rect) Translated(coord Coord) Rect

type Vec2Polar

type Vec2Polar struct {
	R, Phi float64
}

func (Vec2Polar) Add

func (v1 Vec2Polar) Add(v2 Vec2Polar) Vec2Polar

Add converts to recitlinear components and adds, then converts back to polar.

func (Vec2Polar) AngularDistance

func (v1 Vec2Polar) AngularDistance(v2 Vec2Polar) float64

Returns the shortest anglular distance from v1 to v2. positive for counterclockwise, negative for clockwise. NOTE: Do these need to be Pos()'d?? Hmm.

func (Vec2Polar) Get

func (v Vec2Polar) Get() (float64, float64)

func (*Vec2Polar) Pos

func (v *Vec2Polar) Pos()

Reorients vector to ensure R is positive and 0 <= Phi < 2*pi

func (*Vec2Polar) Set

func (v *Vec2Polar) Set(r, phi float64)

func (Vec2Polar) ToRect

func (v Vec2Polar) ToRect() Vec2f

ToRect converts the Polar vector into a rectilinear form

type Vec2f

type Vec2f struct {
	X, Y float64
}

Vec2f is a 2 dimensional vector of floats

func (Vec2f) Add

func (v1 Vec2f) Add(v2 Vec2f) Vec2f

func (Vec2f) Mag

func (v Vec2f) Mag() float64

func (*Vec2f) Mod

func (v *Vec2f) Mod(dx, dy float64)

func (Vec2f) NonZero added in v0.2.0

func (v Vec2f) NonZero() bool

func (*Vec2f) Set

func (v *Vec2f) Set(x, y float64)

func (Vec2f) Sub

func (v1 Vec2f) Sub(v2 Vec2f) Vec2f

returns vec2f = v1 - v2

func (Vec2f) ToPolar

func (v Vec2f) ToPolar() Vec2Polar

func (Vec2f) ToVec2i

func (v Vec2f) ToVec2i() Vec2i

type Vec2i

type Vec2i struct {
	X, Y int
}

func (Vec2i) Add

func (v1 Vec2i) Add(v2 Vec2i) Vec2i

func (Vec2i) Mag

func (v Vec2i) Mag() float64

Returns the magnitude of the vector. Note that this is a float and not an int.

func (Vec2i) NonZero added in v0.2.0

func (v Vec2i) NonZero() bool

func (Vec2i) Scale

func (v Vec2i) Scale(scale int) Vec2i

func (Vec2i) Sub

func (v1 Vec2i) Sub(v2 Vec2i) Vec2i

func (Vec2i) ToVec2f

func (v Vec2i) ToVec2f() Vec2f

Jump to

Keyboard shortcuts

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