Documentation ¶
Overview ¶
Package smath contains: general math functions, constants, matrices and vectors
Index ¶
- Constants
- func Distance(x1, y1, z1, x2, y2, z2 float64) float64
- func DistanceSquared(x1, y1, z1, x2, y2, z2 float64) float64
- func Dot(x1, y1, z1, x2, y2, z2 float64) float64
- func Euler(q Quaternion) (float64, float64, float64)
- func FromAxisAngle(angle float64, x, y, z float64, toQuat *Quaternion)
- func IsEqual(a float32, b float32) bool
- func Length(x, y, z float64) float64
- func LengthSquared(x, y, z float64) float64
- func Max32(a, b float32) float32
- func Min32(a, b float32) float32
- func Multiply(a, b, out *Matrix4)
- func MultiplyIntoA(a, b *Matrix4)
- func Norm(qin Quaternion) float64
- func Norm2(qin Quaternion) float64
- func RotMat(qin Quaternion) [3][3]float64
- func ToDegrees(radians float32) float32
- func ToRadians(degrees float32) float32
- type AxisAngle
- type Matrix4
- func (m *Matrix4) C(i int) float64
- func (m *Matrix4) Clone() *Matrix4
- func (m *Matrix4) GetScale(out *Vector3)
- func (m *Matrix4) GetTranslation(out *Vector3)
- func (m *Matrix4) Multiply(a, b *Matrix4)
- func (m *Matrix4) PostMultiply(b *Matrix4)
- func (m *Matrix4) PostScale(sx, sy, sz float64) *Matrix4
- func (m *Matrix4) PostTranslate(tx, ty, tz float64) *Matrix4
- func (m *Matrix4) PreMultiply(b *Matrix4)
- func (m *Matrix4) RotateBy(angle float64) *Matrix4
- func (m *Matrix4) ScaleBy(v *Vector3) *Matrix4
- func (m *Matrix4) Set(src *Matrix4) *Matrix4
- func (m *Matrix4) SetRotation(angle float64) *Matrix4
- func (m *Matrix4) SetScale(v *Vector3) *Matrix4
- func (m *Matrix4) SetScale2Comp(sx, sy float64) *Matrix4
- func (m *Matrix4) SetScale3Comp(sx, sy, sz float64) *Matrix4
- func (m *Matrix4) SetToOrtho(left, right, bottom, top, near, far float64) *Matrix4
- func (m *Matrix4) SetTranslate3Comp(x, y, z float64) *Matrix4
- func (m *Matrix4) SetTranslateByVector(v *Vector3) *Matrix4
- func (m Matrix4) String() string
- func (m *Matrix4) ToIdentity()
- func (m *Matrix4) TranslateBy(v *Vector3) *Matrix4
- func (m *Matrix4) TranslateBy2Comps(x, y float64) *Matrix4
- func (m *Matrix4) TranslateBy3Comps(x, y, z float64) *Matrix4
- type Quaternion
- type Rectangle
- type Vector3
- func (v *Vector3) Add(src *Vector3) *Vector3
- func (v *Vector3) Add2Components(x, y float64) *Vector3
- func (v *Vector3) Clone() *Vector3
- func (v *Vector3) Cross(o *Vector3) *Vector3
- func (v *Vector3) Distance(src *Vector3) float64
- func (v *Vector3) DistanceSquared(src *Vector3) float64
- func (v *Vector3) DivScalar(scale float64)
- func (v *Vector3) Dot(o *Vector3) float64
- func (v *Vector3) DotByComponent(x, y, z float64) float64
- func (v *Vector3) EqEpsilon(other *Vector3) bool
- func (v *Vector3) Equal(other *Vector3) bool
- func (v *Vector3) Length() float64
- func (v *Vector3) LengthSquared() float64
- func (v *Vector3) Mul(m *Matrix4) *Vector3
- func (v *Vector3) MulAdd(src *Vector3, scalar float64) *Vector3
- func (v *Vector3) Normalize()
- func (v *Vector3) ScaleBy(s float64) *Vector3
- func (v *Vector3) ScaleBy2Components(sx, sy float64) *Vector3
- func (v *Vector3) Set(source *Vector3)
- func (v *Vector3) Set2Components(x, y float64)
- func (v *Vector3) Set3Components(x, y, z float64)
- func (v Vector3) String() string
- func (v *Vector3) Sub(src *Vector3) *Vector3
- func (v *Vector3) Sub2Components(x, y float64) *Vector3
Constants ¶
const ( // M00 XX: Typically the unrotated X component for scaling, also the cosine of the // angle when rotated on the Y and/or Z axis. On // Vector3 multiplication this value is multiplied with the source X component // and added to the target X component. M00 = 0 // M01 XY: Typically the negative sine of the angle when rotated on the Z axis. // On Vector3 multiplication this value is multiplied // with the source Y component and added to the target X component. M01 = 4 // M02 XZ: Typically the sine of the angle when rotated on the Y axis. // On Vector3 multiplication this value is multiplied with the // source Z component and added to the target X component. M02 = 8 // M03 XW: Typically the translation of the X component. // On Vector3 multiplication this value is added to the target X component. M03 = 12 // M10 YX: Typically the sine of the angle when rotated on the Z axis. // On Vector3 multiplication this value is multiplied with the // source X component and added to the target Y component. M10 = 1 // M11 YY: Typically the unrotated Y component for scaling, also the cosine // of the angle when rotated on the X and/or Z axis. On // Vector3 multiplication this value is multiplied with the source Y // component and added to the target Y component. M11 = 5 // M12 YZ: Typically the negative sine of the angle when rotated on the X axis. // On Vector3 multiplication this value is multiplied // with the source Z component and added to the target Y component. M12 = 9 // M13 YW: Typically the translation of the Y component. // On Vector3 multiplication this value is added to the target Y component. M13 = 13 // M20 ZX: Typically the negative sine of the angle when rotated on the Y axis. // On Vector3 multiplication this value is multiplied // with the source X component and added to the target Z component. M20 = 2 // M21 ZY: Typical the sine of the angle when rotated on the X axis. // On Vector3 multiplication this value is multiplied with the // source Y component and added to the target Z component. M21 = 6 // M22 ZZ: Typically the unrotated Z component for scaling, also the cosine of the // angle when rotated on the X and/or Y axis. // On Vector3 multiplication this value is multiplied with the source Z component // and added to the target Z component. M22 = 10 // M23 ZW: Typically the translation of the Z component. // On Vector3 multiplication this value is added to the target Z component. M23 = 14 // M30 WX: Typically the value zero. On Vector3 multiplication this value is ignored. M30 = 3 // M31 WY: Typically the value zero. On Vector3 multiplication this value is ignored. M31 = 7 // M32 WZ: Typically the value zero. On Vector3 multiplication this value is ignored. M32 = 11 // M33 WW: Typically the value one. On Vector3 multiplication this value is ignored. M33 = 15 )
const Epsilon = 0.00001 // ~32 bits
Epsilon = 0.00001
const PI = 3.1415927
PI or π = ~3.1415927
const PI2 = PI * 2
PI2 or 2π
Variables ¶
This section is empty.
Functions ¶
func DistanceSquared ¶
DistanceSquared finds the euclidean distance between the two specified vectors squared
func Euler ¶
func Euler(q Quaternion) (float64, float64, float64)
Euler returns the Euler angles phi, theta, psi corresponding to a Quaternion
func FromAxisAngle ¶
func FromAxisAngle(angle float64, x, y, z float64, toQuat *Quaternion)
FromAxisAngle sets the quaternion as a rotation with with specified angle (radians) and axis. The axis should be normalized! Return this
func LengthSquared ¶
LengthSquared returns the euclidean length squared
func Multiply ¶
func Multiply(a, b, out *Matrix4)
Multiply multiplies a * b and places result into 'out', (i.e. out = a * b)
func MultiplyIntoA ¶
func MultiplyIntoA(a, b *Matrix4)
MultiplyIntoA multiplies a * b and places result into 'a', (i.e. a = a * b)
func Norm ¶
func Norm(qin Quaternion) float64
Norm returns the L1-Norm of a Quaternion (W,X,Y,Z) -> Sqrt(W*W+X*X+Y*Y+Z*Z)
func Norm2 ¶
func Norm2(qin Quaternion) float64
Norm2 returns the L2-Norm of a Quaternion (W,X,Y,Z) -> W*W+X*X+Y*Y+Z*Z
func RotMat ¶
func RotMat(qin Quaternion) [3][3]float64
RotMat returns the rotation matrix (as float array) corresponding to a Quaternion
Types ¶
type AxisAngle ¶
type AxisAngle struct { X float64 // i component Y float64 // j component Z float64 // k component Angle float64 }
AxisAngle represents an axis and angle
func (*AxisAngle) SetFromQuaternion ¶
func (aa *AxisAngle) SetFromQuaternion(q *Quaternion)
SetFromQuaternion axis/angle based on a quaternion.
type Matrix4 ¶
type Matrix4 struct { // Rotation is in radians Rotation float64 Scale Vector3 // contains filtered or unexported fields }
Matrix4 represents a column major opengl array.
func NewMatrix4 ¶
func NewMatrix4() *Matrix4
NewMatrix4 creates a Matrix4 initialized to an identity matrix
func (*Matrix4) GetTranslation ¶
GetTranslation returns the translational components in 'out' Vector3 field.
func (*Matrix4) Multiply ¶
Multiply multiplies a * b and places result into this matrix, (i.e. this = a * b)
func (*Matrix4) PostMultiply ¶
PostMultiply postmultiplies 'b' matrix with 'this' and places the result into 'this' matrix. (i.e. this = this * b)
func (*Matrix4) PostTranslate ¶
PostTranslate postmultiplies this matrix by a translation matrix. Postmultiplication is also used by OpenGL ES.
func (*Matrix4) PreMultiply ¶
PreMultiply premultiplies 'b' matrix with 'this' and places the result into 'this' matrix. (i.e. this = b * this)
func (*Matrix4) RotateBy ¶
RotateBy postmultiplies this matrix with a (counter-clockwise) rotation matrix whose angle is specified in radians.
func (*Matrix4) SetRotation ¶
SetRotation set a rotation matrix about Z axis. 'angle' is specified in radians.
[ M00 M01 _ _ ] [ M10 M11 _ _ ] [ _ _ _ _ ] [ _ _ _ _ ]
func (*Matrix4) SetScale ¶
SetScale sets the scale components of an identity matrix and captures scale values into Scale property.
func (*Matrix4) SetScale2Comp ¶
SetScale2Comp sets the scale components of an identity matrix and captures scale values into Scale property where Z component = 1.0.
func (*Matrix4) SetScale3Comp ¶
SetScale3Comp sets the scale components of an identity matrix and captures scale values into Scale property.
func (*Matrix4) SetToOrtho ¶
SetToOrtho sets the matrix for a 2d ortho graphic projection
func (*Matrix4) SetTranslate3Comp ¶
SetTranslate3Comp sets the translational component to the matrix in the 4th column. The other columns are unmodified.
func (*Matrix4) SetTranslateByVector ¶
SetTranslateByVector sets the translational component to the matrix in the 4th column. The other columns are unmodified.
func (*Matrix4) ToIdentity ¶
func (m *Matrix4) ToIdentity()
ToIdentity set this matrix to the identity matrix
func (*Matrix4) TranslateBy ¶
TranslateBy adds a translational component to the matrix in the 4th column. The other columns are unmodified.
func (*Matrix4) TranslateBy2Comps ¶
TranslateBy2Comps adds a translational component to the matrix in the 4th column. Z is unmodified. The other columns are unmodified.
func (*Matrix4) TranslateBy3Comps ¶
TranslateBy3Comps adds a translational component to the matrix in the 4th column. The other columns are unmodified.
type Quaternion ¶
type Quaternion struct { W float64 // Scalar component X float64 // i component Y float64 // j component Z float64 // k component }
Quaternion represents a quaternion W+X*i+Y*j+Z*k
func Conj ¶
func Conj(qin Quaternion) Quaternion
Conj returns the conjugate of a Quaternion (W,X,Y,Z) -> (W,-X,-Y,-Z)
func FromEuler ¶
func FromEuler(phi, theta, psi float64) Quaternion
FromEuler returns a Quaternion corresponding to Euler angles phi, theta, psi
func Inv ¶
func Inv(qin Quaternion) Quaternion
Inv returns the Quaternion conjugate rescaled so that Q Q* = 1
func Prod ¶
func Prod(qin ...Quaternion) Quaternion
Prod returns the non-commutative product of any number of Quaternions
func Scalar ¶
func Scalar(w float64) Quaternion
Scalar returns a scalar-only Quaternion representation of a float (W,0,0,0)
func Sum ¶
func Sum(qin ...Quaternion) Quaternion
Sum returns the vector sum of any number of Quaternions
func Unit ¶
func Unit(qin Quaternion) Quaternion
Unit returns the Quaternion rescaled to unit-L1-norm
func (*Quaternion) Conjugate ¶
func (qin *Quaternion) Conjugate()
Conjugate conjugates this Quaternion (W,X,Y,Z) -> (W,-X,-Y,-Z)
func (*Quaternion) SetFromComponents ¶
func (qin *Quaternion) SetFromComponents(x, y, z, w float64)
SetFromComponents sets this Q from base components
type Rectangle ¶
type Rectangle struct {
// Upper/Lower Components used mostly for rendering
Top, Left, Bottom, Right float32
// Components typically used for computations
Width, Height float32
Centered bool
}
Rectangle is represented in a coordinate system defined and shaped as follows:
Left,Top ^---------. | |
Y axis | |
| | | | .---------> Right, Bottom X axis
This orientation is configured via an orthographic projection
type Vector3 ¶
type Vector3 struct {
X, Y, Z float64
}
Vector3 contains base components
func NewVector3 ¶
func NewVector3() *Vector3
NewVector3 creates a Vector3 initialized to 0.0, 0.0, 0.0
func NewVector3With2Components ¶
NewVector3With2Components creates a Vector3 initialized with x,y and z = 0.0
func NewVector3With3Components ¶
NewVector3With3Components creates a Vector3 initialized with x,y,z
func (*Vector3) Add2Components ¶
Add2Components adds x and y to this vector
func (*Vector3) Cross ¶
Cross sets this vector to the cross product between it and the other vector.
func (*Vector3) DistanceSquared ¶
DistanceSquared finds the euclidean distance between the two specified vectors squared
func (*Vector3) DotByComponent ¶
DotByComponent returns the product between the two vectors
func (*Vector3) LengthSquared ¶
LengthSquared returns the euclidean length squared
func (*Vector3) Mul ¶
Mul left-multiplies the vector by the given matrix, assuming the fourth (w) component of the vector is 1.
func (*Vector3) ScaleBy2Components ¶
ScaleBy2Components scales this vector by sx and sy
func (*Vector3) Set2Components ¶
Set2Components modifies x,y only
func (*Vector3) Set3Components ¶
Set3Components modifies x,y,z
func (*Vector3) Sub2Components ¶
Sub2Components subtracts x and y to this vector