geo

package module
v1.0.0-beta2 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2017 License: MIT Imports: 6 Imported by: 0

README

geo

GoDoc Build Status Go Report Card

This package contains a collection of 2-D types and other mathematical functions geared towards games in Go. See the geoexamples package for a few examples.

Features

  • Types for 2-D vector, rectangle, circle, and ray
  • A collection of easing functions
  • Functions for generating random numbers and vectors
  • Perlin noise and shaking functions
  • Several miscellaneous functions like Clamp, Map, and Mod

Documentation

Overview

Package geo contains a collection of 2-D types and other mathematical functions geared towards games.

Includes

  • Types for 2-D vector, rectangle, circle, and ray
  • A collection of easing functions
  • Functions for generating random numbers and vectors, including a function for randomly selecting an item from a list with weighted probabilities
  • Perlin noise functions
  • Other miscellaneous functions like Clamp, Map, and Mod

This package assumes coordinates where +x is right and +y is down.

Index

Constants

This section is empty.

Variables

View Source
var (
	EaseLinear = EaseIn(1)

	EaseInQuad    = EaseIn(2)
	EaseOutQuad   = EaseOut(2)
	EaseInOutQuad = EaseInOut(2)

	EaseInCubic    = EaseIn(3)
	EaseOutCubic   = EaseOut(3)
	EaseInOutCubic = EaseInOut(3)

	EaseInQuart    = EaseIn(4)
	EaseOutQuart   = EaseOut(4)
	EaseInOutQuart = EaseInOut(4)

	EaseInQuint    = EaseIn(5)
	EaseOutQuint   = EaseOut(5)
	EaseInOutQuint = EaseInOut(5)
)
View Source
var Vec0 = Vec{}

Vec0 is the zero vector.

Functions

func Clamp

func Clamp(n, a, b float64) float64

Clamp returns n if it is within [min(a, b), max(a, b)] otherwise it returns the closer of a and b.

func Ease

func Ease(a, b, t float64, easefn EaseFn) float64

Ease interpolates from a to b by percent t following the path defined by easefn.

func EaseInBack

func EaseInBack(t float64) float64

func EaseInBounce

func EaseInBounce(t float64) float64

func EaseInCirc

func EaseInCirc(t float64) float64

func EaseInElastic

func EaseInElastic(t float64) float64

func EaseInExpo

func EaseInExpo(t float64) float64

func EaseInOutBack

func EaseInOutBack(t float64) float64

func EaseInOutBounce

func EaseInOutBounce(t float64) float64

func EaseInOutCirc

func EaseInOutCirc(t float64) float64

func EaseInOutElastic

func EaseInOutElastic(t float64) float64

func EaseInOutExpo

func EaseInOutExpo(t float64) float64

func EaseInOutSine

func EaseInOutSine(t float64) float64

func EaseInSine

func EaseInSine(t float64) float64

func EaseOutBack

func EaseOutBack(t float64) float64

func EaseOutBounce

func EaseOutBounce(t float64) float64

func EaseOutCirc

func EaseOutCirc(t float64) float64

func EaseOutElastic

func EaseOutElastic(t float64) float64

func EaseOutExpo

func EaseOutExpo(t float64) float64

func EaseOutSine

func EaseOutSine(t float64) float64

func I2F2

func I2F2(i1, i2 int) (f1, f2 float64)

I2F2 takes 2 ints and converts them to float64. e.g.

rect.SetTopLeft(I2F2(functionThatReturns2Ints()))

func Lerp

func Lerp(a, b, t float64) float64

Lerp linear interpolates from a to b by percent t, where t=0 returns a and t=1 returns b.

func Map

func Map(n, a1, b1, a2, b2 float64) float64

Map takes a number n in the range [a1, b1] and remaps it be in the range [a2, b2], with its relative position within the range preserved. E.g if n is half way between a1 and b1 then the value returned will be half way between a2 and b2. If a1 == b1 then +Inf is returned.

func Mod

func Mod(a, b float64) float64

Mod is the modulus operator. Unlike the Mod in the math package this wraps negative numbers around to the positive axis. If b is 0 then this function returns 0. e.g. if b is 3

a       -5 -4 -3 -2 -1 0 1 2 3 4 5
return   1  2  0  1  2 0 1 2 0 1 2

func Perlin

func Perlin(x, y, z float64) float64

Perlin implements Perlin noise. It returns values bewtween 0 and 1.

func PerlinOctave

func PerlinOctave(x, y, z float64, octaves int, persistence float64) float64

PerlinOctave combines a number of Perlin functions, equal to octaves, of decreasing contribution. A persistence of 1 means each octave has equal contribution. A persistence of 0.5 means each octave contributes half as much as the previous one.

func RandIndex

func RandIndex(weights []float64) int

RandIndex takes a list of weights and returns an index with a probability corresponding to the relative weight of each index. Behavior is undefined if weights is the empty list. A weight of 0 will never be selected unless all are 0, in which case all indices have equal probability. Negative weights are treated as 0.

Types

type Circle

type Circle struct {
	X, Y, R float64
}

Circle is a 2-D circle with position (X, Y) and radius R.

func CircleCircumscribe

func CircleCircumscribe(r Rect) Circle

CircleCircumscribe returns the smallest circle that surrounds the Rect.

func CircleInscribe

func CircleInscribe(r Rect) Circle

CircleInscribe returns the largest Circle that fits insided the Rect, and shares its center with the Rect.

func CircleR

func CircleR(r float64) Circle

CircleR creates a Circle centered at the origin with radius r.

func CircleVec

func CircleVec(pos Vec) Circle

CircleVec creates a unit Circle at position pos.

func CircleVecR

func CircleVecR(pos Vec, r float64) Circle

CircleVecR creates a Circle at position pos with radius r.

func CircleXY

func CircleXY(x, y float64) Circle

CircleXY creates a unit Circle at position (x, y).

func CircleXYR

func CircleXYR(x, y, r float64) Circle

CircleXYR creates a Circle at position (x, y) with radius r.

func (Circle) Area

func (c Circle) Area() float64

Area returns the area of the Circle.

func (Circle) Bottom

func (c Circle) Bottom() float64

Bottom returns the y position that is tangent to the bottom of the Circle.

func (Circle) BottomLeft

func (c Circle) BottomLeft() (x, y float64)

BottomLeft returns the position of the bottom left corner of the Circle's bounding box.

func (Circle) BottomMid

func (c Circle) BottomMid() (x, y float64)

BottomMid returns the position of the bottom of the Circle, bellow its center.

func (Circle) BottomRight

func (c Circle) BottomRight() (x, y float64)

BottomRight returns the position of the bottom right corner of the Circle's bounding box.

func (Circle) BoundingRect

func (c Circle) BoundingRect() Rect

BoundingRect returns the smallest Rect that surrounds the Circle.

func (Circle) Circumference

func (c Circle) Circumference() float64

Circumference returns the circumference of the Circle.

func (Circle) CollideCircle

func (c Circle) CollideCircle(other Circle) bool

CollideCircle returns true if other overlaps with this one.

func (Circle) CollideCircleList

func (c Circle) CollideCircleList(others []Circle) (i int, ok bool)

CollideCircleList returns the index of the first Circle this one collides with. If there is no collision then ok is false and i is undefined.

func (Circle) CollideCircleListAll

func (c Circle) CollideCircleListAll(others []Circle) []int

CollideCircleListAll returns a list of indices of the Circles that collide with this one, or an empty list if none.

func (Circle) CollidePoint

func (c Circle) CollidePoint(x, y float64) bool

CollidePoint returns true if the given point is inside the Circle.

func (Circle) CollideRect

func (c Circle) CollideRect(r Rect) bool

CollideRect returns true if the Circle is colliding with the Rect.

func (Circle) CollideRectList

func (c Circle) CollideRectList(rs []Rect) (i int, ok bool)

CollideRectList returns the index of the first Rect the Circle collides with. If there is no collision then ok is false and i is undefined.

func (Circle) CollideRectListAll

func (c Circle) CollideRectListAll(rs []Rect) []int

CollideRectListAll returns a list of indices of the Rects that collide with the Circle, or an empty list if none.

func (Circle) Contains

func (c Circle) Contains(other Circle) bool

Contains returns true if the other Circle is completely inside this one.

func (Circle) Diameter

func (c Circle) Diameter() float64

Diameter returns the diameter of the Circle.

func (Circle) Equals

func (c Circle) Equals(other Circle, e float64) bool

Equals returns true if the two circles position and radius are with in error e.

func (*Circle) Inflate

func (c *Circle) Inflate(dr float64)

Inflate keeps the same center but changes the size by the given amount.

func (Circle) Inflated

func (c Circle) Inflated(dr float64) Circle

Inflated returns a new Circle with the same center but changes the size by the given amount.

func (Circle) InscribedRect

func (c Circle) InscribedRect() Rect

InscribedRect returns the largest square that fits inside the Circle.

func (Circle) Left

func (c Circle) Left() float64

Left returns the x position that is tangent to the left side of the Circle.

func (Circle) LeftMid

func (c Circle) LeftMid() (x, y float64)

LeftMid returns the position of the left of the Circle, inline with its center.

func (Circle) Mid

func (c Circle) Mid() (x, y float64)

Mid returns the Circle's center x and y position.

func (*Circle) Move

func (c *Circle) Move(dx, dy float64)

Move moves the center of the Circle by the given amount.

func (Circle) Moved

func (c Circle) Moved(dx, dy float64) Circle

Moved returns a new Circle with the same size but with the center moved by the given amount.

func (*Circle) Normalize

func (c *Circle) Normalize()

Normalize modifies the radius to be positive if needed.

func (Circle) Normalized

func (c Circle) Normalized() Circle

Normalized returns a new Circle with a positive radius.

func (Circle) PointAt

func (c Circle) PointAt(radians float64) (x, y float64)

PointAt returns the position of the point along the Circle's edge that is at the given angle (from +x axis).

func (Circle) Pos

func (c Circle) Pos() Vec

Pos returns the Circle's position as a Vec.

func (Circle) PosR

func (c Circle) PosR() (pos Vec, r float64)

PosR returns the Circle's position as a Vec and radius.

func (Circle) Right

func (c Circle) Right() float64

Right returns the x position that is tangent to the right side of the Circle.

func (Circle) RightMid

func (c Circle) RightMid() (x, y float64)

RightMid returns the position of the right of the Circle, inline with its center.

func (*Circle) SetBottom

func (c *Circle) SetBottom(y float64)

SetBottom moves the Circle so that its bottom side is tangent to y.

func (*Circle) SetBottomLeft

func (c *Circle) SetBottomLeft(x, y float64)

SetBottomLeft moves the Circle so that the bottom left corner of its bounding box is at position (x, y).

func (*Circle) SetBottomMid

func (c *Circle) SetBottomMid(x, y float64)

SetBottomMid moves the Circle so the bottom mod position is at (x, y).

func (*Circle) SetBottomRight

func (c *Circle) SetBottomRight(x, y float64)

SetBottomRight moves the Circle so that the bottom right corner of its bounding box is at position (x, y).

func (*Circle) SetCircumference

func (c *Circle) SetCircumference(circ float64)

SetCircumference modifies the Circle so that it's cercumference is equal to circ.

func (*Circle) SetDiameter

func (c *Circle) SetDiameter(d float64)

SetDiameter modies the Circle so that it's diameter is equal to d.

func (*Circle) SetLeft

func (c *Circle) SetLeft(x float64)

SetLeft moves the Circle so that its left side is tangent to x.

func (*Circle) SetLeftMid

func (c *Circle) SetLeftMid(x, y float64)

SetLeftMid moves the Circle so that the left mid position is at (x, y).

func (*Circle) SetMid

func (c *Circle) SetMid(x, y float64)

SetMid moves the Circle so that its center is at position (x, y).

func (*Circle) SetPos

func (c *Circle) SetPos(pos Vec)

SetPos sets moves the Circel's center position.

func (*Circle) SetRight

func (c *Circle) SetRight(x float64)

SetRight moves the Circle so that its right side is tangent to x.

func (*Circle) SetRightMid

func (c *Circle) SetRightMid(x, y float64)

SetRightMid moves the Circle so that the right mid position is at (x, y).

func (*Circle) SetTop

func (c *Circle) SetTop(y float64)

SetTop moves the Circle so that its top side is tangent to y.

func (*Circle) SetTopLeft

func (c *Circle) SetTopLeft(x, y float64)

SetTopLeft moves the Circle so that the top left corner of its bounding box is at the given coordinates.

func (*Circle) SetTopMid

func (c *Circle) SetTopMid(x, y float64)

SetTopMid moves the Circle so that the top mid position is at (x, y).

func (*Circle) SetTopRight

func (c *Circle) SetTopRight(x, y float64)

SetTopRight moves the Circle so that the top right corner of its bounding box is at position (x, y).

func (Circle) String

func (c Circle) String() string

func (Circle) Top

func (c Circle) Top() float64

Top returns the y position that is tangent to the top of the Circle.

func (Circle) TopLeft

func (c Circle) TopLeft() (x, y float64)

TopLeft returns the coordinates of the top left corner of the Circle's bounding box.

func (Circle) TopMid

func (c Circle) TopMid() (x, y float64)

TopMid returns the coordinates of the top of the Circle above its center.

func (Circle) TopRight

func (c Circle) TopRight() (x, y float64)

TopRight returns the position of the top right corner of the Circle's bounding box.

func (*Circle) Union

func (c *Circle) Union(other Circle)

Union modifies this Circle to be the smallest one that would contain both itself and the given one

func (Circle) Unioned

func (c Circle) Unioned(other Circle) Circle

Unioned returns the smallest Circle that contains both this one and other.

func (Circle) XYR

func (c Circle) XYR() (x, y, r float64)

XYR returns the Circle's x, y and r values.

type EaseFn

type EaseFn func(t float64) float64

EaseFn is a function that takes a time t and returns a value. Ease functions taken from https://gist.github.com/gre/1650294 and https://github.com/warrenm/AHEasing/blob/master/AHEasing/easing.c

func EaseIn

func EaseIn(power float64) EaseFn

EaseIn creates an EaseFn with the given power that eases into the destination.

func EaseInOut

func EaseInOut(power float64) EaseFn

EaseInOut creates an EaseFn with the given power that eases both at the start and the end.

func EaseOut

func EaseOut(power float64) EaseFn

EaseOut creates an EaseFn with the given power that eases out of the start.

type NumGen

type NumGen func() float64

NumGen (Number Generator) is a function that returns a number.

func ConstNum

func ConstNum(n float64) NumGen

ConstNum returns a NumGen that always returns n.

func RandNum

func RandNum(a, b float64) NumGen

RandNum returns a NumGen that returns a uniform random number in [min(a, b), max(a, b)).

func RandRadius

func RandRadius(radius1, radius2 float64) NumGen

RandRadius returns a NumGen that returns a random, uniform circle radius in [min(radius1, radius2), max(radius1, radius2)). This function is undefined for negative radii.

type Ray

type Ray struct {
	Origin, Direction Vec
}

Ray is a 2-D ray with an origin and direction. The direction Vec does not have to be normalized, the methods of Ray will use the normalized direction when needed.

func RayAngle

func RayAngle(origin Vec, radians float64) Ray

RayAngle creates a new Ray whose direction is angled from the +x axis by the given radians.

func (Ray) At

func (r Ray) At(t float64) Vec

At returns the Vec that distance t from the Ray's origin in the Ray's direction. If the Ray's direction has length 0 then the origin will always be returned.

func (Ray) IntersectCircle

func (r Ray) IntersectCircle(c Circle) (tMin, tMax float64, hit bool)

IntersectCircle tests whether the Ray intersects the Circle. It returns both intersection points as tMin and tMax (tMin <= tMax). The t values can be negative, in which case the intersection point is behind the Ray (if tMin < 0 and tMax > 0 then the point is in the Circle). The function will return true for hit if the Ray intersects either behind or in front of the Ray. If hit is false then tMin and tMax are undefined. Use Ray.At with tMin or tMax to get the actual intersection points as a Vec.

func (Ray) IntersectLine

func (r Ray) IntersectLine(v1, v2 Vec) (t float64, hit bool)

IntersectLine tests whether the Ray intersects the line between v1 and v2. The return value of t is how far the Ray must be extended to hit the line, and will be negative if it hits behind the Ray. The value of hit will be true if the intersection point is in between v1 and v2. However the value of t will still be accurate if the Ray intersects outside of those points. If the Ray is parallel to the line between v1 and v2 then t will +/-Inf, no promises are made about its sign but math.IsInf(t, 0) will be true.

func (Ray) IntersectRect

func (r Ray) IntersectRect(rect Rect) (tMin, tMax float64, hit bool)

IntersectRect tests whether the Ray intersects the Rect. It returns both intersection points as tMin and tMax (tMin <= tMax). The t values can be negative, in which case the intersection point is behind the Ray (if tMin < 0 and tMax > 0 the nthe point is in the Rect). The function will return true for hit if the Ray intersects either behind or in front oe the Ray. If hit is false then tMin and tMax are undefined. Use Ray.At with tMin or tMax to get the actual intersection points as a Vec.

func (Ray) String

func (r Ray) String() string

type Rect

type Rect struct {
	X, Y, W, H float64
}

Rect defines rectangular coordinates, by the top left corner (X, Y), width W and height H.

func RectCorners

func RectCorners(x1, y1, x2, y2 float64) Rect

RectCorners creates a Rect given top left and bottom right corners.

func RectCornersVec

func RectCornersVec(topLeft, bottomRight Vec) Rect

RectCornersVec creates a Rect given top left and bottom right corners.

func RectUnion

func RectUnion(rects []Rect) Rect

RectUnion returns a Rect that contains all the given Rects. An empty list returns a Rect with size 0.

func RectVSize

func RectVSize(topLeft, size Vec) Rect

RectVSize is like RectVWH but also takes w and h as a Vec.

func RectVWH

func RectVWH(topLeft Vec, w, h float64) Rect

RectVWH is like RectXYWH but takes x and y as a Vec.

func RectWH

func RectWH(w, h float64) Rect

RectWH creates a Rect with the given size with the top left corner at (0, 0)

func RectXYWH

func RectXYWH(x, y, w, h float64) Rect

RectXYWH creates a Rect with top left corner (x, y) and size (w, h).

func Rectangle

func Rectangle(r image.Rectangle) Rect

Rectangle creates a Rect from an image.Rectangle.

func (Rect) Area

func (r Rect) Area() float64

Area returns the area of the rectangle.

func (Rect) Bottom

func (r Rect) Bottom() float64

Bottom returns the bottom boundary.

func (Rect) BottomLeft

func (r Rect) BottomLeft() (x, y float64)

BottomLeft returns the coordinates of the bottom left corner.

func (Rect) BottomMid

func (r Rect) BottomMid() (x, y float64)

BottomMid returns the coordinates at the bottom of the rectangle below the center.

func (Rect) BottomRight

func (r Rect) BottomRight() (x, y float64)

BottomRight returns the coordinates of the bottom right corner.

func (*Rect) Clamp

func (r *Rect) Clamp(bounds Rect)

Clamp moves this Rect so that it is within bounds. If it is too large than it is centered within bounds.

func (Rect) Clamped

func (r Rect) Clamped(bounds Rect) Rect

Clamped returns a new Rect that is moved to be within bounds. If it is too large than it is centered within bounds.

func (Rect) CollideCircle

func (r Rect) CollideCircle(c Circle) bool

CollideCircle returns true if the Rect is colliding with the Circle.

func (Rect) CollideCircleList

func (r Rect) CollideCircleList(cs []Circle) (i int, ok bool)

CollideCircleList returns the index of the first Circle the Rect collides with. If there is no collision then ok is false and i is undefined.

func (Rect) CollideCircleListAll

func (r Rect) CollideCircleListAll(cs []Circle) []int

CollideCircleListAll returns a list of indices of the Circles that collide with the Rect, or an empty list if none.

func (Rect) CollidePoint

func (r Rect) CollidePoint(x, y float64) bool

CollidePoint returns true if the point is within the Rect. A point along the right or bottom edge is not considered inside.

func (Rect) CollideRect

func (r Rect) CollideRect(other Rect) bool

CollideRect returns true if the Rects overlap.

func (Rect) CollideRectList

func (r Rect) CollideRectList(others []Rect) (i int, ok bool)

CollideRectList returns the index of the first Rect this one collides with. If there is no collision then ok is false and i is undefined.

func (Rect) CollideRectListAll

func (r Rect) CollideRectListAll(others []Rect) []int

CollideRectListAll returns a list of indices of the Rects that collide with this one, or an empty list if none.

func (Rect) Contains

func (r Rect) Contains(other Rect) bool

Contains returns true if other is completely inside this one.

func (*Rect) Fit

func (r *Rect) Fit(bounds Rect)

Fit modifies this Rect so that it is moved and resized to fit within bounds while maintaining its original aspect ratio.

func (Rect) Fitted

func (r Rect) Fitted(bounds Rect) Rect

Fitted returns a new Rect that is moved and resized to fit within bounds while maintaining its original aspect ratio.

func (*Rect) Inflate

func (r *Rect) Inflate(dw, dh float64)

Inflate keeps the same center but changes the size by the given amount, in place.

func (Rect) Inflated

func (r Rect) Inflated(dw, dh float64) Rect

Inflated returns a new Rect with the same center whose size is changed by the given amount.

func (Rect) Intersect

func (r Rect) Intersect(other Rect) Rect

Intersect returns a new Rect that marks the area where the two overlap. If there is no intersection the returned Rect will have 0 size.

func (Rect) Left

func (r Rect) Left() float64

Left returns the left boundary.

func (Rect) LeftMid

func (r Rect) LeftMid() (x, y float64)

LeftMid returns the coordinates at the left of the rectangle in line with the center.

func (Rect) Mid

func (r Rect) Mid() (x, y float64)

Mid returns the center coordinates.

func (Rect) MidX

func (r Rect) MidX() float64

MidX returns the center x coordinates

func (Rect) MidY

func (r Rect) MidY() float64

MidY returns the center y coordinates.

func (*Rect) Move

func (r *Rect) Move(dx, dy float64)

Move moves the Rect by the given offset, in place.

func (Rect) Moved

func (r Rect) Moved(dx, dy float64) Rect

Moved returns a new Rect moved by the given offset relative to this one.

func (*Rect) Normalize

func (r *Rect) Normalize()

Normalize flips the Rect in place if its size is negative.

func (Rect) Normalized

func (r Rect) Normalized() Rect

Normalized returns a flipped but equivalent Rect if its size is negative.

func (Rect) Rectangle

func (r Rect) Rectangle() image.Rectangle

Rectangle converts a Rect to image.Rectangle, discarding any fractional components.

func (Rect) Right

func (r Rect) Right() float64

Right returns the right boundary.

func (Rect) RightMid

func (r Rect) RightMid() (x, y float64)

RightMid returns the coordinates at the right of the rectangle in line with the center.

func (*Rect) SetBottom

func (r *Rect) SetBottom(bottom float64)

SetBottom sets the bottom boundary by moving the Rect.

func (*Rect) SetBottomLeft

func (r *Rect) SetBottomLeft(x, y float64)

SetBottomLeft set the coordinates of the bottom left corner by moving the Rect.

func (*Rect) SetBottomMid

func (r *Rect) SetBottomMid(x, y float64)

SetBottomMid sets the coordinates at the bottom of the rectangle below the center by moving the Rect.

func (*Rect) SetBottomRight

func (r *Rect) SetBottomRight(x, y float64)

SetBottomRight sets the coordinates of the bottom right corner by moving the Rect.

func (*Rect) SetLeft

func (r *Rect) SetLeft(left float64)

SetLeft sets the left boundary by moving the Rect.

func (*Rect) SetLeftMid

func (r *Rect) SetLeftMid(x, y float64)

SetLeftMid sets the coordinates at the left of the rectangle in line with the center by moving the Rect.

func (*Rect) SetMid

func (r *Rect) SetMid(x, y float64)

SetMid sets the center coordinates by moving the Rect.

func (*Rect) SetMidX

func (r *Rect) SetMidX(x float64)

SetMidX sets the center x coordinates by moving the Rect.

func (*Rect) SetMidY

func (r *Rect) SetMidY(y float64)

SetMidY set the center y coordinates by moving the Rect.

func (*Rect) SetRight

func (r *Rect) SetRight(right float64)

SetRight sets the right boundary by moving the Rect.

func (*Rect) SetRightMid

func (r *Rect) SetRightMid(x, y float64)

SetRightMid sets the coordinates at the right of the rectangle in line with the center by moving the Rect.

func (*Rect) SetSize

func (r *Rect) SetSize(w, h float64)

SetSize sets the width and height. Preserves top left corner.

func (*Rect) SetTop

func (r *Rect) SetTop(top float64)

SetTop sets the top boundary by moving the Rect.

func (*Rect) SetTopLeft

func (r *Rect) SetTopLeft(x, y float64)

SetTopLeft sets the coordinates of the top left corner by moving the Rect.

func (*Rect) SetTopMid

func (r *Rect) SetTopMid(x, y float64)

SetTopMid sets the coordinates at the top of the rectangle above the center by moving the Rect.

func (*Rect) SetTopRight

func (r *Rect) SetTopRight(x, y float64)

SetTopRight sets the coordinates of the top right corner by moving the Rect.

func (Rect) Size

func (r Rect) Size() (w, h float64)

Size returns the width and height.

func (Rect) String

func (r Rect) String() string

func (Rect) Top

func (r Rect) Top() (top float64)

Top returns the top boundary.

func (Rect) TopLeft

func (r Rect) TopLeft() (x, y float64)

TopLeft returns the coordinates of the top left corner.

func (Rect) TopMid

func (r Rect) TopMid() (x, y float64)

TopMid returns the coordinates at the top of the rectangle above the center.

func (Rect) TopRight

func (r Rect) TopRight() (x, y float64)

TopRight returns the coordinates of the top right corner.

func (*Rect) Union

func (r *Rect) Union(other Rect)

Union modifies this Rect to contain both itself and other.

func (Rect) Unioned

func (r Rect) Unioned(other Rect) Rect

Unioned returns a new Rect that contain both Rects.

type Shaker

type Shaker struct {
	// Seed can be used to change up the shaking behaviour, because all of the shake functions
	// are deterministic and often one wants it look different while keeping the other parameters
	// the same. Though if different values for t are always being used then changing the Seed
	// may not be necessary.
	Seed float64
	// StartTime is the time that the shaking starts. Reactivating a Shaker that has ended
	// should usually be as simple as updating the StartTime.
	StartTime time.Time
	// Duration is how long the shaking takes place.
	Duration time.Duration
	// Amplitude is the maximum length of the offset.
	Amplitude float64
	// Frequency controls how quickly the shaking happens.
	Frequency float64
	// Falloff modifies the amplitude over time, using StartTime and Duration. This makes
	// it easy to fade out the shaking.
	Falloff EaseFn
}

Shaker wraps the arguments to the shake functions for convenience and reusability.

func (*Shaker) EndTime

func (s *Shaker) EndTime() time.Time

EndTime returns the time that the shaking would end, if using the non-Const Shake functions.

func (*Shaker) Shake

func (s *Shaker) Shake(t time.Time) Vec

Shake takes a current time t and returns an offset. The time t will be clamped between s.StartTime and s.StartTime + s.Duration. This function makes use of StartTime, Duration, and Falloff to change the amplitude of the offset over time. The length of the Vec returned is never greater than the amplitude.

func (*Shaker) Shake1

func (s *Shaker) Shake1(t time.Time) float64

Shake1 is the same as Shake function but works in 1 dimension.

func (*Shaker) ShakeConst

func (s *Shaker) ShakeConst(t time.Time) Vec

ShakeConst takes a current time t and returns an offset. The max amplitude of the offset is not varied over time so StartTime, Duration, and Falloff are not used.

func (*Shaker) ShakeConst1

func (s *Shaker) ShakeConst1(t time.Time) float64

ShakeConst1 is the same as the ShakeConst but works in 1 dimension.

type Vec

type Vec struct {
	X, Y float64
}

Vec is a 2-D vector. Many of the functions for Vec have two versions, one that modifies the Vec and one that returns a new Vec. Their names follow a convention that is hopefully intuitive. For example, when working with Vec as a value you use v1.Plus(v2) which returns a new value and reads like when working with other value types such as "1 + 2". The other function, v1.Add(v2), adds v2 to v1 which actually modifies v1.

func EaseVec

func EaseVec(a, b Vec, t float64, easefn EaseFn) Vec

EaseVec interpolates from a to b by percent t following the path defined by easefn.

func LerpVec

func LerpVec(a, b Vec, t float64) Vec

LerpVec linear interpolates from a to b by percent t, where t=0 returns a and t=1 returns b.

func RandVec

func RandVec() Vec

RandVec returns a unit vector in a random direction.

func VecLA

func VecLA(length, rad float64) Vec

VecLA creates a Vec from a length and an angle.

func VecPoint

func VecPoint(p image.Point) Vec

VecPoint creates a Vec from an image.Point.

func VecXY

func VecXY(x, y float64) Vec

VecXY constructs a Vec from its arguments. Useful for making a Vec from a function that returns two floats.

func VecXYi

func VecXYi(x, y int) Vec

VecXYi creates a Vec from two ints.

func (*Vec) Add

func (v *Vec) Add(v2 Vec)

Add modifies v to be the sum of v2 and itself.

func (Vec) Angle

func (v Vec) Angle() float64

Angle returns the radians relative to the positive +x-axis (counterclockwise in screen coordinates). The returned value is in the range [-π, π).

func (Vec) AngleFrom

func (v Vec) AngleFrom(v2 Vec) float64

AngleFrom returns the radians from v2 to v (counterclockwise in screen coordinates). The returned value is in the range [-π, π).

func (*Vec) Clamp

func (v *Vec) Clamp(r Rect)

Clamp modifies the Vec to be closest point within Rect to the original Vec.

func (Vec) Clamped

func (v Vec) Clamped(r Rect) Vec

Clamped returns the closest point within Rect to the Vec.

func (Vec) Cross

func (v Vec) Cross(v2 Vec) float64

Cross returns the magnitude of the cross product of the two vectors.

func (Vec) Dist

func (v Vec) Dist(v2 Vec) float64

Dist returns the distance between the two vectors.

func (Vec) Dist2

func (v Vec) Dist2(v2 Vec) float64

Dist2 returns the distance squared between the two vectors.

func (*Vec) Div

func (v *Vec) Div(n float64)

Div modifies v to be itself divided by n.

func (Vec) DividedBy

func (v Vec) DividedBy(n float64) Vec

DividedBy returns a new vector that is v divided by n.

func (Vec) Dot

func (v Vec) Dot(v2 Vec) float64

Dot returns the dot product between the two vectors.

func (Vec) Equals

func (v Vec) Equals(v2 Vec, e float64) bool

Equals returns true if the corresponding components of the vectors are within the error e.

func (Vec) Len

func (v Vec) Len() float64

Len returns the length of the vector.

func (Vec) Len2

func (v Vec) Len2() float64

Len2 is the length of the vector squared.

func (*Vec) Limit

func (v *Vec) Limit(len float64)

Limit constrains the length of the vector be no greater than len. If the vector is already less than len then no change is made.

func (Vec) Limited

func (v Vec) Limited(len float64) Vec

Limited returns a new vector in the same direction as v with length no greater than len. The vector returned will be equivalent to v if v.Len() <= len.

func (*Vec) Map

func (v *Vec) Map(r1, r2 Rect)

Map takes v relative to r1 and moves it to the same relative position in r2. E.g. if v is in the center of r1 it will be moved to the center of r2.

func (Vec) Mapped

func (v Vec) Mapped(r1, r2 Rect) Vec

Mapped is like Map but returns a new Vec instead of modifing this one.

func (Vec) Minus

func (v Vec) Minus(v2 Vec) Vec

Minus returns a new vector that is the difference of the two vectors.

func (*Vec) Mod

func (v *Vec) Mod(r Rect)

Mod "wraps" the vector around the Rect so it stays with its bounds.

func (Vec) Modded

func (v Vec) Modded(r Rect) Vec

Modded is like Mod but returns a new Vec instead of modifing this one.

func (*Vec) Mul

func (v *Vec) Mul(n float64)

Mul modifies v to be itself times n.

func (*Vec) Normalize

func (v *Vec) Normalize()

Normalize modifies v to be of length one in the same direction.

func (Vec) Normalized

func (v Vec) Normalized() Vec

Normalized returns a new vector of length one in the same direction as v.

func (Vec) Plus

func (v Vec) Plus(v2 Vec) Vec

Plus returns a new vector that is the sum of the two vectors.

func (Vec) Point

func (v Vec) Point() image.Point

Point converts a Vec into image.Point, discarding any fractional components to X and Y.

func (*Vec) Project

func (v *Vec) Project(v2 Vec)

Project modifies v to be the vector that is the projection of v onto v2.

func (Vec) Projected

func (v Vec) Projected(v2 Vec) Vec

Projected return the Vec that is v projected onto v2.

func (*Vec) Rotate

func (v *Vec) Rotate(rad float64)

Rotate rotates the vector (counterclockwise in screen coordinates) by the given radians.

func (Vec) Rotated

func (v Vec) Rotated(rad float64) Vec

Rotated returns a new vector that is equal to this one rotated (counterclockwise in screen coordinates) by the given radians.

func (*Vec) Set

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

Set modifies the Vec's x and Y to those given.

func (*Vec) SetLen

func (v *Vec) SetLen(l float64)

SetLen sets the length of the vector. Negative lengths will flip the vectors direction. If v's length is zero then it will remain unchanged.

func (Vec) String

func (v Vec) String() string

func (*Vec) Sub

func (v *Vec) Sub(v2 Vec)

Sub modifies v to be the difference between itself and v2.

func (Vec) Times

func (v Vec) Times(n float64) Vec

Times returns a new vector that is v times n.

func (Vec) WithLen

func (v Vec) WithLen(l float64) Vec

WithLen returns a new vector in the same direction as v but with the given length. It v's length is 0 then the zero vector is returned.

func (Vec) XY

func (v Vec) XY() (x, y float64)

XY returns the Vec's components. Useful for passing a Vec to a function that takes x and y individually.

type VecGen

type VecGen func() Vec

VecGen (Vector Generator) is a function that returns a vector.

func DynamicVec

func DynamicVec(v *Vec) VecGen

DynamicVec returns a VecGen that always returns a copy of the vector pointed to by v.

func OffsetVec

func OffsetVec(gen VecGen, offset VecGen) VecGen

OffsetVec returns a VecGen that adds offset to gen. For example, if you wanted to use RandCircle as an initial position you might use the following to center the circle at position 100, 100.

OffsetVec(RandVecCircle(5, 10), StaticVec(Vec{X: 100, Y: 100}))

func RandVecArc

func RandVecArc(radius1, radius2, radians1, radians2 float64) VecGen

RandVecArc returns a VecGen that will generate a random vector within the slice of a circle defined by the parameters. The radians are relative to the +x axis. The length of the vector will be within [min(radius1, radius2), max(radius1, radius2)) and the angle will be within [min(radians1, radians2), max(radians1, radians2)).

func RandVecCircle

func RandVecCircle(radius1, radius2 float64) VecGen

RandVecCircle returns a VecGen that will generate a random vector whose length is in [min(radius1, radius2), max(radius1, radius2)), and is uniformly distributed within the circle.

func RandVecRect

func RandVecRect(rect Rect) VecGen

RandVecRect returns a VecGen that will generate a random vector within the given Rect.

func RandVecRects

func RandVecRects(rects []Rect) VecGen

RandVecRects returns a VecGen that will generate a random vector that is uniformly distributed between all the given rects. If the slice given is empty then the zero vector is returned.

func StaticVec

func StaticVec(v Vec) VecGen

StaticVec returns a VecGen that always returns the constant vector v.

Jump to

Keyboard shortcuts

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