sprec

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2025 License: MIT Imports: 2 Imported by: 10

Documentation

Overview

Package sprec represents a mathematics library for working with single precision types.

Index

Constants

View Source
const (
	// RotationOrderLocalXYZ specifies that rotations are applied in the order
	// of X, Y, Z using a local gizmo (i.e. from the point of view of the object).
	RotationOrderLocalXYZ = RotationOrderGlobalZYX

	// RotationOrderLocalXZY specifies that rotations are applied in the order
	// of X, Z, Y using a local gizmo (i.e. from the point of view of the object).
	RotationOrderLocalXZY = RotationOrderGlobalYZX

	// RotationOrderLocalYXZ specifies that rotations are applied in the order
	// of Y, X, Z using a local gizmo (i.e. from the point of view of the object).
	RotationOrderLocalYXZ = RotationOrderGlobalZXY

	// RotationOrderLocalYZX specifies that rotations are applied in the order
	// of Y, Z, X using a local gizmo (i.e. from the point of view of the object).
	RotationOrderLocalYZX = RotationOrderGlobalXZY

	// RotationOrderLocalZXY specifies that rotations are applied in the order
	// of Z, X, Y using a local gizmo (i.e. from the point of view of the object).
	RotationOrderLocalZXY = RotationOrderGlobalYXZ

	// RotationOrderLocalZYX specifies that rotations are applied in the order
	// of Z, Y, X using a local gizmo (i.e. from the point of view of the object).
	RotationOrderLocalZYX = RotationOrderGlobalXYZ
)
View Source
const (
	Pi      = float32(math.Pi)
	Tau     = float32(math.Pi * 2.0)
	Epsilon = float32(0.000001)
)

Variables

This section is empty.

Functions

func Abs

func Abs[T ~float32](value T) T

func Ceil added in v0.9.0

func Ceil[T ~float32](value T) T

func Clamp

func Clamp[T ~float32](value, lower, upper T) T

func Cos

func Cos(angle Angle) float32

func Eq

func Eq(a, b float32) bool

func EqEps

func EqEps(a, b, epsilon float32) bool

func Floor added in v0.9.0

func Floor[T ~float32](value T) T

func IsNegative added in v0.9.0

func IsNegative[T ~float32](value T) bool

func IsValid added in v0.9.0

func IsValid[T ~float32](value T) bool

func Max

func Max[T ~float32](a, b T) T

func Min

func Min[T ~float32](a, b T) T

func Mix added in v0.2.0

func Mix[T ~float32](a, b T, amount float32) T

func Mod added in v0.9.0

func Mod(a, b float32) float32

func MoveTowards added in v0.15.0

func MoveTowards[T ~float32](current, target, maxDelta T) T

func Pow added in v0.9.0

func Pow(a, b float32) float32

func QuatDot added in v0.4.0

func QuatDot(a, b Quat) float32

func Sign

func Sign(value float32) float32

func Sin

func Sin(angle Angle) float32

func Smoothstep added in v0.14.0

func Smoothstep[T ~float32](lowerEdge, upperEdge, value T) T

func Sqr added in v0.12.0

func Sqr[T ~float32](value T) T

func Sqrt

func Sqrt(value float32) float32

func Step added in v0.14.0

func Step[T ~float32](edge, value T) T

func Sum added in v0.12.0

func Sum[T ~float32](values ...T) T

func Tan

func Tan(angle Angle) float32

func Vec2Cross added in v0.13.0

func Vec2Cross(a, b Vec2) float32

func Vec2Dot

func Vec2Dot(a, b Vec2) float32

func Vec3Dot

func Vec3Dot(a, b Vec3) float32

func Vec4Dot added in v0.3.0

func Vec4Dot(a, b Vec4) float32

Types

type Angle

type Angle float32

Angle represents an angle.

func Acos added in v0.4.0

func Acos(cs float32) Angle

func Asin added in v0.4.0

func Asin(sn float32) Angle

func Atan2 added in v0.9.0

func Atan2(y, x float32) Angle

func Degrees

func Degrees(degrees float32) Angle

Degrees creates an angle from degrees.

func NormalizeAngle added in v0.12.0

func NormalizeAngle(a Angle) Angle

NormalizeAngle normalizes an angle to the range [-Pi..Pi].

func NormalizeAngleNeg added in v0.14.0

func NormalizeAngleNeg(a Angle) Angle

NormalizeAngleNeg normalizes an angle to the range [-Tau..0.0].

func NormalizeAnglePos added in v0.14.0

func NormalizeAnglePos(a Angle) Angle

NormalizeAnglePos normalizes an angle to the range [0.0..Tau].

func Radians

func Radians(radians float32) Angle

Radians creates an angle from radians.

func Vec3Angle added in v0.10.0

func Vec3Angle(a, b Vec3) Angle

Vec3Angle returns the shortest angle between two vectors. It always returns a positive angle.

func Vec3ProjectionAngle added in v0.10.0

func Vec3ProjectionAngle(a, b, normal Vec3) Angle

Vec3ProjectionAngle returns the angle between two vectors projected onto a plane defined by a normal vector. Unlike Vec3Angle, this function returns a signed angle and the ordering of the vectors matters.

func (Angle) Degrees

func (a Angle) Degrees() float32

Degrees returns the angle in degrees.

func (Angle) IsInf added in v0.7.0

func (a Angle) IsInf() bool

IsInf returns true if the angle is an Inf value.

func (Angle) IsNaN added in v0.7.0

func (a Angle) IsNaN() bool

IsNaN returns true if the angle is a NaN value.

func (Angle) Radians

func (a Angle) Radians() float32

Radians returns the angle in radians.

type Mat3

type Mat3 struct {
	M11, M12, M13 float32
	M21, M22, M23 float32
	M31, M32, M33 float32
}

func ColumnMajorArrayToMat3 added in v0.3.0

func ColumnMajorArrayToMat3(values [9]float32) Mat3

func FastInverseMat3

func FastInverseMat3(m Mat3) Mat3

FastInverseMat3 calculates the inverse of the matrix with a few caveats.

The matrix should be a transformation one that was constructed through the multiplication of one or more of the following transformations: identity, translation, rotation.

For all other scenarios (e.g. a scale transformation was used), the InverseMat3 method should be used instead, though it will be slower.

func IdentityMat3

func IdentityMat3() Mat3

func InverseMat3

func InverseMat3(m Mat3) Mat3

InverseMat3 calculates the inverse of the matrix.

The behavior is undefined if the matrix is not reversible (i.e. has a zero determinant).

func Mat3MultiProd

func Mat3MultiProd(first Mat3, others ...Mat3) Mat3

func Mat3Prod

func Mat3Prod(left, right Mat3) Mat3

func NewMat3

func NewMat3(
	m11, m12, m13 float32,
	m21, m22, m23 float32,
	m31, m32, m33 float32,
) Mat3

func OrthoMat3

func OrthoMat3(left, right, top, bottom float32) Mat3

func RotationMat3

func RotationMat3(angle Angle) Mat3

func RowMajorArrayToMat3 added in v0.3.0

func RowMajorArrayToMat3(values [9]float32) Mat3

func ScaleMat3

func ScaleMat3(x, y float32) Mat3

func TransformationMat3

func TransformationMat3(orientX, orientY, translation Vec2) Mat3

func TranslationMat3

func TranslationMat3(x, y float32) Mat3

func TransposedMat3 added in v0.5.0

func TransposedMat3(m Mat3) Mat3

func ZeroMat3

func ZeroMat3() Mat3

func (Mat3) Column1 added in v0.3.0

func (m Mat3) Column1() Vec3

func (Mat3) Column2 added in v0.3.0

func (m Mat3) Column2() Vec3

func (Mat3) Column3 added in v0.3.0

func (m Mat3) Column3() Vec3

func (Mat3) ColumnMajorArray added in v0.3.0

func (m Mat3) ColumnMajorArray() [9]float32

func (Mat3) GoString

func (m Mat3) GoString() string

func (Mat3) IsInf added in v0.7.0

func (m Mat3) IsInf() bool

func (Mat3) IsNaN added in v0.7.0

func (m Mat3) IsNaN() bool

func (Mat3) OrientationX

func (m Mat3) OrientationX() Vec2

func (Mat3) OrientationY

func (m Mat3) OrientationY() Vec2

func (Mat3) Row1 added in v0.3.0

func (m Mat3) Row1() Vec3

func (Mat3) Row2 added in v0.3.0

func (m Mat3) Row2() Vec3

func (Mat3) Row3 added in v0.3.0

func (m Mat3) Row3() Vec3

func (Mat3) RowMajorArray added in v0.3.0

func (m Mat3) RowMajorArray() [9]float32

func (Mat3) Translation

func (m Mat3) Translation() Vec2

type Mat4

type Mat4 struct {
	M11, M12, M13, M14 float32
	M21, M22, M23, M24 float32
	M31, M32, M33, M34 float32
	M41, M42, M43, M44 float32
}

func ColumnMajorArrayToMat4 added in v0.3.0

func ColumnMajorArrayToMat4(values [16]float32) Mat4

func FastInverseMat4

func FastInverseMat4(m Mat4) Mat4

FastInverseMat4 calculates the inverse of the matrix with a few caveats.

The matrix should be a transformation one that was constructed through the multiplication of one or more of the following transformations: identity, translation, rotation.

For all other scenarios (e.g. a scale transformation was used), the InverseMat4 method should be used instead, though it will be slower.

func IdentityMat4

func IdentityMat4() Mat4

func InverseMat4

func InverseMat4(m Mat4) Mat4

InverseMat4 calculates the inverse of the matrix.

The behavior is undefined if the matrix is not reversible (i.e. has a zero determinant).

func Mat4MultiProd

func Mat4MultiProd(first Mat4, others ...Mat4) Mat4

func Mat4Prod

func Mat4Prod(left, right Mat4) Mat4

func NewMat4

func NewMat4(
	m11, m12, m13, m14 float32,
	m21, m22, m23, m24 float32,
	m31, m32, m33, m34 float32,
	m41, m42, m43, m44 float32,
) Mat4

func OrientationMat4

func OrientationMat4(orientX, orientY, orientZ Vec3) Mat4

func OrthoMat4

func OrthoMat4(left, right, top, bottom, near, far float32) Mat4

func PerspectiveMat4

func PerspectiveMat4(left, right, bottom, top, near, far float32) Mat4

func RotationMat4

func RotationMat4(angle Angle, x, y, z float32) Mat4

func RowMajorArrayToMat4 added in v0.3.0

func RowMajorArrayToMat4(values [16]float32) Mat4

func ScaleMat4

func ScaleMat4(x, y, z float32) Mat4

func TRSMat4 added in v0.3.0

func TRSMat4(translation Vec3, rotation Quat, scale Vec3) Mat4

func TransformationMat4

func TransformationMat4(orientX, orientY, orientZ, translation Vec3) Mat4

func TranslationMat4

func TranslationMat4(x, y, z float32) Mat4

func TransposedMat4 added in v0.5.0

func TransposedMat4(m Mat4) Mat4

func ZeroMat4

func ZeroMat4() Mat4

func (Mat4) Column1 added in v0.3.0

func (m Mat4) Column1() Vec4

func (Mat4) Column2 added in v0.3.0

func (m Mat4) Column2() Vec4

func (Mat4) Column3 added in v0.3.0

func (m Mat4) Column3() Vec4

func (Mat4) Column4 added in v0.3.0

func (m Mat4) Column4() Vec4

func (Mat4) ColumnMajorArray

func (m Mat4) ColumnMajorArray() [16]float32

func (Mat4) GoString

func (m Mat4) GoString() string

func (Mat4) IsInf added in v0.7.0

func (m Mat4) IsInf() bool

func (Mat4) IsNaN added in v0.7.0

func (m Mat4) IsNaN() bool

func (Mat4) OrientationX

func (m Mat4) OrientationX() Vec3

func (Mat4) OrientationY

func (m Mat4) OrientationY() Vec3

func (Mat4) OrientationZ

func (m Mat4) OrientationZ() Vec3

func (Mat4) Rotation added in v0.5.0

func (m Mat4) Rotation() Quat

Rotation returns the rotation that is represented by this matrix. NOTE: This function assumes that the matrix has identity scale. If you want to get the rotation of a matrix that has non-identity scale, consider using the TRS method.

func (Mat4) Row1 added in v0.3.0

func (m Mat4) Row1() Vec4

func (Mat4) Row2 added in v0.3.0

func (m Mat4) Row2() Vec4

func (Mat4) Row3 added in v0.3.0

func (m Mat4) Row3() Vec4

func (Mat4) Row4 added in v0.3.0

func (m Mat4) Row4() Vec4

func (Mat4) RowMajorArray

func (m Mat4) RowMajorArray() [16]float32

func (Mat4) Scale added in v0.2.0

func (m Mat4) Scale() Vec3

func (Mat4) TRS added in v0.5.0

func (m Mat4) TRS() (Vec3, Quat, Vec3)

func (Mat4) Translation

func (m Mat4) Translation() Vec3

type Quat

type Quat struct {
	W float32
	X float32
	Y float32
	Z float32
}

func ConjugateQuat

func ConjugateQuat(q Quat) Quat

func EulerQuat added in v0.10.0

func EulerQuat(x, y, z Angle, order RotationOrder) Quat

func IdentityQuat

func IdentityQuat() Quat

func InverseQuat

func InverseQuat(q Quat) Quat

func NegativeQuat added in v0.4.0

func NegativeQuat(q Quat) Quat

func NewQuat

func NewQuat(w, x, y, z float32) Quat

func QuatDiff added in v0.4.0

func QuatDiff(second, first Quat, shortest bool) Quat

func QuatLerp added in v0.4.0

func QuatLerp(first, second Quat, t float32) Quat

func QuatPow added in v0.4.0

func QuatPow(q Quat, pow float32) Quat

func QuatProd

func QuatProd(first, second Quat) Quat

func QuatScalarProd

func QuatScalarProd(q Quat, value float32) Quat

func QuatScalarQuot

func QuatScalarQuot(q Quat, value float32) Quat

func QuatSlerp added in v0.4.0

func QuatSlerp(first, second Quat, t float32) Quat

func RotationQuat

func RotationQuat(angle Angle, direction Vec3) Quat

func UnitQuat

func UnitQuat(q Quat) Quat

func (Quat) EulerAngles added in v0.10.0

func (q Quat) EulerAngles(order RotationOrder) (x Angle, y Angle, z Angle)

EulerAngles returns the Euler rotation angles for the given quaternion and rotation order in which it was presumably created.

The rotations are always returned for X, Y, Z axis in that order.

NOTE: This assumes that the quaternion is normalized.

func (Quat) GoString

func (q Quat) GoString() string

func (Quat) IsIdentity added in v0.7.0

func (q Quat) IsIdentity() bool

func (Quat) IsInf added in v0.7.0

func (q Quat) IsInf() bool

func (Quat) IsNaN added in v0.7.0

func (q Quat) IsNaN() bool

func (Quat) Norm

func (q Quat) Norm() float32

func (Quat) OrientationX

func (q Quat) OrientationX() Vec3

func (Quat) OrientationY

func (q Quat) OrientationY() Vec3

func (Quat) OrientationZ

func (q Quat) OrientationZ() Vec3

func (Quat) SqrNorm

func (q Quat) SqrNorm() float32

type RotationOrder added in v0.10.0

type RotationOrder uint8

RotationOrder specifies the order in which rotations are applied.

const (
	// RotationOrderGlobalXYZ specifies that rotations are applied in the order
	// of X, Y, Z using a global gizmo.
	RotationOrderGlobalXYZ RotationOrder = iota

	// RotationOrderGlobalXZY specifies that rotations are applied in the order
	// of X, Z, Y using a global gizmo.
	RotationOrderGlobalXZY

	// RotationOrderGlobalYXZ specifies that rotations are applied in the order
	// of Y, X, Z using a global gizmo.
	RotationOrderGlobalYXZ

	// RotationOrderGlobalYZX specifies that rotations are applied in the order
	// of Y, Z, X using a global gizmo.
	RotationOrderGlobalYZX

	// RotationOrderGlobalZXY specifies that rotations are applied in the order
	// of Z, X, Y using a global gizmo.
	RotationOrderGlobalZXY

	// RotationOrderGlobalZYX specifies that rotations are applied in the order
	// of Z, Y, X using a global gizmo.
	RotationOrderGlobalZYX
)

type Vec2

type Vec2 struct {
	X float32
	Y float32
}

func AngleVec2Rotation added in v0.15.0

func AngleVec2Rotation(angle Angle, v Vec2) Vec2

AngleVec2Rotation rotates a Vec2 by the given angle.

func ArrayToVec2 added in v0.3.0

func ArrayToVec2(array [2]float32) Vec2

func BasisXVec2

func BasisXVec2() Vec2

func BasisYVec2

func BasisYVec2() Vec2

func InverseVec2

func InverseVec2(vector Vec2) Vec2

func Mat3Vec2Transformation added in v0.12.0

func Mat3Vec2Transformation(mat Mat3, vec Vec2) Vec2

func NewVec2

func NewVec2(x, y float32) Vec2

func NormalVec2 added in v0.7.0

func NormalVec2(vector Vec2) Vec2

func ResizedVec2

func ResizedVec2(vector Vec2, newLength float32) Vec2

func UnitVec2

func UnitVec2(vector Vec2) Vec2

func Vec2Diff

func Vec2Diff(a, b Vec2) Vec2

func Vec2Lerp added in v0.4.0

func Vec2Lerp(a, b Vec2, t float32) Vec2

func Vec2MultiDiff added in v0.7.0

func Vec2MultiDiff(first Vec2, others ...Vec2) Vec2

func Vec2MultiSum added in v0.7.0

func Vec2MultiSum(first Vec2, others ...Vec2) Vec2

func Vec2Prod

func Vec2Prod(vector Vec2, value float32) Vec2

func Vec2Quot

func Vec2Quot(vector Vec2, value float32) Vec2

func Vec2Sum

func Vec2Sum(a, b Vec2) Vec2

func ZeroVec2

func ZeroVec2() Vec2

func (Vec2) GoString

func (v Vec2) GoString() string

func (Vec2) IsInf added in v0.7.0

func (v Vec2) IsInf() bool

func (Vec2) IsNaN added in v0.7.0

func (v Vec2) IsNaN() bool

func (Vec2) IsZero

func (v Vec2) IsZero() bool

func (Vec2) Length

func (v Vec2) Length() float32

func (Vec2) SqrLength

func (v Vec2) SqrLength() float32

type Vec3

type Vec3 struct {
	X float32
	Y float32
	Z float32
}

func ArrayToVec3 added in v0.3.0

func ArrayToVec3(array [3]float32) Vec3

func BasisXVec3

func BasisXVec3() Vec3

func BasisYVec3

func BasisYVec3() Vec3

func BasisZVec3

func BasisZVec3() Vec3

func InverseVec3

func InverseVec3(vector Vec3) Vec3

func Mat3Vec3Prod

func Mat3Vec3Prod(mat Mat3, vec Vec3) Vec3

func Mat4Vec3Transformation

func Mat4Vec3Transformation(mat Mat4, vec Vec3) Vec3

func NewVec3

func NewVec3(x, y, z float32) Vec3

func NormalVec3 added in v0.7.0

func NormalVec3(vector Vec3) Vec3

func QuatVec3Rotation

func QuatVec3Rotation(q Quat, v Vec3) Vec3

func ResizedVec3

func ResizedVec3(vector Vec3, newLength float32) Vec3

func UnitVec3

func UnitVec3(vector Vec3) Vec3

func Vec3Cross

func Vec3Cross(a, b Vec3) Vec3

func Vec3Diff

func Vec3Diff(a, b Vec3) Vec3

func Vec3Lerp added in v0.4.0

func Vec3Lerp(a, b Vec3, t float32) Vec3

func Vec3MultiDiff added in v0.7.0

func Vec3MultiDiff(first Vec3, others ...Vec3) Vec3

func Vec3MultiSum added in v0.7.0

func Vec3MultiSum(first Vec3, others ...Vec3) Vec3

func Vec3Prod

func Vec3Prod(vector Vec3, value float32) Vec3

func Vec3Projection added in v0.10.0

func Vec3Projection(vector Vec3, normal Vec3) Vec3

Vec3Projection returns the specified vector flattened along the specified normal. The normal must be a unit vector. The result is the projection of the vector onto the plane defined by the normal.

func Vec3Quot

func Vec3Quot(vector Vec3, value float32) Vec3

func Vec3Sum

func Vec3Sum(a, b Vec3) Vec3

func ZeroVec3

func ZeroVec3() Vec3

func (Vec3) Array added in v0.2.0

func (v Vec3) Array() [3]float32

func (Vec3) GoString

func (v Vec3) GoString() string

func (Vec3) IsInf added in v0.7.0

func (v Vec3) IsInf() bool

func (Vec3) IsNaN added in v0.7.0

func (v Vec3) IsNaN() bool

func (Vec3) IsZero

func (v Vec3) IsZero() bool

func (Vec3) Length

func (v Vec3) Length() float32

func (Vec3) SqrLength

func (v Vec3) SqrLength() float32

type Vec4

type Vec4 struct {
	X float32
	Y float32
	Z float32
	W float32
}

func ArrayToVec4 added in v0.3.0

func ArrayToVec4(array [4]float32) Vec4

func InverseVec4

func InverseVec4(vector Vec4) Vec4

func Mat4Vec4Prod

func Mat4Vec4Prod(mat Mat4, vec Vec4) Vec4

func NewVec4

func NewVec4(x, y, z, w float32) Vec4

func Vec4Diff

func Vec4Diff(a, b Vec4) Vec4

func Vec4Lerp added in v0.4.0

func Vec4Lerp(a, b Vec4, t float32) Vec4

func Vec4MultiDiff added in v0.7.0

func Vec4MultiDiff(first Vec4, others ...Vec4) Vec4

func Vec4MultiSum added in v0.7.0

func Vec4MultiSum(first Vec4, others ...Vec4) Vec4

func Vec4Prod

func Vec4Prod(vector Vec4, value float32) Vec4

func Vec4Quot

func Vec4Quot(vector Vec4, value float32) Vec4

func Vec4Sum

func Vec4Sum(a, b Vec4) Vec4

func ZeroVec4

func ZeroVec4() Vec4

func (Vec4) Array added in v0.2.0

func (v Vec4) Array() [4]float32

func (Vec4) GoString

func (v Vec4) GoString() string

func (Vec4) IsInf added in v0.7.0

func (v Vec4) IsInf() bool

func (Vec4) IsNaN added in v0.7.0

func (v Vec4) IsNaN() bool

func (Vec4) IsZero

func (v Vec4) IsZero() bool

func (Vec4) VecXYZ

func (v Vec4) VecXYZ() Vec3

Jump to

Keyboard shortcuts

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