Documentation ¶
Overview ¶
Go programming helpers for common maths needs; plus vectors, matrices and quaternions.
Index ¶
- Constants
- Variables
- func Clamp(val, c0, c1 float64) float64
- func Clamp01(v float64) float64
- func ClosestPowerOfTwo(v uint32) uint32
- func DegToRad(degrees float64) float64
- func DeltaAngle(cur, target float64) float64
- func Eq(a, b float64) (eq bool)
- func InvLerp(from, to, val float64) float64
- func IsPowerOfTwo(x uint32) bool
- func Lerp(a, b, t float64) float64
- func LerpAngle(a, b, t float64) float64
- func Mat3Identities(mats ...*Mat3)
- func Mat4Identities(mats ...*Mat4)
- func NextPowerOfTwo(v uint32) uint32
- func Percent(p, of float64) float64
- func PingPong(t, l float64) float64
- func RadToDeg(radians float64) float64
- func Round(v float64) (fint float64)
- func Sign(v float64) (sign float64)
- func SmoothStep(from, to, t float64) float64
- func SmootherStep(from, to, t float64) float64
- func SumFrom1To(to int) int
- func SumFromTo(from, to int) int
- type Mat3
- type Mat4
- func NewMat4Add(a, b *Mat4) (mat *Mat4)
- func NewMat4Frustum(left, right, bottom, top, near, far float64) (mat *Mat4)
- func NewMat4Identity() (mat *Mat4)
- func NewMat4Lookat(eyePos, lookTarget, upVec *Vec3) (mat *Mat4)
- func NewMat4Mult1(m *Mat4, v float64) (mat *Mat4)
- func NewMat4Mult4(one, two *Mat4) (mat *Mat4)
- func NewMat4MultN(mats ...*Mat4) (mat *Mat4)
- func NewMat4Orient(lookTarget, worldUp *Vec3) (mat *Mat4)
- func NewMat4Perspective(fovY, aspect, near, far float64) (mat *Mat4)
- func NewMat4RotationX(rad float64) (mat *Mat4)
- func NewMat4RotationY(rad float64) (mat *Mat4)
- func NewMat4RotationZ(rad float64) (mat *Mat4)
- func NewMat4Scaling(vec *Vec3) (mat *Mat4)
- func NewMat4Sub(a, b *Mat4) (mat *Mat4)
- func NewMat4Translation(vec *Vec3) (mat *Mat4)
- func (me *Mat4) Abs() (abs *Mat4)
- func (me *Mat4) Add(mat *Mat4)
- func (me *Mat4) Clear()
- func (me *Mat4) Clone() (mat *Mat4)
- func (me *Mat4) CopyFrom(mat *Mat4)
- func (me *Mat4) CopyTo(mat *Mat4)
- func (me *Mat4) Frustum(left, right, bottom, top, near, far float64)
- func (me *Mat4) Identity()
- func (me *Mat4) Lookat(eyePos, lookTarget, upVec *Vec3)
- func (me *Mat4) Mult1(v float64)
- func (me *Mat4) Orient(lookTarget, worldUp *Vec3)
- func (me *Mat4) Perspective(fovYDeg, a, n, f float64) (fovYRadHalf float64)
- func (me *Mat4) RotationX(rad float64)
- func (me *Mat4) RotationY(rad float64)
- func (me *Mat4) RotationZ(rad float64)
- func (me *Mat4) Scaling(vec *Vec3)
- func (me *Mat4) SetFromMult4(one, two *Mat4)
- func (me *Mat4) SetFromMultN(mats ...*Mat4)
- func (me *Mat4) SetFromTransposeOf(mat *Mat4)
- func (me *Mat4) Sub(mat *Mat4)
- func (me *Mat4) Translation(vec *Vec3)
- func (me *Mat4) Transposed() (mat *Mat4)
- type Quat
- type Vec2
- func (me *Vec2) Add(vec *Vec2)
- func (me *Vec2) AddedDiv(a *Vec2, d float64) *Vec2
- func (me *Vec2) AngleDeg(to *Vec2) float64
- func (me *Vec2) AngleRad(to *Vec2) float64
- func (me *Vec2) ClampMagnitude(maxLength float64) *Vec2
- func (me *Vec2) Clear()
- func (me *Vec2) Distance(vec *Vec2) float64
- func (me *Vec2) Div(vec *Vec2) *Vec2
- func (me *Vec2) DivSafe(vec *Vec2) *Vec2
- func (me *Vec2) Divide(d float64)
- func (me *Vec2) Divided(d float64) *Vec2
- func (me *Vec2) Dot(vec *Vec2) float64
- func (me *Vec2) Eq(vec *Vec2) bool
- func (me *Vec2) Length() float64
- func (me *Vec2) Magnitude() float64
- func (me *Vec2) MoveTowards(target *Vec2, maxDistanceDelta float64) *Vec2
- func (me *Vec2) Mult(vec *Vec2) *Vec2
- func (me *Vec2) Negate() *Vec2
- func (me *Vec2) Normalize()
- func (me *Vec2) NormalizeSafe()
- func (me *Vec2) Normalized() *Vec2
- func (me *Vec2) NormalizedSafe() *Vec2
- func (me *Vec2) NormalizedScaled(factor float64) *Vec2
- func (me *Vec2) NormalizedScaledSafe(factor float64) *Vec2
- func (me *Vec2) Scale(factor float64)
- func (me *Vec2) Scaled(factor float64) *Vec2
- func (me *Vec2) Set(x, y float64)
- func (me *Vec2) String() string
- func (me *Vec2) Sub(vec *Vec2) *Vec2
- func (me *Vec2) Subtract(vec *Vec2)
- type Vec3
- func (me *Vec3) Add(vec *Vec3)
- func (me *Vec3) Add1(val float64)
- func (me *Vec3) Add3(x, y, z float64)
- func (me *Vec3) Added(vec *Vec3) *Vec3
- func (me *Vec3) AllEq(val float64) bool
- func (me *Vec3) AllGEq(vec *Vec3) bool
- func (me *Vec3) AllIn(min, max *Vec3) bool
- func (me *Vec3) AllLEq(vec *Vec3) bool
- func (me *Vec3) AngleDeg(to *Vec3) float64
- func (me *Vec3) AngleRad(to *Vec3) float64
- func (me *Vec3) Clamp(min, max *Vec3)
- func (me *Vec3) Clamp01()
- func (me *Vec3) ClampMagnitude(maxLength float64) *Vec3
- func (me *Vec3) Clear()
- func (me *Vec3) Cross(vec *Vec3) *Vec3
- func (me *Vec3) CrossNormalized(vec *Vec3) (r *Vec3)
- func (me *Vec3) Distance(vec *Vec3) float64
- func (me *Vec3) DistanceManhattan(vec *Vec3) float64
- func (me *Vec3) Div(vec *Vec3) *Vec3
- func (me *Vec3) Divide(d float64)
- func (me *Vec3) Divided(d float64) *Vec3
- func (me *Vec3) Dot(vec *Vec3) float64
- func (me *Vec3) DotSub(vec1, vec2 *Vec3) float64
- func (me *Vec3) Eq(vec *Vec3) bool
- func (me *Vec3) Length() float64
- func (me *Vec3) Magnitude() float64
- func (me *Vec3) Max() float64
- func (me *Vec3) MaxAbs() float64
- func (me *Vec3) Min() float64
- func (me *Vec3) Mult(vec *Vec3) *Vec3
- func (me *Vec3) Mult3(x, y, z float64) *Vec3
- func (me *Vec3) Negate()
- func (me *Vec3) Negated() *Vec3
- func (me *Vec3) Normalize()
- func (me *Vec3) NormalizeSafe()
- func (me *Vec3) Normalized() *Vec3
- func (me *Vec3) NormalizedScaled(factor float64) (vec *Vec3)
- func (me *Vec3) Rcp() *Vec3
- func (me *Vec3) RotateDeg(angleDeg float64, axis *Vec3)
- func (me *Vec3) RotateRad(angleRad float64, axis *Vec3)
- func (me *Vec3) Scale(factor float64)
- func (me *Vec3) ScaleAdd(factor, add *Vec3)
- func (me *Vec3) Scaled(factor float64) *Vec3
- func (me *Vec3) ScaledAdded(factor float64, add *Vec3) *Vec3
- func (me *Vec3) Set(x, y, z float64)
- func (me *Vec3) SetFromAdd(vec1, vec2 *Vec3)
- func (me *Vec3) SetFromAddAdd(a, b, c *Vec3)
- func (me *Vec3) SetFromAddScaled(vec1, vec2 *Vec3, mul float64)
- func (me *Vec3) SetFromAddSub(a, b, c *Vec3)
- func (me *Vec3) SetFromCross(vec *Vec3)
- func (me *Vec3) SetFromCrossOf(one, two *Vec3)
- func (me *Vec3) SetFromDegToRad(deg *Vec3)
- func (me *Vec3) SetFromDivided(vec *Vec3, d float64)
- func (me *Vec3) SetFromMad(mul1, mul2, add *Vec3)
- func (me *Vec3) SetFromMult(v1, v2 *Vec3)
- func (me *Vec3) SetFromNegated(vec *Vec3)
- func (me *Vec3) SetFromNormalized(vec *Vec3)
- func (me *Vec3) SetFromRcp(vec *Vec3)
- func (me *Vec3) SetFromRotation(pos, rotCos, rotSin *Vec3)
- func (me *Vec3) SetFromScaled(vec *Vec3, mul float64)
- func (me *Vec3) SetFromScaledSub(vec1, vec2 *Vec3, mul float64)
- func (me *Vec3) SetFromSub(vec1, vec2 *Vec3)
- func (me *Vec3) SetFromSubAdd(a, b, c *Vec3)
- func (me *Vec3) SetFromSubMult(sub1, sub2, mul *Vec3)
- func (me *Vec3) SetFromSubScaled(v1, v2 *Vec3, v2Scale float64)
- func (me *Vec3) SetFromSubSub(a, b, c *Vec3)
- func (me *Vec3) SetToMax()
- func (me *Vec3) SetToMin()
- func (me *Vec3) Sign() *Vec3
- func (me *Vec3) String() string
- func (me *Vec3) Sub(vec *Vec3) *Vec3
- func (me *Vec3) SubDivMult(sub, div, mul *Vec3) *Vec3
- func (me *Vec3) SubFloorDivMult(div, mul float64) *Vec3
- func (me *Vec3) SubFrom(val float64) *Vec3
- func (me *Vec3) SubScaled(vec *Vec3, val float64) *Vec3
- func (me *Vec3) Subtract(vec *Vec3)
- func (me *Vec3) TransformCoord(mat *Mat4)
- func (me *Vec3) TransformNormal(mat *Mat4, absMat bool)
- type Vec4
- func (me *Vec4) AddedDiv(a *Vec4, d float64) *Vec4
- func (me *Vec4) Clear()
- func (me *Vec4) Clone() (q *Vec4)
- func (me *Vec4) Conjugate()
- func (me *Vec4) Conjugated() (v *Vec4)
- func (me *Vec4) Distance(vec *Vec4) float64
- func (me *Vec4) Divide(d float64)
- func (me *Vec4) Divided(d float64) *Vec4
- func (me *Vec4) Dot(vec *Vec4) float64
- func (me *Vec4) Eq(vec *Vec4) bool
- func (me *Vec4) Length() float64
- func (me *Vec4) Magnitude() float64
- func (me *Vec4) MoveTowards(target *Vec4, maxDistanceDelta float64) *Vec4
- func (me *Vec4) MultMat4(mat *Mat4)
- func (me *Vec4) MultMat4Vec3(mat *Mat4, vec *Vec3)
- func (me *Vec4) MultMat4Vec4(mat *Mat4, vec *Vec4)
- func (me *Vec4) Negate()
- func (me *Vec4) Negated() *Vec4
- func (me *Vec4) Normalize()
- func (me *Vec4) NormalizeFrom(magnitude float64)
- func (me *Vec4) Normalized() *Vec4
- func (me *Vec4) Project(vec *Vec4)
- func (me *Vec4) Projected(vec *Vec4) *Vec4
- func (me *Vec4) Scale(v float64)
- func (me *Vec4) Scaled(v float64) *Vec4
- func (me *Vec4) SetFromConjugated(c *Vec4)
- func (me *Vec4) SetFromMult(l, r *Vec4)
- func (me *Vec4) SetFromMult3(q *Vec4, v *Vec3)
- func (me *Vec4) SetFromVec3(vec *Vec3)
- func (me *Vec4) String() string
- func (me *Vec4) Sub(vec *Vec4) *Vec4
- func (me *Vec4) Subtract(vec *Vec4)
Constants ¶
const ( Deg2Rad = math.Pi / 180 Rad2Deg = 180 / math.Pi )
Variables ¶
Functions ¶
func ClosestPowerOfTwo ¶
Returns `v` if it is a power-of-two, or else the closest power-of-two.
func DeltaAngle ¶
Calculates the shortest difference between two given angles.
func Lerp ¶
Returns `a` if `t` is 0, or `b` if `t` is 1, or else the linear interpolation from `a` to `b` according to `t`.
func LerpAngle ¶
Same as Lerp but makes sure the values interpolate correctly when they wrap around 360 degrees.
func Mat3Identities ¶
func Mat3Identities(mats ...*Mat3)
Calls the `Identity` method on all specified `mats`.
func Mat4Identities ¶
func Mat4Identities(mats ...*Mat4)
Calls the `Identity` method on all specified `mats`.
func NextPowerOfTwo ¶
Returns `v` if it is a power-of-two, or else the next-highest power-of-two.
func Round ¶
Returns the next-higher integer if fraction>0.5; if fraction<0.5 returns the next-lower integer; if fraction==0.5, returns the next even integer.
func SmoothStep ¶
Interpolates between `from` and `to` with smoothing at the limits.
func SmootherStep ¶
Interpolates between `from` and `to` with smoother smoothing at the limits.
Types ¶
type Mat3 ¶
type Mat3 [9]float64
Represents a 3x3 matrix.
var ( // The 3x3 identity matrix. Mat3Identity Mat3 )
type Mat4 ¶
type Mat4 [16]float64
Represents a 4x4 column-major matrix.
var ( // The 4x4 identity matrix. Mat4Identity Mat4 )
func NewMat4Add ¶
Returns a new `*Mat4` representing the result of adding `a` to `b`.
func NewMat4Frustum ¶
Returns a new `*Mat4` representing the specified frustum.
func NewMat4Identity ¶
func NewMat4Identity() (mat *Mat4)
Returns a new `*Mat4` representing the identity matrix.
func NewMat4Lookat ¶
Returns a new `*Mat4` representing the "look-at matrix" computed from the specified vectors.
func NewMat4Mult1 ¶
Returns a new `*Mat4` representing the result of multiplying all values in `m` with `v`.
func NewMat4Mult4 ¶
Returns a new `*Mat4` that represents the result of multiplying `one` with `two`.
func NewMat4MultN ¶
Returns a new `*Mat4` that represents the result of multiplying all specified `mats` with one another.
func NewMat4Orient ¶
Returns a new `*Mat4` representing the "orientation matrix" computed from the specified vectors.
func NewMat4Perspective ¶
Returns a new `*Mat4` that represents the specified perspective-projection matrix.
func NewMat4RotationX ¶
Returns a new `*Mat4` that represents a rotation of `rad` radians around the X axis.
func NewMat4RotationY ¶
Returns a new `*Mat4` that represents a rotation of `rad` radians around the Y axis.
func NewMat4RotationZ ¶
Returns a new `*Mat4` that represents a rotation of `rad` radians around the Z axis.
func NewMat4Scaling ¶
Returns a new `*Mat4` that represents a transformation of "scale by `vec`".
func NewMat4Sub ¶
Returns a new `*Mat4` that represents `a` minus `b`.
func NewMat4Translation ¶
Returns a new `*Mat4` that represents a transformation of "translate by `vec`".
func (*Mat4) Abs ¶
Returns a new `*Mat4` with each cell representing the `math.Abs` value of the respective corresponding cell in `me`.
func (*Mat4) Perspective ¶
Sets `me` to the specified perspective-projection matrix.
`fovYRad` -- vertical field-of-view angle in radians. `a` -- aspect ratio. `n` -- near-plane. `f` -- far-plane.
func (*Mat4) RotationX ¶
Sets `me` to a rotation matrix representing "rotate `rad` radians around the X axis".
func (*Mat4) RotationY ¶
Sets `me` to a rotation matrix representing "rotate `rad` radians around the Y axis".
func (*Mat4) RotationZ ¶
Sets `me` to a rotation matrix representing "rotate `rad` radians around the Z axis".
func (*Mat4) SetFromMult4 ¶
Sets `me` to the result of multiplying `one` times `two`.
func (*Mat4) SetFromMultN ¶
Sets `me` to the result of multiplying all the specified `mats` with one another.
func (*Mat4) SetFromTransposeOf ¶
Sets `me` to the transpose of `mat`.
func (*Mat4) Translation ¶
Sets `me` to a transformation matrix representing "translate by `vec`"
type Quat ¶
type Quat struct { // X, Y, Z, W Vec4 }
Quaternion
func Quat_Identity ¶
func Quat_Identity() (q Quat)
type Vec2 ¶
type Vec2 struct{ X, Y float64 }
A 2-dimensional vector.
func (*Vec2) ClampMagnitude ¶
func (*Vec2) Div ¶
Returns a new `*Vec2` that is the result of dividing `me` by `vec` without checking for division-by-0.
func (*Vec2) DivSafe ¶
Returns a new `*Vec2` that is the result of dividing `me` by `vec`, safely checking for division-by-0.
func (*Vec2) MoveTowards ¶
func (*Vec2) Normalize ¶
func (me *Vec2) Normalize()
Normalizes `me` in-place without checking for division-by-0.
func (*Vec2) NormalizeSafe ¶
func (me *Vec2) NormalizeSafe()
Normalizes `me` in-place, safely checking for division-by-0.
func (*Vec2) Normalized ¶
Returns a new `*Vec2` that is the normalized representation of `me` without checking for division-by-0.
func (*Vec2) NormalizedSafe ¶
Returns a new `*Vec2` that is the normalized representation of `me`, safely checking for division-by-0.
func (*Vec2) NormalizedScaled ¶
Returns a new `*Vec2` that is the normalized representation of `me` scaled by `factor` without checking for division-by-0.
func (*Vec2) NormalizedScaledSafe ¶
Returns a new `*Vec2` that is the normalized representation of `me` scaled by `factor`, safely checking for division-by-0.
type Vec3 ¶
type Vec3 struct {
X, Y, Z float64
}
Represents a 3-dimensional vector.
func Vec3_Right ¶
func Vec3_Right() Vec3
func (*Vec3) AllEq ¶
Returns whether all 3 components in `me` are approximately equivalent to their respective counterparts in `val`.
func (*Vec3) AllGEq ¶
Returns whether all 3 components in `me` are greater than (or approximately equivalent to) their respective component counterparts in `vec`.
func (*Vec3) AllIn ¶
Returns whether all 3 components in `me` are greater than `min`, and also less than `max`.
func (*Vec3) AllLEq ¶
Returns whether all 3 components in `me` are less than (or approximately equivalent to) their respective component counterparts in `vec`.
func (*Vec3) Clamp ¶
Clamps each component in `me` between the respective corresponding counter-part component in `min` and `max`.
func (*Vec3) ClampMagnitude ¶
func (*Vec3) CrossNormalized ¶
Returns a new `*Vec` that represents the cross-product of `me` and `vec`, normalized.
func (*Vec3) DistanceManhattan ¶
Returns the "manhattan distance" of `me` from `vec`.
func (*Vec3) Divided ¶
Returns a new `*Vec3` that represents all 3 components in `me`, each divided by `val`.
func (*Vec3) Mult3 ¶
Returns a new `*Vec3` with each component in `me` multiplied by the respective corresponding specified factor.
func (*Vec3) Negate ¶
func (me *Vec3) Negate()
Reverses the signs of all 3 vector components in `me`.
func (*Vec3) Negated ¶
Returns a new `*Vec` with each component representing the negative (sign inverted) corresponding component in `me`.
func (*Vec3) Normalize ¶
func (me *Vec3) Normalize()
Normalizes `me` in-place without checking for division-by-0.
func (*Vec3) NormalizeSafe ¶
func (me *Vec3) NormalizeSafe()
Normalizes `me` in-place, safely checking for division-by-0.
func (*Vec3) Normalized ¶
Returns a new `*Vec3` that represents `me`, normalized.
func (*Vec3) NormalizedScaled ¶
Returns a new `*Vec3` that represents `me` normalized, then scaled by `factor`.
func (*Vec3) ScaledAdded ¶
Returns a new `*Vec3` that represents `me` scaled by `factor`, then `add` added.
func (*Vec3) Set ¶
Sets all 3 vector components in `me` to the corresponding respective specified value.
func (*Vec3) SetFromAdd ¶
Sets `me` to the result of adding `vec1` and `vec2`.
func (*Vec3) SetFromAddScaled ¶
`me = mul * vec2 + vec1`
func (*Vec3) SetFromCross ¶
Sets `me` to the cross-product of `me` and `vec`.
func (*Vec3) SetFromCrossOf ¶
Sets `me` to the cross-product of `one` and `two`.
func (*Vec3) SetFromDegToRad ¶
Sets each vector component in `me` to the radian equivalent of the degree angle stored in the respective corresponding component of `vec`.
func (*Vec3) SetFromDivided ¶
func (*Vec3) SetFromMad ¶
`me = mul1 * mul2 + add`
func (*Vec3) SetFromNormalized ¶
Sets `me` to `vec` normalized.
func (*Vec3) SetFromRotation ¶
Sets `me` to `pos` rotated as expressed in `rotCos` and `rotSin`.
func (*Vec3) SetFromScaledSub ¶
`me = (vec1 - vec2) * mul`
func (*Vec3) SetFromSubMult ¶
`me = (sub1 - sub2) * mul`
func (*Vec3) SetFromSubScaled ¶
`me = v1 - v2 * v2Scale`
func (*Vec3) SetToMax ¶
func (me *Vec3) SetToMax()
Sets all 3 vector components in `me` to `math.MaxFloat64`.
func (*Vec3) SetToMin ¶
func (me *Vec3) SetToMin()
Sets all 3 vector components in `me` to `-math.MaxFloat64`.
func (*Vec3) Sign ¶
Returns a new `*Vec3` with each vector component indicating the sign (-1, 1 or 0) of the respective corresponding component in `me`.
func (*Vec3) SubDivMult ¶
Returns a new `*Vec3` that represents `((me - sub) / div) * mul`.
func (*Vec3) SubFloorDivMult ¶
Returns a new `*Vec3` that represents `mul * math.Floor(me / div)`.
func (*Vec3) TransformCoord ¶
Transform coordinate vector `me` according to the specified `*Mat4`.
func (*Vec3) TransformNormal ¶
Transform normal vector `me` according to the specified `*Mat4`.
type Vec4 ¶
type Vec4 struct {
X, Y, Z, W float64
}
Represents an arbitrary 4-dimensional vector.
func (*Vec4) Conjugate ¶
func (me *Vec4) Conjugate()
Negates the `X`, `Y`, `Z` components in `me`, but not `W`.
func (*Vec4) Conjugated ¶
Returns a new `*Vec4` that represents `me` conjugated.
func (*Vec4) MoveTowards ¶
func (*Vec4) MultMat4Vec3 ¶
Sets `me` to the result of multiplying the specified `*Mat4` with the specified `*Vec3`.
func (*Vec4) MultMat4Vec4 ¶
Sets `me` to the result of multiplying the specified `*Mat4` with the specified `*Vec4`.
func (*Vec4) NormalizeFrom ¶
Normalizes `me` according to the specified `magnitude`.
func (*Vec4) Normalized ¶
Returns a new `*Vec4` that represents `me` normalized according to `me.Magnitude`.
func (*Vec4) SetFromConjugated ¶
Sets `me` to `c` conjugated.
func (*Vec4) SetFromMult ¶
Applies various 4D vector component computations of `l` and `r` to `me`, as needed by the `Vec3.RotateRad` method.
func (*Vec4) SetFromMult3 ¶
Applies various 4D vector component computations of `q` and `v` to `me`, as needed by the `Vec3.RotateRad` method.