Documentation ¶
Index ¶
- Constants
- func ArcContains(angle, measure Rad, pos, point Vec) bool
- func ArcSectionContains(angle, measure Rad, r float64, pos, point Vec) bool
- func Clamp[T numeric](v, min, max T) T
- func ClampMax[T numeric](v, max T) T
- func ClampMin[T numeric](v, min T) T
- func EqualApprox[T float](a, b T) bool
- func Lerp(from, to, t float64) float64
- func Percentage[T numeric](value, max T) T
- func RandElem[T any](r *Rand, slice []T) (elem T)
- func RandIndex[T any](r *Rand, slice []T) int
- func Shuffle[T any](r *Rand, slice []T)
- type Pos
- type Rad
- func (r Rad) Abs() float64
- func (r Rad) AngleDelta(r2 Rad) Rad
- func (r Rad) Cos() float64
- func (r Rad) EqualApprox(other Rad) bool
- func (r Rad) LerpAngle(toAngle Rad, weight float64) Rad
- func (r Rad) Normalized() Rad
- func (r Rad) Positive() Rad
- func (r Rad) RotatedTowards(toAngle, amount Rad) Rad
- func (r Rad) Sin() float64
- type Rand
- func (r *Rand) Bool() bool
- func (r *Rand) Chance(probability float64) bool
- func (r *Rand) Float() float64
- func (r *Rand) FloatRange(min, max float64) float64
- func (r *Rand) IntRange(min, max int) int
- func (r *Rand) Offset(min, max float64) Vec
- func (r *Rand) PositiveInt() int
- func (r *Rand) PositiveInt64() int64
- func (r *Rand) Rad() Rad
- func (r *Rand) SetSeed(seed int64)
- func (r *Rand) Uint64() uint64
- type RandPicker
- type Range
- type Rect
- func (r Rect) Center() Vec
- func (r Rect) Contains(p Vec) bool
- func (r Rect) ContainsRect(other Rect) bool
- func (r Rect) Height() float64
- func (r Rect) Intersects(other Rect) bool
- func (r Rect) IsEmpty() bool
- func (r Rect) ToStd() image.Rectangle
- func (r Rect) Width() float64
- func (r Rect) X1() float64
- func (r Rect) X2() float64
- func (r Rect) Y1() float64
- func (r Rect) Y2() float64
- type Slider
- type Vec
- func (v Vec) Add(other Vec) Vec
- func (v Vec) Angle() Rad
- func (v Vec) AngleToPoint(pos Vec) Rad
- func (v Vec) ClampLen(limit float64) Vec
- func (v Vec) CubicInterpolate(preA, b, postB Vec, t float64) Vec
- func (v Vec) DirectionTo(v2 Vec) Vec
- func (v Vec) DistanceSquaredTo(v2 Vec) float64
- func (v Vec) DistanceTo(v2 Vec) float64
- func (v Vec) Div(other Vec) Vec
- func (v Vec) Divf(scalar float64) Vec
- func (v Vec) Dot(v2 Vec) float64
- func (v Vec) EqualApprox(other Vec) bool
- func (v Vec) IsNormalized() bool
- func (v Vec) IsZero() bool
- func (v Vec) Len() float64
- func (v Vec) LenSquared() float64
- func (v Vec) LinearInterpolate(to Vec, t float64) Vec
- func (v Vec) MarshalJSON() ([]byte, error)
- func (v Vec) Midpoint(to Vec) Vec
- func (v Vec) MoveInDirection(dist float64, dir Rad) Vec
- func (v Vec) MoveTowards(pos Vec, length float64) Vec
- func (v Vec) Mul(other Vec) Vec
- func (v Vec) Mulf(scalar float64) Vec
- func (v Vec) Neg() Vec
- func (v Vec) Normalized() Vec
- func (v Vec) Rotated(angle Rad) Vec
- func (v Vec) String() string
- func (v Vec) Sub(other Vec) Vec
- func (v Vec) ToStd() image.Point
- func (v *Vec) UnmarshalJSON(data []byte) error
- func (v Vec) VecTowards(pos Vec, length float64) Vec
Constants ¶
const Epsilon = 1e-9
Variables ¶
This section is empty.
Functions ¶
func ArcContains ¶
func ArcSectionContains ¶
func EqualApprox ¶
func EqualApprox[T float](a, b T) bool
func Percentage ¶
func Percentage[T numeric](value, max T) T
Types ¶
type Pos ¶
Pos represents a position with optional offset relative to its base.
func (Pos) WithOffset ¶
type Rad ¶
type Rad float64
Rad represents a radian value. It's not capped in [0, 2*Pi] range.
In terms of the orientations, Pi rotation points the object down (South). Zero radians point towards the right side (East).
func (Rad) AngleDelta ¶
AngleDelta returns an angle delta between two radian values. The sign is preserved.
When using this function to calculate a rotation direction (CW vs CCW), r is a current rotation and r2 is a target rotation.
It doesn't need the angles to be normalized, r=0 and r=2*Pi are considered to have no delta. The return value is always normalized.
func (Rad) EqualApprox ¶
EqualApprox compares two radian values using EqualApprox function. Note that you may want to normalize the operands in some way before doing this.
func (Rad) Normalized ¶
Normalized returns the equivalent radians value in [0, 2*Pi] range. For example, 3*Pi becomes just Pi.
func (Rad) RotatedTowards ¶
type Rand ¶
type Rand struct {
// contains filtered or unexported fields
}
func (*Rand) FloatRange ¶
func (*Rand) PositiveInt ¶
func (*Rand) PositiveInt64 ¶
type RandPicker ¶
type RandPicker[T any] struct { // contains filtered or unexported fields }
RandPicker performs a uniformly distributed random probing among the given objects with weights. Higher the weight, higher the chance of that object of being picked.
func NewRandPicker ¶
func NewRandPicker[T any](r *Rand) *RandPicker[T]
func (*RandPicker[T]) AddOption ¶
func (p *RandPicker[T]) AddOption(value T, weight float64)
func (*RandPicker[T]) IsEmpty ¶
func (p *RandPicker[T]) IsEmpty() bool
func (*RandPicker[T]) Pick ¶
func (p *RandPicker[T]) Pick() T
func (*RandPicker[T]) Reset ¶
func (p *RandPicker[T]) Reset()
type Rect ¶
func RectFromStd ¶
RectFromStd converts an image.Rectangle into a Rect. There is Rect.ToStd method to reverse it.
func (Rect) Center ¶
Center returns the center point of this rectangle.
This center point may need some rounding, since a rect of a 3x3 size would return {1.5, 1.5}.
func (Rect) ContainsRect ¶
func (Rect) Intersects ¶
Intersects reports whether r and other have a common intersection.
func (Rect) ToStd ¶
ToStd converts an Rect into a image.Rectangle. There is RectFromStd function to reverse it.
type Slider ¶
type Slider struct { // Clamp makes the slider use clamping overflow/underflow strategy // instead of the default wrapping around strategy. Clamp bool // contains filtered or unexported fields }
Slider is a value that can be increased and decreased with a custom overflow/underflow behavior.
Min/Max fields control the range of the accepted values.
It's a useful foundation for more high-level concepts like progress bars and gauges, paginators, option button selector, etc.
func (*Slider) Add ¶
Add adds v to the current slider value. The overflow/underflow behavior depends on the slider settings.
func (*Slider) Dec ¶
func (s *Slider) Dec()
Dec subtracts 1 from the slider value. Semantically identical to Sub(1), but more efficient.
func (*Slider) Inc ¶
func (s *Slider) Inc()
Inc adds 1 to the slider value. Semantically identical to Add(1), but more efficient.
func (*Slider) SetBounds ¶
SetBounds sets the slider values range. It also sets the current value to min. Use TrySetValue if you need to override that.
If max<min, this method panics.
func (*Slider) Sub ¶
Sub subtracts v from the current slider value. The overflow/underflow behavior depends on the slider settings.
func (*Slider) TrySetValue ¶
TrySetValue assigns the v value to the slider if it fits its range. Returns true whether the slider value was assigned.
type Vec ¶
Vec is a 2-element structure that is used to represent positions, velocities, and other kinds numerical pairs.
Its implementation as well as its API is inspired by Vector2 type of the Godot game engine. Where feasible, its adjusted to fit Go coding conventions better. Also, earlier versions of Godot used 32-bit values for X and Y; our vector uses 64-bit values.
Since Go has no operator overloading, we implement scalar forms of operations with "f" suffix. So, Add() is used to add two vectors while Addf() is used to add scalar to the vector.
func RadToVec ¶
RadToVec converts a given angle into a normalized vector that encodes that direction.
func VecFromStd ¶
VecFromStd converts an image.Point into a Vec. There is Vec.ToStd method to reverse it.
func (Vec) AngleToPoint ¶
AngleToPoint returns the angle from v towards the given point.
func (Vec) CubicInterpolate ¶
CubicInterpolate interpolates between a (this vector) and b using preA and postB as handles. The t arguments specifies the interpolation progression (a value from 0 to 1). With t=0 it returns a, with t=1 it returns b.
func (Vec) DirectionTo ¶
func (Vec) DistanceSquaredTo ¶
func (Vec) DistanceTo ¶
DistanceTo calculates the distance between the two vectors.
func (Vec) EqualApprox ¶
func (Vec) IsNormalized ¶
IsNormalizer reports whether the vector is normalized. A vector is considered to be normalized if its length is 1.
func (Vec) IsZero ¶
IsZero reports whether v is a zero value vector. A zero value vector has X=0 and Y=0, created with Vec{}.
The zero value vector has a property that its length is 0, but not all zero length vectors are zero value vectors.
func (Vec) LenSquared ¶
LenSquared returns the squared length of this vector.
This function runs faster than Len(), so prefer it if you need to compare vectors or need the squared distance for some formula.
func (Vec) LinearInterpolate ¶
LinearInterpolate interpolates between two points by a normalized value. This function is commonly named "lerp".
func (Vec) MarshalJSON ¶
func (Vec) Midpoint ¶
Midpoint returns the middle point vector of two point vectors.
If we imagine [v] and [to] form a line, the midpoint would be a central point of this line.
func (Vec) Normalized ¶
Normalized returns the vector scaled to unit length. Functionally equivalent to `v.Divf(v.Len())`.
Special case: for zero value vectors it returns that unchanged.
func (Vec) ToStd ¶
ToStd converts Vec into image.Point. There is VecFromStd function to reverse it.