Documentation
¶
Overview ¶
Package math implements basic math functions which operate directly on float32 numbers without casting and contains types of common entities used in 3D Graphics such as vectors, matrices, quaternions and others.
Index ¶
- Constants
- Variables
- func Abs[T Float](x T) T
- func Acos[T Float](x T) T
- func ApproxEq[T Float](v1, v2, epsilon T) bool
- func ApproxLess[T Float](v1, v2, epsilon T) bool
- func Atan[T Float](x T) T
- func Atan2[T Float](y, x T) T
- func Barycoord[T Float](p, t1, t2, t3 Vec2[T]) [3]T
- func Ceil[T Float](x T) T
- func Clamp[T ~uint | ~uint8 | ~uint32 | ~uint64 | ~int | ~int32 | ~int64 | Float](n, min, max T) T
- func Cos[T Float](x T) T
- func DegToRad[T Float](deg T) T
- func FMA[T Float](x, y, z T) T
- func Floor[T Float](x T) T
- func Inf(sign int) float32
- func IsInsideTriangle[T Float](p, v1, v2, v3 Vec2[T]) bool
- func Lerp[T Float](from, to, t T) T
- func LerpC[T Float](from color.RGBA, to color.RGBA, t T) color.RGBA
- func LerpInt(from, to int, t float32) int
- func Log2[T Float](x T) T
- func Max[T Float](xs ...T) T
- func Min[T Float](xs ...T) T
- func Modf[T Float](f T) (T, T)
- func Pow[T Float](x, y T) T
- func RadToDeg[T Float](rad T) T
- func Round[T Float](x T) T
- func Sin[T Float](x T) T
- func Sqrt[T Float](x T) T
- func Tan[T Float](x T) T
- func TypeSize[T Type]() int
- type Float
- type Int26_6
- type Mat
- func (m Mat[T]) Add(n Mat[T]) Mat[T]
- func (m Mat[T]) Eq(n Mat[T]) bool
- func (m Mat[T]) Get(i, j int) T
- func (m Mat[T]) Index(i, j int) int
- func (m Mat[T]) Mul(n Mat[T]) Mat[T]
- func (m Mat[T]) Set(i, j int, v T)
- func (m Mat[T]) Sqrt() Mat[T]
- func (m Mat[T]) String() string
- func (m Mat[T]) Sub(n Mat[T]) Mat[T]
- func (m Mat[T]) T() Mat[T]
- type Mat2
- func (m Mat2[T]) Add(n Mat2[T]) Mat2[T]
- func (m Mat2[T]) Det() T
- func (m Mat2[T]) Eq(n Mat2[T]) bool
- func (m Mat2[T]) Get(i, j int) T
- func (m Mat2[T]) MulM(n Mat2[T]) Mat2[T]
- func (m Mat2[T]) MulV(v Vec2[T]) Vec2[T]
- func (m *Mat2[T]) Set(i, j int, v T)
- func (m Mat2[T]) String() string
- func (m Mat2[T]) Sub(n Mat2[T]) Mat2[T]
- func (m Mat2[T]) T() Mat2[T]
- type Mat3
- func (m Mat3[T]) Add(n Mat3[T]) Mat3[T]
- func (m Mat3[T]) Det() T
- func (m Mat3[T]) Eq(n Mat3[T]) bool
- func (m Mat3[T]) Get(i, j int) T
- func (m Mat3[T]) MulM(n Mat3[T]) Mat3[T]
- func (m Mat3[T]) MulV(v Vec3[T]) Vec3[T]
- func (m *Mat3[T]) Set(i, j int, v T)
- func (m Mat3[T]) String() string
- func (m Mat3[T]) Sub(n Mat3[T]) Mat3[T]
- func (m Mat3[T]) T() Mat3[T]
- type Mat4
- func (m Mat4[T]) Add(n Mat4[T]) Mat4[T]
- func (m Mat4[T]) Det() T
- func (m Mat4[T]) Eq(n Mat4[T]) bool
- func (m Mat4[T]) Get(i, j int) T
- func (m Mat4[T]) Inv() Mat4[T]
- func (m Mat4[T]) MulM(n Mat4[T]) Mat4[T]
- func (m Mat4[T]) MulV(v Vec4[T]) Vec4[T]
- func (m *Mat4[T]) Set(i, j int, v T)
- func (m Mat4[T]) String() string
- func (m Mat4[T]) Sub(n Mat4[T]) Mat4[T]
- func (m Mat4[T]) T() Mat4[T]
- type Point26_6
- type Quaternion
- type Rectangle26_6
- type TransformContext
- func (ctx *TransformContext[T]) ModelMatrix() Mat4[T]
- func (ctx *TransformContext[T]) ResetContext()
- func (ctx *TransformContext[T]) Rotate(dir Vec3[T], angle T)
- func (ctx *TransformContext[T]) RotateX(angle T)
- func (ctx *TransformContext[T]) RotateY(angle T)
- func (ctx *TransformContext[T]) RotateZ(angle T)
- func (ctx *TransformContext[T]) Scale(sx, sy, sz T)
- func (ctx *TransformContext[T]) ScaleX(sx T)
- func (ctx *TransformContext[T]) ScaleY(sy T)
- func (ctx *TransformContext[T]) ScaleZ(sz T)
- func (ctx *TransformContext[T]) Translate(tx, ty, tz T)
- func (ctx *TransformContext[T]) TranslateX(tx T)
- func (ctx *TransformContext[T]) TranslateY(ty T)
- func (ctx *TransformContext[T]) TranslateZ(tz T)
- type Type
- type Vec
- func (v *Vec[T]) Add(u Vec[T]) (r Vec[T])
- func (v Vec[T]) Dot(u Vec[T]) T
- func (v Vec[T]) Eq(u Vec[T]) bool
- func (v Vec[T]) IsZero() bool
- func (v Vec[T]) Len() T
- func (v Vec[T]) Scale(w T) (r Vec[T])
- func (m Vec[T]) String() string
- func (v Vec[T]) Sub(u Vec[T]) (r Vec[T])
- func (v Vec[T]) Translate(u Vec[T]) (r Vec[T])
- func (v Vec[T]) Unit() (r Vec[T])
- type Vec2
- func (v Vec2[T]) Add(u Vec2[T]) Vec2[T]
- func (v Vec2[T]) Apply(m Mat2[T]) Vec2[T]
- func (v Vec2[T]) Dot(u Vec2[T]) T
- func (v Vec2[T]) Eq(u Vec2[T]) bool
- func (v Vec2[T]) IsZero() bool
- func (v Vec2[T]) Len() T
- func (v Vec2[T]) Scale(x, y T) Vec2[T]
- func (m Vec2[T]) String() string
- func (v Vec2[T]) Sub(u Vec2[T]) Vec2[T]
- func (v Vec2[T]) Translate(x, y T) Vec2[T]
- func (v Vec2[T]) Unit() Vec2[T]
- type Vec3
- func (v Vec3[T]) Add(u Vec3[T]) Vec3[T]
- func (v Vec3[T]) Apply(m Mat3[T]) Vec3[T]
- func (v Vec3[T]) Cross(u Vec3[T]) Vec3[T]
- func (v Vec3[T]) Dot(u Vec3[T]) T
- func (v Vec3[T]) Eq(u Vec3[T]) bool
- func (v Vec3[T]) IsZero() bool
- func (v Vec3[T]) Len() T
- func (v Vec3[T]) Less(u Vec3[T]) bool
- func (v Vec3[T]) Scale(x, y, z T) Vec3[T]
- func (m Vec3[T]) String() string
- func (v Vec3[T]) Sub(u Vec3[T]) Vec3[T]
- func (v Vec3[T]) ToVec4(w T) Vec4[T]
- func (v Vec3[T]) Translate(x, y, z T) Vec3[T]
- func (v Vec3[T]) Unit() Vec3[T]
- type Vec4
- func (v *Vec4[T]) Add(u Vec4[T]) Vec4[T]
- func (v Vec4[T]) Apply(m Mat4[T]) Vec4[T]
- func (v Vec4[T]) Cross(u Vec4[T]) Vec4[T]
- func (v Vec4[T]) Dot(u Vec4[T]) T
- func (v Vec4[T]) Eq(u Vec4[T]) bool
- func (v Vec4[T]) IsZero() bool
- func (v Vec4[T]) Len() T
- func (v Vec4[T]) Less(u Vec4[T]) bool
- func (v Vec4[T]) Pos() Vec4[T]
- func (v Vec4[T]) Scale(x, y, z, w T) Vec4[T]
- func (m Vec4[T]) String() string
- func (v Vec4[T]) Sub(u Vec4[T]) Vec4[T]
- func (v Vec4[T]) ToVec2() Vec2[T]
- func (v Vec4[T]) ToVec3() Vec3[T]
- func (v Vec4[T]) Translate(x, y, z T) Vec4[T]
- func (v Vec4[T]) Unit() Vec4[T]
- func (v Vec4[T]) Vec() Vec4[T]
Constants ¶
const ( Pi = 3.14159265358979323846264338327950288419716939937510582097494459 HalfPi = Pi * 0.5 TwoPi = Pi * 2 MaxInt32 = math.MaxInt32 MaxInt64 = math.MaxInt64 MaxFloat32 = math.MaxFloat32 MaxFloat64 = math.MaxFloat64 MaxUint32 = math.MaxUint32 )
Equivalents to the standard math package.
const (
// Epsilon is a default epsilon value for computation.
Epsilon = 1e-7
)
Variables ¶
var (
IsNaN = math.IsNaN
)
Functions ¶
func Abs ¶
func Abs[T Float](x T) T
Abs returns the absolute value of x.
Special cases are:
Abs(±Inf) = +Inf Abs(NaN) = NaN
func Acos ¶
func Acos[T Float](x T) T
Acos returns the arccosine, in radians, of x.
Special case is:
Acos(x) = NaN if x < -1 or x > 1
func ApproxLess ¶
ApproxLess compares whether v1 is less than v2 (v1 < v2) approximately.
func Atan ¶
func Atan[T Float](x T) T
Atan returns the arctangent, in radians, of x.
Special cases are:
Atan(±0) = ±0 Atan(±Inf) = ±Pi/2
func Atan2 ¶
func Atan2[T Float](y, x T) T
Atan2 returns the arc tangent of y/x, using the signs of the two to determine the quadrant of the return value.
Special cases are (in order):
Atan2(y, NaN) = NaN Atan2(NaN, x) = NaN Atan2(+0, x>=0) = +0 Atan2(-0, x>=0) = -0 Atan2(+0, x<=-0) = +Pi Atan2(-0, x<=-0) = -Pi Atan2(y>0, 0) = +Pi/2 Atan2(y<0, 0) = -Pi/2 Atan2(+Inf, +Inf) = +Pi/4 Atan2(-Inf, +Inf) = -Pi/4 Atan2(+Inf, -Inf) = 3Pi/4 Atan2(-Inf, -Inf) = -3Pi/4 Atan2(y, +Inf) = 0 Atan2(y>0, -Inf) = +Pi Atan2(y<0, -Inf) = -Pi Atan2(+Inf, x) = +Pi/2 Atan2(-Inf, x) = -Pi/2
func Barycoord ¶
Barycoord computes the barycentric coordinates of a given position regards to the given three positions.
func Ceil ¶
func Ceil[T Float](x T) T
Ceil returns the least integer value greater than or equal to x.
Special cases are:
Ceil(±0) = ±0 Ceil(±Inf) = ±Inf Ceil(NaN) = NaN
func Cos ¶
func Cos[T Float](x T) T
Cos returns the cosine of the radian argument x.
Special cases are:
Cos(±Inf) = NaN Cos(NaN) = NaN
func FMA ¶
func FMA[T Float](x, y, z T) T
FMA returns x * y + z, computed with only one rounding. (That is, FMA returns the fused multiply-add of x, y, and z.)
func Floor ¶
func Floor[T Float](x T) T
Floor returns the greatest integer value less than or equal to x.
Special cases are:
Floor(±0) = ±0 Floor(±Inf) = ±Inf Floor(NaN) = NaN
func IsInsideTriangle ¶
IsInsideTriangle tests three given vertices and a position p, returns true if p is inside the three vertices, or false otherwise.
func Lerp ¶
func Lerp[T Float](from, to, t T) T
Lerp computes a linear interpolation between two given numbers regarding the given t parameter.
func LerpC ¶
LerpC computes a linear interpolation between two given colors regarding the given t parameter.
func LerpInt ¶
Lerp computes a linear interpolation between two given integers regarding the given t parameter.
func Log2 ¶
func Log2[T Float](x T) T
Log2 returns the binary logarithm of x. The special cases are the same as for Log.
func Modf ¶
func Modf[T Float](f T) (T, T)
Modf returns integer and fractional floating-point numbers that sum to f. Both values have the same sign as f.
Special cases are:
Modf(±Inf) = ±Inf, NaN Modf(NaN) = NaN, NaN
func Pow ¶
func Pow[T Float](x, y T) T
Pow returns x**y, the base-x exponential of y.
Special cases are (in order):
Pow(x, ±0) = 1 for any x Pow(1, y) = 1 for any y Pow(x, 1) = x for any x Pow(NaN, y) = NaN Pow(x, NaN) = NaN Pow(±0, y) = ±Inf for y an odd integer < 0 Pow(±0, -Inf) = +Inf Pow(±0, +Inf) = +0 Pow(±0, y) = +Inf for finite y < 0 and not an odd integer Pow(±0, y) = ±0 for y an odd integer > 0 Pow(±0, y) = +0 for finite y > 0 and not an odd integer Pow(-1, ±Inf) = 1 Pow(x, +Inf) = +Inf for |x| > 1 Pow(x, -Inf) = +0 for |x| > 1 Pow(x, +Inf) = +0 for |x| < 1 Pow(x, -Inf) = +Inf for |x| < 1 Pow(+Inf, y) = +Inf for y > 0 Pow(+Inf, y) = +0 for y < 0 Pow(-Inf, y) = Pow(-0, -y) Pow(x, y) = NaN for finite x < 0 and finite non-integer y
func Round ¶
func Round[T Float](x T) T
Round returns the nearest integer, rounding half away from zero.
Special cases are:
Round(±0) = ±0 Round(±Inf) = ±Inf Round(NaN) = NaN
func Sin ¶
func Sin[T Float](x T) T
Sin returns the sine of the radian argument x.
Special cases are:
Sin(±0) = ±0 Sin(±Inf) = NaN Sin(NaN) = NaN
func Sqrt ¶
func Sqrt[T Float](x T) T
Sqrt returns the square root of x.
Special cases are:
Sqrt(+Inf) = +Inf Sqrt(±0) = ±0 Sqrt(x < 0) = NaN Sqrt(NaN) = NaN
Types ¶
type Int26_6 ¶
type Int26_6 int32
Int26_6 is a signed 26.6 fixed-point number.
The integer part ranges from -33554432 to 33554431, inclusive. The fractional part has 6 bits of precision.
For example, the number one-and-a-quarter is Int26_6(1<<6 + 1<<4).
type Mat ¶
Mat represents a WxH matrix.
func (Mat[T]) Mul ¶
Mul applies matrix multiplication of two given matrix, and returns the resulting matrix: r = m*n
type Mat2 ¶
type Mat2[T Float] struct { // This is the best implementation that benefits from compiler // optimization, which exports all elements of a 3x4 Mat2. // See benchmarks at https://golang.design/research/pointer-params/. X00, X01 T X10, X11 T }
Mat2 represents a 2x2 Mat2:
/ X00, X01 \ \ X10, X11 /
func (Mat2[T]) MulM ¶
Mul implements Mat2 multiplication for two 3x3 matrices and assigns the result to this.
func (Mat2[T]) MulV ¶
MulVec implements Mat2 vector multiplication and returns the resulting vector.
type Mat3 ¶
type Mat3[T Float] struct { // This is the best implementation that benefits from compiler // optimization, which exports all elements of a 3x4 Mat3. // See benchmarks at https://golang.design/research/pointer-params/. X00, X01, X02 T X10, X11, X12 T X20, X21, X22 T }
Mat3 represents a 3x3 Mat3
/ X00, X01, X02 \ | X10, X11, X12 | \ X20, X21, X22 /
func (Mat3[T]) MulM ¶
Mul implements Mat3 multiplication for two 3x3 matrices and assigns the result to this.
func (Mat3[T]) MulV ¶
MulVec implements Mat3[T] vector multiplication and returns the resulting vector.
type Mat4 ¶
type Mat4[T Float] struct { // This is the best implementation that benefits from compiler // optimization, which exports all elements of a 4x4 Mat4. // See benchmarks at https://golang.design/research/pointer-params/. X00, X01, X02, X03 T X10, X11, X12, X13 T X20, X21, X22, X23 T X30, X31, X32, X33 T }
Mat4 represents a 4x4 Mat4
/ X00, X01, X02, X03 \ | X10, X11, X12, X13 | | X20, X21, X22, X23 | \ X30, X31, X32, X33 /
func ViewportMatrix ¶
ViewportMatrix returns the viewport matrix.
func (Mat4[T]) MulM ¶
Mul implements Mat4 multiplication for two 4x4 matrices and assigns the result to this.
func (Mat4[T]) MulV ¶
MulVec implements Mat4 vector multiplication and returns the resulting vector.
type Point26_6 ¶
type Point26_6 struct {
X, Y Int26_6
}
Point26_6 is a 26.6 fixed-point coordinate pair.
It is analogous to the image.Point type in the standard library.
type Quaternion ¶
func NewQuaternion ¶
func NewQuaternion[T Float](a, b, c, d T) Quaternion[T]
func (*Quaternion[T]) Mul ¶
func (q *Quaternion[T]) Mul(p Quaternion[T]) Quaternion[T]
func (*Quaternion[T]) ToRoMat ¶
func (q *Quaternion[T]) ToRoMat() Mat4[T]
type Rectangle26_6 ¶
type Rectangle26_6 struct {
Min, Max Point26_6
}
Rectangle26_6 is a 26.6 fixed-point coordinate rectangle. The Min bound is inclusive and the Max bound is exclusive. It is well-formed if Min.X <= Max.X and likewise for Y.
It is analogous to the image.Rectangle type in the standard library.
func R ¶
func R(minX, minY, maxX, maxY int) Rectangle26_6
R returns the integer values minX, minY, maxX, maxY as a Rectangle26_6.
For example, passing the integer values (0, 1, 2, 3) yields Rectangle26_6{Point26_6{0, 64}, Point26_6{128, 192}}.
Like the image.Rect function in the standard library, the returned rectangle has minimum and maximum coordinates swapped if necessary so that it is well-formed.
type TransformContext ¶
type TransformContext[T Float] struct { // contains filtered or unexported fields }
TransformContext is a transformation context (model matrix) that accumulates applied transformation matrices. The applying order of different types of transformations is:
1. rotation, 2. scaling, and 3. translation
context is a persistent status for the given mesh and can be reused for each of the rendering frame unless the mesh intentionally calls ResetContext() method.
A transformation context must be reset before use.
func (*TransformContext[T]) ModelMatrix ¶
func (ctx *TransformContext[T]) ModelMatrix() Mat4[T]
ModelMatrix returns the most recent transformation context.
func (*TransformContext[T]) ResetContext ¶
func (ctx *TransformContext[T]) ResetContext()
ResetContext resets the transformation context.
func (*TransformContext[T]) Rotate ¶
func (ctx *TransformContext[T]) Rotate(dir Vec3[T], angle T)
Rotate applies rotation on an arbitrary direction with an specified angle counterclockwise.
func (*TransformContext[T]) RotateX ¶
func (ctx *TransformContext[T]) RotateX(angle T)
RotateX applies rotation on X-axis direction with an specified angle counterclockwise.
func (*TransformContext[T]) RotateY ¶
func (ctx *TransformContext[T]) RotateY(angle T)
RotateY applies rotation on Y-axis direction with an specified angle counterclockwise.
func (*TransformContext[T]) RotateZ ¶
func (ctx *TransformContext[T]) RotateZ(angle T)
RotateZ applies rotation on Z-axis direction with an specified angle counterclockwise.
func (*TransformContext[T]) Scale ¶
func (ctx *TransformContext[T]) Scale(sx, sy, sz T)
Scale sets the scale matrix.
func (*TransformContext[T]) ScaleX ¶
func (ctx *TransformContext[T]) ScaleX(sx T)
ScaleX sets the scale matrix on X-axis.
func (*TransformContext[T]) ScaleY ¶
func (ctx *TransformContext[T]) ScaleY(sy T)
ScaleY sets the scale matrix on Y-axis.
func (*TransformContext[T]) ScaleZ ¶
func (ctx *TransformContext[T]) ScaleZ(sz T)
ScaleZ sets the scale matrix on Z-axis.
func (*TransformContext[T]) Translate ¶
func (ctx *TransformContext[T]) Translate(tx, ty, tz T)
Translate sets the translate matrix.
func (*TransformContext[T]) TranslateX ¶
func (ctx *TransformContext[T]) TranslateX(tx T)
TranslateX sets the translate matrix on X-axis.
func (*TransformContext[T]) TranslateY ¶
func (ctx *TransformContext[T]) TranslateY(ty T)
TranslateY sets the translate matrix on Y-axis.
func (*TransformContext[T]) TranslateZ ¶
func (ctx *TransformContext[T]) TranslateZ(tz T)
TranslateZ sets the translate matrix on Z-axis.
type Vec ¶
type Vec[T Float] struct { Data []T }
Vec is an N dimensional vector.
func (Vec[T]) IsZero ¶
IsZero asserts the x, y, z components of the given vector, and returns true if it is a zero vector or point.
type Vec2 ¶
type Vec2[T Float] struct { X, Y T }
Vec2 represents a 2D vector (x, y).
func LerpVec2 ¶
LerpV computes a linear interpolation between two given vectors regarding the given t parameter.
func NewRandVec2 ¶
NewRandVec2 returns a random 2D vector where all components are sitting in range [0, 1].
func (Vec2[T]) Apply ¶
Apply applies the 2D matrix multiplication to the given vector on the left side and returns the resulting 2D vector.
type Vec3 ¶
type Vec3[T Float] struct { X, Y, Z T }
Vec3 represents a 3D vector (x, y, z).
func LerpVec3 ¶
LerpV computes a linear interpolation between two given vectors regarding the given t parameter.
func NewRandVec3 ¶
NewRandVec2 returns a random 3D vector where all components are sitting in range [0, 1].
func (Vec3[T]) Apply ¶
Apply applies the 3D matrix multiplication to the given vector on the left side and returns the resulting 3D vector.
func (Vec3[T]) Cross ¶
Cross applies cross product of two given vectors and returns the resulting vector.
type Vec4 ¶
type Vec4[T Float] struct { X, Y, Z, W T }
Vec4 uses homogeneous coordinates (x, y, z, w) that represents either a point or a vector.
func LerpVec4 ¶
LerpV computes a linear interpolation between two given vectors regarding the given t parameter.
func (Vec4[T]) Apply ¶
ApplyMatrix applies 4x4 matrix and 4x1 vector multiplication. the given matrix multiplies v from the left.
func (Vec4[T]) Cross ¶
Cross applies cross product of two given vectors and returns the resulting vector.
func (Vec4[T]) IsZero ¶
IsZero asserts the x, y, z components of the given vector, and returns true if it is a zero vector or point.