gmath

package module
v0.0.0-...-39a72ed Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 8 Imported by: 45

Documentation

Index

Constants

View Source
const Epsilon = 1e-9

Variables

This section is empty.

Functions

func ArcContains

func ArcContains(angle, measure Rad, pos, point Vec) bool

func ArcSectionContains

func ArcSectionContains(angle, measure Rad, r float64, pos, point Vec) bool

func Clamp

func Clamp[T numeric](v, min, max T) T

func ClampMax

func ClampMax[T numeric](v, max T) T

func ClampMin

func ClampMin[T numeric](v, min T) T

func EqualApprox

func EqualApprox[T float](a, b T) bool

func Lerp

func Lerp(from, to, t float64) float64

func Percentage

func Percentage[T numeric](value, max T) T

func RandElem

func RandElem[T any](r *Rand, slice []T) (elem T)

func RandIndex

func RandIndex[T any](r *Rand, slice []T) int

func Shuffle

func Shuffle[T any](r *Rand, slice []T)

Types

type Pos

type Pos struct {
	Base   *Vec
	Offset Vec
}

Pos represents a position with optional offset relative to its base.

func MakePos

func MakePos(base Vec) Pos

func (Pos) Resolve

func (p Pos) Resolve() Vec

func (*Pos) Set

func (p *Pos) Set(base *Vec, offsetX, offsetY float64)

func (*Pos) SetBase

func (p *Pos) SetBase(base Vec)

func (Pos) WithOffset

func (p Pos) WithOffset(offsetX, offsetY float64) Pos

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 DegToRad

func DegToRad(deg float64) Rad

func (Rad) Abs

func (r Rad) Abs() float64

func (Rad) AngleDelta

func (r Rad) AngleDelta(r2 Rad) Rad

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) Cos

func (r Rad) Cos() float64

func (Rad) EqualApprox

func (r Rad) EqualApprox(other Rad) bool

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) LerpAngle

func (r Rad) LerpAngle(toAngle Rad, weight float64) Rad

func (Rad) Normalized

func (r Rad) Normalized() Rad

Normalized returns the equivalent radians value in [0, 2*Pi] range. For example, 3*Pi becomes just Pi.

func (Rad) Positive

func (r Rad) Positive() Rad

Positive returns the equivalent radian value expressed as a positive value.

func (Rad) RotatedTowards

func (r Rad) RotatedTowards(toAngle, amount Rad) Rad

func (Rad) Sin

func (r Rad) Sin() float64

type Rand

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

func (*Rand) Bool

func (r *Rand) Bool() bool

func (*Rand) Chance

func (r *Rand) Chance(probability float64) bool

func (*Rand) Float

func (r *Rand) Float() float64

func (*Rand) FloatRange

func (r *Rand) FloatRange(min, max float64) float64

func (*Rand) IntRange

func (r *Rand) IntRange(min, max int) int

func (*Rand) Offset

func (r *Rand) Offset(min, max float64) Vec

func (*Rand) PositiveInt

func (r *Rand) PositiveInt() int

func (*Rand) PositiveInt64

func (r *Rand) PositiveInt64() int64

func (*Rand) Rad

func (r *Rand) Rad() Rad

func (*Rand) SetSeed

func (r *Rand) SetSeed(seed int64)

func (*Rand) Uint64

func (r *Rand) Uint64() uint64

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 Range

type Range[T numeric] struct {
	Min T
	Max T
}

func MakeRange

func MakeRange[T numeric](min, max T) Range[T]

type Rect

type Rect struct {
	Min Vec
	Max Vec
}

func RectFromStd

func RectFromStd(src image.Rectangle) Rect

RectFromStd converts an image.Rectangle into a Rect. There is Rect.ToStd method to reverse it.

func (Rect) Center

func (r Rect) Center() Vec

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) Contains

func (r Rect) Contains(p Vec) bool

func (Rect) ContainsRect

func (r Rect) ContainsRect(other Rect) bool

func (Rect) Height

func (r Rect) Height() float64

func (Rect) Intersects

func (r Rect) Intersects(other Rect) bool

Intersects reports whether r and other have a common intersection.

func (Rect) IsEmpty

func (r Rect) IsEmpty() bool

func (Rect) ToStd

func (r Rect) ToStd() image.Rectangle

ToStd converts an Rect into a image.Rectangle. There is RectFromStd function to reverse it.

func (Rect) Width

func (r Rect) Width() float64

func (Rect) X1

func (r Rect) X1() float64

func (Rect) X2

func (r Rect) X2() float64

func (Rect) Y1

func (r Rect) Y1() float64

func (Rect) Y2

func (r Rect) Y2() float64

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

func (s *Slider) Add(v int)

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) Len

func (s *Slider) Len() int

Len returns the range of the values. Basically, it returns the max-min result.

func (*Slider) SetBounds

func (s *Slider) SetBounds(min, max int)

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

func (s *Slider) Sub(v int)

Sub subtracts v from the current slider value. The overflow/underflow behavior depends on the slider settings.

func (*Slider) TrySetValue

func (s *Slider) TrySetValue(v int) bool

TrySetValue assigns the v value to the slider if it fits its range. Returns true whether the slider value was assigned.

func (*Slider) Value

func (s *Slider) Value() int

Value returns the current slider value. The returned value is guarandeed to be in [min, max] range.

type Vec

type Vec struct {
	X float64
	Y float64
}

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

func RadToVec(angle Rad) Vec

RadToVec converts a given angle into a normalized vector that encodes that direction.

func VecFromStd

func VecFromStd(src image.Point) Vec

VecFromStd converts an image.Point into a Vec. There is Vec.ToStd method to reverse it.

func (Vec) Add

func (v Vec) Add(other Vec) Vec

func (Vec) Angle

func (v Vec) Angle() Rad

func (Vec) AngleToPoint

func (v Vec) AngleToPoint(pos Vec) Rad

AngleToPoint returns the angle from v towards the given point.

func (Vec) ClampLen

func (v Vec) ClampLen(limit float64) Vec

func (Vec) CubicInterpolate

func (v Vec) CubicInterpolate(preA, b, postB Vec, t float64) Vec

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 (v Vec) DirectionTo(v2 Vec) Vec

func (Vec) DistanceSquaredTo

func (v Vec) DistanceSquaredTo(v2 Vec) float64

func (Vec) DistanceTo

func (v Vec) DistanceTo(v2 Vec) float64

DistanceTo calculates the distance between the two vectors.

func (Vec) Div

func (v Vec) Div(other Vec) Vec

func (Vec) Divf

func (v Vec) Divf(scalar float64) Vec

func (Vec) Dot

func (v Vec) Dot(v2 Vec) float64

Dot returns a dot-product of the two vectors.

func (Vec) EqualApprox

func (v Vec) EqualApprox(other Vec) bool

func (Vec) IsNormalized

func (v Vec) IsNormalized() bool

IsNormalizer reports whether the vector is normalized. A vector is considered to be normalized if its length is 1.

func (Vec) IsZero

func (v Vec) IsZero() bool

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) Len

func (v Vec) Len() float64

Len reports the length of this vector (also known as magnitude).

func (Vec) LenSquared

func (v Vec) LenSquared() float64

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

func (v Vec) LinearInterpolate(to Vec, t float64) Vec

LinearInterpolate interpolates between two points by a normalized value. This function is commonly named "lerp".

func (Vec) MarshalJSON

func (v Vec) MarshalJSON() ([]byte, error)

func (Vec) Midpoint

func (v Vec) Midpoint(to Vec) Vec

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) MoveInDirection

func (v Vec) MoveInDirection(dist float64, dir Rad) Vec

func (Vec) MoveTowards

func (v Vec) MoveTowards(pos Vec, length float64) Vec

func (Vec) Mul

func (v Vec) Mul(other Vec) Vec

func (Vec) Mulf

func (v Vec) Mulf(scalar float64) Vec

func (Vec) Neg

func (v Vec) Neg() Vec

Neg applies unary minus (-) to the vector.

func (Vec) Normalized

func (v Vec) Normalized() Vec

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) Rotated

func (v Vec) Rotated(angle Rad) Vec

func (Vec) String

func (v Vec) String() string

String returns a pretty-printed representation of a 2D vector object.

func (Vec) Sub

func (v Vec) Sub(other Vec) Vec

func (Vec) ToStd

func (v Vec) ToStd() image.Point

ToStd converts Vec into image.Point. There is VecFromStd function to reverse it.

func (*Vec) UnmarshalJSON

func (v *Vec) UnmarshalJSON(data []byte) error

func (Vec) VecTowards

func (v Vec) VecTowards(pos Vec, length float64) Vec

Jump to

Keyboard shortcuts

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