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 ¶
- Variables
- func Clamp(n, a, b float64) float64
- func Ease(a, b, t float64, easefn EaseFn) float64
- func EaseInBack(t float64) float64
- func EaseInBounce(t float64) float64
- func EaseInCirc(t float64) float64
- func EaseInElastic(t float64) float64
- func EaseInExpo(t float64) float64
- func EaseInOutBack(t float64) float64
- func EaseInOutBounce(t float64) float64
- func EaseInOutCirc(t float64) float64
- func EaseInOutElastic(t float64) float64
- func EaseInOutExpo(t float64) float64
- func EaseInOutSine(t float64) float64
- func EaseInSine(t float64) float64
- func EaseOutBack(t float64) float64
- func EaseOutBounce(t float64) float64
- func EaseOutCirc(t float64) float64
- func EaseOutElastic(t float64) float64
- func EaseOutExpo(t float64) float64
- func EaseOutSine(t float64) float64
- func I2F2(i1, i2 int) (f1, f2 float64)
- func Lerp(a, b, t float64) float64
- func Map(n, a1, b1, a2, b2 float64) float64
- func Mod(a, b float64) float64
- func Perlin(x, y, z float64) float64
- func PerlinOctave(x, y, z float64, octaves int, persistence float64) float64
- func RandIndex(weights []float64) int
- type Circle
- func (c Circle) Area() float64
- func (c Circle) Bottom() float64
- func (c Circle) BottomLeft() (x, y float64)
- func (c Circle) BottomMid() (x, y float64)
- func (c Circle) BottomRight() (x, y float64)
- func (c Circle) BoundingRect() Rect
- func (c Circle) Circumference() float64
- func (c Circle) CollideCircle(other Circle) bool
- func (c Circle) CollideCircleList(others []Circle) (i int, ok bool)
- func (c Circle) CollideCircleListAll(others []Circle) []int
- func (c Circle) CollidePoint(x, y float64) bool
- func (c Circle) CollideRect(r Rect) bool
- func (c Circle) CollideRectList(rs []Rect) (i int, ok bool)
- func (c Circle) CollideRectListAll(rs []Rect) []int
- func (c Circle) Contains(other Circle) bool
- func (c Circle) Diameter() float64
- func (c Circle) Equals(other Circle, e float64) bool
- func (c *Circle) Inflate(dr float64)
- func (c Circle) Inflated(dr float64) Circle
- func (c Circle) InscribedRect() Rect
- func (c Circle) Left() float64
- func (c Circle) LeftMid() (x, y float64)
- func (c Circle) Mid() (x, y float64)
- func (c *Circle) Move(dx, dy float64)
- func (c Circle) Moved(dx, dy float64) Circle
- func (c *Circle) Normalize()
- func (c Circle) Normalized() Circle
- func (c Circle) PointAt(radians float64) (x, y float64)
- func (c Circle) Pos() Vec
- func (c Circle) PosR() (pos Vec, r float64)
- func (c Circle) Right() float64
- func (c Circle) RightMid() (x, y float64)
- func (c *Circle) SetBottom(y float64)
- func (c *Circle) SetBottomLeft(x, y float64)
- func (c *Circle) SetBottomMid(x, y float64)
- func (c *Circle) SetBottomRight(x, y float64)
- func (c *Circle) SetCircumference(circ float64)
- func (c *Circle) SetDiameter(d float64)
- func (c *Circle) SetLeft(x float64)
- func (c *Circle) SetLeftMid(x, y float64)
- func (c *Circle) SetMid(x, y float64)
- func (c *Circle) SetPos(pos Vec)
- func (c *Circle) SetRight(x float64)
- func (c *Circle) SetRightMid(x, y float64)
- func (c *Circle) SetTop(y float64)
- func (c *Circle) SetTopLeft(x, y float64)
- func (c *Circle) SetTopMid(x, y float64)
- func (c *Circle) SetTopRight(x, y float64)
- func (c Circle) String() string
- func (c Circle) Top() float64
- func (c Circle) TopLeft() (x, y float64)
- func (c Circle) TopMid() (x, y float64)
- func (c Circle) TopRight() (x, y float64)
- func (c *Circle) Union(other Circle)
- func (c Circle) Unioned(other Circle) Circle
- func (c Circle) XYR() (x, y, r float64)
- type EaseFn
- type NumGen
- type Ray
- type Rect
- func RectCorners(x1, y1, x2, y2 float64) Rect
- func RectCornersVec(topLeft, bottomRight Vec) Rect
- func RectUnion(rects []Rect) Rect
- func RectVSize(topLeft, size Vec) Rect
- func RectVWH(topLeft Vec, w, h float64) Rect
- func RectWH(w, h float64) Rect
- func RectXYWH(x, y, w, h float64) Rect
- func Rectangle(r image.Rectangle) Rect
- func (r Rect) Area() float64
- func (r Rect) Bottom() float64
- func (r Rect) BottomLeft() (x, y float64)
- func (r Rect) BottomMid() (x, y float64)
- func (r Rect) BottomRight() (x, y float64)
- func (r *Rect) Clamp(bounds Rect)
- func (r Rect) Clamped(bounds Rect) Rect
- func (r Rect) CollideCircle(c Circle) bool
- func (r Rect) CollideCircleList(cs []Circle) (i int, ok bool)
- func (r Rect) CollideCircleListAll(cs []Circle) []int
- func (r Rect) CollidePoint(x, y float64) bool
- func (r Rect) CollideRect(other Rect) bool
- func (r Rect) CollideRectList(others []Rect) (i int, ok bool)
- func (r Rect) CollideRectListAll(others []Rect) []int
- func (r Rect) Contains(other Rect) bool
- func (r *Rect) Fit(bounds Rect)
- func (r Rect) Fitted(bounds Rect) Rect
- func (r *Rect) Inflate(dw, dh float64)
- func (r Rect) Inflated(dw, dh float64) Rect
- func (r Rect) Intersect(other Rect) Rect
- func (r Rect) Left() float64
- func (r Rect) LeftMid() (x, y float64)
- func (r Rect) Mid() (x, y float64)
- func (r Rect) MidX() float64
- func (r Rect) MidY() float64
- func (r *Rect) Move(dx, dy float64)
- func (r Rect) Moved(dx, dy float64) Rect
- func (r *Rect) Normalize()
- func (r Rect) Normalized() Rect
- func (r Rect) Rectangle() image.Rectangle
- func (r Rect) Right() float64
- func (r Rect) RightMid() (x, y float64)
- func (r *Rect) SetBottom(bottom float64)
- func (r *Rect) SetBottomLeft(x, y float64)
- func (r *Rect) SetBottomMid(x, y float64)
- func (r *Rect) SetBottomRight(x, y float64)
- func (r *Rect) SetLeft(left float64)
- func (r *Rect) SetLeftMid(x, y float64)
- func (r *Rect) SetMid(x, y float64)
- func (r *Rect) SetMidX(x float64)
- func (r *Rect) SetMidY(y float64)
- func (r *Rect) SetRight(right float64)
- func (r *Rect) SetRightMid(x, y float64)
- func (r *Rect) SetSize(w, h float64)
- func (r *Rect) SetTop(top float64)
- func (r *Rect) SetTopLeft(x, y float64)
- func (r *Rect) SetTopMid(x, y float64)
- func (r *Rect) SetTopRight(x, y float64)
- func (r Rect) Size() (w, h float64)
- func (r Rect) String() string
- func (r Rect) Top() (top float64)
- func (r Rect) TopLeft() (x, y float64)
- func (r Rect) TopMid() (x, y float64)
- func (r Rect) TopRight() (x, y float64)
- func (r *Rect) Union(other Rect)
- func (r Rect) Unioned(other Rect) Rect
- type Shaker
- type Vec
- func (v *Vec) Add(v2 Vec)
- func (v Vec) Angle() float64
- func (v Vec) AngleFrom(v2 Vec) float64
- func (v *Vec) Clamp(r Rect)
- func (v Vec) Clamped(r Rect) Vec
- func (v Vec) Cross(v2 Vec) float64
- func (v Vec) Dist(v2 Vec) float64
- func (v Vec) Dist2(v2 Vec) float64
- func (v *Vec) Div(n float64)
- func (v Vec) DividedBy(n float64) Vec
- func (v Vec) Dot(v2 Vec) float64
- func (v Vec) Equals(v2 Vec, e float64) bool
- func (v Vec) Len() float64
- func (v Vec) Len2() float64
- func (v *Vec) Limit(len float64)
- func (v Vec) Limited(len float64) Vec
- func (v *Vec) Map(r1, r2 Rect)
- func (v Vec) Mapped(r1, r2 Rect) Vec
- func (v Vec) Minus(v2 Vec) Vec
- func (v *Vec) Mod(r Rect)
- func (v Vec) Modded(r Rect) Vec
- func (v *Vec) Mul(n float64)
- func (v *Vec) Normalize()
- func (v Vec) Normalized() Vec
- func (v Vec) Plus(v2 Vec) Vec
- func (v Vec) Point() image.Point
- func (v *Vec) Project(v2 Vec)
- func (v Vec) Projected(v2 Vec) Vec
- func (v *Vec) Rotate(rad float64)
- func (v Vec) Rotated(rad float64) Vec
- func (v *Vec) Set(x, y float64)
- func (v *Vec) SetLen(l float64)
- func (v Vec) String() string
- func (v *Vec) Sub(v2 Vec)
- func (v Vec) Times(n float64) Vec
- func (v Vec) WithLen(l float64) Vec
- func (v Vec) XY() (x, y float64)
- type VecGen
- func DynamicVec(v *Vec) VecGen
- func OffsetVec(gen VecGen, offset VecGen) VecGen
- func RandVecArc(radius1, radius2, radians1, radians2 float64) VecGen
- func RandVecCircle(radius1, radius2 float64) VecGen
- func RandVecRect(rect Rect) VecGen
- func RandVecRects(rects []Rect) VecGen
- func StaticVec(v Vec) VecGen
Constants ¶
This section is empty.
Variables ¶
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) )
var Vec0 = Vec{}
Vec0 is the zero vector.
Functions ¶
func Clamp ¶
Clamp returns n if it is within [min(a, b), max(a, b)] otherwise it returns the closer of a and b.
func EaseInBack ¶
func EaseInBounce ¶
func EaseInCirc ¶
func EaseInElastic ¶
func EaseInExpo ¶
func EaseInOutBack ¶
func EaseInOutBounce ¶
func EaseInOutCirc ¶
func EaseInOutElastic ¶
func EaseInOutExpo ¶
func EaseInOutSine ¶
func EaseInSine ¶
func EaseOutBack ¶
func EaseOutBounce ¶
func EaseOutCirc ¶
func EaseOutElastic ¶
func EaseOutExpo ¶
func EaseOutSine ¶
func I2F2 ¶
I2F2 takes 2 ints and converts them to float64. e.g.
rect.SetTopLeft(I2F2(functionThatReturns2Ints()))
func Lerp ¶
Lerp linear interpolates from a to b by percent t, where t=0 returns a and t=1 returns b.
func Map ¶
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 ¶
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 PerlinOctave ¶
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 ¶
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 ¶
CircleCircumscribe returns the smallest circle that surrounds the Rect.
func CircleInscribe ¶
CircleInscribe returns the largest Circle that fits insided the Rect, and shares its center with the Rect.
func CircleVecR ¶
CircleVecR creates a Circle at position pos with radius r.
func (Circle) BottomLeft ¶
BottomLeft returns the position of the bottom left corner of the Circle's bounding box.
func (Circle) BottomMid ¶
BottomMid returns the position of the bottom of the Circle, bellow its center.
func (Circle) BottomRight ¶
BottomRight returns the position of the bottom right corner of the Circle's bounding box.
func (Circle) BoundingRect ¶
BoundingRect returns the smallest Rect that surrounds the Circle.
func (Circle) Circumference ¶
Circumference returns the circumference of the Circle.
func (Circle) CollideCircle ¶
CollideCircle returns true if other overlaps with this one.
func (Circle) CollideCircleList ¶
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 ¶
CollideCircleListAll returns a list of indices of the Circles that collide with this one, or an empty list if none.
func (Circle) CollidePoint ¶
CollidePoint returns true if the given point is inside the Circle.
func (Circle) CollideRect ¶
CollideRect returns true if the Circle is colliding with the Rect.
func (Circle) CollideRectList ¶
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 ¶
CollideRectListAll returns a list of indices of the Rects that collide with the Circle, or an empty list if none.
func (Circle) Equals ¶
Equals returns true if the two circles position and radius are with in error e.
func (Circle) Inflated ¶
Inflated returns a new Circle with the same center but changes the size by the given amount.
func (Circle) InscribedRect ¶
InscribedRect returns the largest square that fits inside the Circle.
func (Circle) LeftMid ¶
LeftMid returns the position of the left of the Circle, inline with its center.
func (Circle) Moved ¶
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 ¶
Normalized returns a new Circle with a positive radius.
func (Circle) PointAt ¶
PointAt returns the position of the point along the Circle's edge that is at the given angle (from +x axis).
func (Circle) RightMid ¶
RightMid returns the position of the right of the Circle, inline with its center.
func (*Circle) SetBottomLeft ¶
SetBottomLeft moves the Circle so that the bottom left corner of its bounding box is at position (x, y).
func (*Circle) SetBottomMid ¶
SetBottomMid moves the Circle so the bottom mod position is at (x, y).
func (*Circle) SetBottomRight ¶
SetBottomRight moves the Circle so that the bottom right corner of its bounding box is at position (x, y).
func (*Circle) SetCircumference ¶
SetCircumference modifies the Circle so that it's cercumference is equal to circ.
func (*Circle) SetDiameter ¶
SetDiameter modies the Circle so that it's diameter is equal to d.
func (*Circle) SetLeftMid ¶
SetLeftMid moves the Circle so that the left mid position is at (x, y).
func (*Circle) SetRightMid ¶
SetRightMid moves the Circle so that the right mid position is at (x, y).
func (*Circle) SetTopLeft ¶
SetTopLeft moves the Circle so that the top left corner of its bounding box is at the given coordinates.
func (*Circle) SetTopRight ¶
SetTopRight moves the Circle so that the top right corner of its bounding box is at position (x, y).
func (Circle) TopLeft ¶
TopLeft returns the coordinates of the top left corner of the Circle's bounding box.
func (Circle) TopRight ¶
TopRight returns the position of the top right corner of the Circle's bounding box.
func (*Circle) Union ¶
Union modifies this Circle to be the smallest one that would contain both itself and the given one
type EaseFn ¶
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
type NumGen ¶
type NumGen func() float64
NumGen (Number Generator) is a function that returns a number.
func RandNum ¶
RandNum returns a NumGen that returns a uniform random number in [min(a, b), max(a, b)).
func RandRadius ¶
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 ¶
RayAngle creates a new Ray whose direction is angled from the +x axis by the given radians.
func (Ray) At ¶
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 ¶
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 ¶
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 ¶
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.
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 ¶
RectCorners creates a Rect given top left and bottom right corners.
func RectCornersVec ¶
RectCornersVec creates a Rect given top left and bottom right corners.
func RectUnion ¶
RectUnion returns a Rect that contains all the given Rects. An empty list returns a Rect with size 0.
func (Rect) BottomLeft ¶
BottomLeft returns the coordinates of the bottom left corner.
func (Rect) BottomMid ¶
BottomMid returns the coordinates at the bottom of the rectangle below the center.
func (Rect) BottomRight ¶
BottomRight returns the coordinates of the bottom right corner.
func (*Rect) Clamp ¶
Clamp moves this Rect so that it is within bounds. If it is too large than it is centered within bounds.
func (Rect) Clamped ¶
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 ¶
CollideCircle returns true if the Rect is colliding with the Circle.
func (Rect) CollideCircleList ¶
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 ¶
CollideCircleListAll returns a list of indices of the Circles that collide with the Rect, or an empty list if none.
func (Rect) CollidePoint ¶
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 ¶
CollideRect returns true if the Rects overlap.
func (Rect) CollideRectList ¶
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 ¶
CollideRectListAll returns a list of indices of the Rects that collide with this one, or an empty list if none.
func (*Rect) Fit ¶
Fit modifies this Rect so that it is moved and resized to fit within bounds while maintaining its original aspect ratio.
func (Rect) Fitted ¶
Fitted returns a new Rect that is moved and resized to fit within bounds while maintaining its original aspect ratio.
func (*Rect) Inflate ¶
Inflate keeps the same center but changes the size by the given amount, in place.
func (Rect) Inflated ¶
Inflated returns a new Rect with the same center whose size is changed by the given amount.
func (Rect) Intersect ¶
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) LeftMid ¶
LeftMid returns the coordinates at the left of the rectangle in line with the center.
func (*Rect) Normalize ¶
func (r *Rect) Normalize()
Normalize flips the Rect in place if its size is negative.
func (Rect) Normalized ¶
Normalized returns a flipped but equivalent Rect if its size is negative.
func (Rect) Rectangle ¶
Rectangle converts a Rect to image.Rectangle, discarding any fractional components.
func (Rect) RightMid ¶
RightMid returns the coordinates at the right of the rectangle in line with the center.
func (*Rect) SetBottomLeft ¶
SetBottomLeft set the coordinates of the bottom left corner by moving the Rect.
func (*Rect) SetBottomMid ¶
SetBottomMid sets the coordinates at the bottom of the rectangle below the center by moving the Rect.
func (*Rect) SetBottomRight ¶
SetBottomRight sets the coordinates of the bottom right corner by moving the Rect.
func (*Rect) SetLeftMid ¶
SetLeftMid sets the coordinates at the left of the rectangle in line with the center by moving the Rect.
func (*Rect) SetRightMid ¶
SetRightMid sets the coordinates at the right of the rectangle in line with the center by moving the Rect.
func (*Rect) SetTopLeft ¶
SetTopLeft sets the coordinates of the top left corner by moving the Rect.
func (*Rect) SetTopMid ¶
SetTopMid sets the coordinates at the top of the rectangle above the center by moving the Rect.
func (*Rect) SetTopRight ¶
SetTopRight sets the coordinates of the top right corner by moving the Rect.
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 ¶
EndTime returns the time that the shaking would end, if using the non-Const Shake functions.
func (*Shaker) Shake ¶
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) ShakeConst ¶
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.
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 LerpVec ¶
LerpVec linear interpolates from a to b by percent t, where t=0 returns a and t=1 returns b.
func VecXY ¶
VecXY constructs a Vec from its arguments. Useful for making a Vec from a function that returns two floats.
func (Vec) Angle ¶
Angle returns the radians relative to the positive +x-axis (counterclockwise in screen coordinates). The returned value is in the range [-π, π).
func (Vec) AngleFrom ¶
AngleFrom returns the radians from v2 to v (counterclockwise in screen coordinates). The returned value is in the range [-π, π).
func (Vec) Equals ¶
Equals returns true if the corresponding components of the vectors are within the error e.
func (*Vec) Limit ¶
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 ¶
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 ¶
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) Normalize ¶
func (v *Vec) Normalize()
Normalize modifies v to be of length one in the same direction.
func (Vec) Normalized ¶
Normalized returns a new vector of length one in the same direction as v.
func (Vec) Point ¶
Point converts a Vec into image.Point, discarding any fractional components to X and Y.
func (*Vec) Rotate ¶
Rotate rotates the vector (counterclockwise in screen coordinates) by the given radians.
func (Vec) Rotated ¶
Rotated returns a new vector that is equal to this one rotated (counterclockwise in screen coordinates) by the given radians.
func (*Vec) SetLen ¶
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.
type VecGen ¶
type VecGen func() Vec
VecGen (Vector Generator) is a function that returns a vector.
func DynamicVec ¶
DynamicVec returns a VecGen that always returns a copy of the vector pointed to by v.
func OffsetVec ¶
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 ¶
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 ¶
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 ¶
RandVecRect returns a VecGen that will generate a random vector within the given Rect.
func RandVecRects ¶
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.