smath

package
v0.0.0-...-72a9a6f Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2019 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package smath contains: general math functions, constants, matrices and vectors

Index

Constants

View Source
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
)
View Source
const Epsilon = 0.00001 // ~32 bits

Epsilon = 0.00001

View Source
const PI = 3.1415927

PI or π = ~3.1415927

View Source
const PI2 = PI * 2

PI2 or 2 π

Variables

This section is empty.

Functions

func Distance

func Distance(x1, y1, z1, x2, y2, z2 float64) float64

Distance finds the euclidean distance between the two specified vectors

func DistanceSquared

func DistanceSquared(x1, y1, z1, x2, y2, z2 float64) float64

DistanceSquared finds the euclidean distance between the two specified vectors squared

func Dot

func Dot(x1, y1, z1, x2, y2, z2 float64) float64

Dot returns the product between the two vectors

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 IsEqual

func IsEqual(a float32, b float32) bool

IsEqual compares to floats based on an Epsilon float

func Length

func Length(x, y, z float64) float64

Length returns the euclidean length

func LengthSquared

func LengthSquared(x, y, z float64) float64

LengthSquared returns the euclidean length squared

func Max32

func Max32(a, b float32) float32

Max32 return the float32 max value

func Min32

func Min32(a, b float32) float32

Min32 return the float32 min value

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

func ToDegrees

func ToDegrees(radians float32) float32

ToDegrees converts radians to degrees

func ToRadians

func ToRadians(degrees float32) float32

ToRadians converts degrees to radians

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

func (m *Matrix4) C(i int) float64

C returns a cell value based on Mxx index

func (*Matrix4) Clone

func (m *Matrix4) Clone() *Matrix4

Clone returns a clone of this matrix

func (*Matrix4) GetScale

func (m *Matrix4) GetScale(out *Vector3)

GetScale returns the scale in 'out' field.

func (*Matrix4) GetTranslation

func (m *Matrix4) GetTranslation(out *Vector3)

GetTranslation returns the translational components in 'out' Vector3 field.

func (*Matrix4) Multiply

func (m *Matrix4) Multiply(a, b *Matrix4)

Multiply multiplies a * b and places result into this matrix, (i.e. this = a * b)

func (*Matrix4) PostMultiply

func (m *Matrix4) PostMultiply(b *Matrix4)

PostMultiply postmultiplies 'b' matrix with 'this' and places the result into 'this' matrix. (i.e. this = this * b)

func (*Matrix4) PostScale

func (m *Matrix4) PostScale(sx, sy, sz float64) *Matrix4

PostScale postmultiplies this matrix with a scale matrix.

func (*Matrix4) PostTranslate

func (m *Matrix4) PostTranslate(tx, ty, tz float64) *Matrix4

PostTranslate postmultiplies this matrix by a translation matrix. Postmultiplication is also used by OpenGL ES.

func (*Matrix4) PreMultiply

func (m *Matrix4) PreMultiply(b *Matrix4)

PreMultiply premultiplies 'b' matrix with 'this' and places the result into 'this' matrix. (i.e. this = b * this)

func (*Matrix4) RotateBy

func (m *Matrix4) RotateBy(angle float64) *Matrix4

RotateBy postmultiplies this matrix with a (counter-clockwise) rotation matrix whose angle is specified in radians.

func (*Matrix4) ScaleBy

func (m *Matrix4) ScaleBy(v *Vector3) *Matrix4

ScaleBy scales the scale components.

func (*Matrix4) Set

func (m *Matrix4) Set(src *Matrix4) *Matrix4

Set copies src into this matrix

func (*Matrix4) SetRotation

func (m *Matrix4) SetRotation(angle float64) *Matrix4

SetRotation set a rotation matrix about Z axis. 'angle' is specified in radians.

[  M00  M01   _    _   ]
[  M10  M11   _    _   ]
[   _    _    _    _   ]
[   _    _    _    _   ]

func (*Matrix4) SetScale

func (m *Matrix4) SetScale(v *Vector3) *Matrix4

SetScale sets the scale components of an identity matrix and captures scale values into Scale property.

func (*Matrix4) SetScale2Comp

func (m *Matrix4) SetScale2Comp(sx, sy float64) *Matrix4

SetScale2Comp sets the scale components of an identity matrix and captures scale values into Scale property where Z component = 1.0.

func (*Matrix4) SetScale3Comp

func (m *Matrix4) SetScale3Comp(sx, sy, sz float64) *Matrix4

SetScale3Comp sets the scale components of an identity matrix and captures scale values into Scale property.

func (*Matrix4) SetToOrtho

func (m *Matrix4) SetToOrtho(left, right, bottom, top, near, far float64) *Matrix4

SetToOrtho sets the matrix for a 2d ortho graphic projection

func (*Matrix4) SetTranslate3Comp

func (m *Matrix4) SetTranslate3Comp(x, y, z float64) *Matrix4

SetTranslate3Comp sets the translational component to the matrix in the 4th column. The other columns are unmodified.

func (*Matrix4) SetTranslateByVector

func (m *Matrix4) SetTranslateByVector(v *Vector3) *Matrix4

SetTranslateByVector sets the translational component to the matrix in the 4th column. The other columns are unmodified.

func (Matrix4) String

func (m Matrix4) String() string

func (*Matrix4) ToIdentity

func (m *Matrix4) ToIdentity()

ToIdentity set this matrix to the identity matrix

func (*Matrix4) TranslateBy

func (m *Matrix4) TranslateBy(v *Vector3) *Matrix4

TranslateBy adds a translational component to the matrix in the 4th column. The other columns are unmodified.

func (*Matrix4) TranslateBy2Comps

func (m *Matrix4) TranslateBy2Comps(x, y float64) *Matrix4

TranslateBy2Comps adds a translational component to the matrix in the 4th column. Z is unmodified. The other columns are unmodified.

func (*Matrix4) TranslateBy3Comps

func (m *Matrix4) TranslateBy3Comps(x, y, z float64) *Matrix4

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

func (qin *Quaternion) Set(q *Quaternion)

Set sets this Q from 'q'

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

func NewVector3With2Components(x, y float64) *Vector3

NewVector3With2Components creates a Vector3 initialized with x,y and z = 0.0

func NewVector3With3Components

func NewVector3With3Components(x, y, z float64) *Vector3

NewVector3With3Components creates a Vector3 initialized with x,y,z

func (*Vector3) Add

func (v *Vector3) Add(src *Vector3) *Vector3

Add a Vector3 to this vector

func (*Vector3) Add2Components

func (v *Vector3) Add2Components(x, y float64) *Vector3

Add2Components adds x and y to this vector

func (*Vector3) Clone

func (v *Vector3) Clone() *Vector3

Clone returns a new copy this vector

func (*Vector3) Cross

func (v *Vector3) Cross(o *Vector3) *Vector3

Cross sets this vector to the cross product between it and the other vector.

func (*Vector3) Distance

func (v *Vector3) Distance(src *Vector3) float64

Distance finds the euclidean distance between the two specified vectors

func (*Vector3) DistanceSquared

func (v *Vector3) DistanceSquared(src *Vector3) float64

DistanceSquared finds the euclidean distance between the two specified vectors squared

func (*Vector3) DivScalar

func (v *Vector3) DivScalar(scale float64)

DivScalar 1/scales this vector

func (*Vector3) Dot

func (v *Vector3) Dot(o *Vector3) float64

Dot returns the product between the two vectors

func (*Vector3) DotByComponent

func (v *Vector3) DotByComponent(x, y, z float64) float64

DotByComponent returns the product between the two vectors

func (*Vector3) EqEpsilon

func (v *Vector3) EqEpsilon(other *Vector3) bool

EqEpsilon makes an approximate equality check. Preferred

func (*Vector3) Equal

func (v *Vector3) Equal(other *Vector3) bool

Equal makes an exact equality check. Use EqEpsilon, it is more realistic.

func (*Vector3) Length

func (v *Vector3) Length() float64

Length returns the euclidean length

func (*Vector3) LengthSquared

func (v *Vector3) LengthSquared() float64

LengthSquared returns the euclidean length squared

func (*Vector3) Mul

func (v *Vector3) Mul(m *Matrix4) *Vector3

Mul left-multiplies the vector by the given matrix, assuming the fourth (w) component of the vector is 1.

func (*Vector3) MulAdd

func (v *Vector3) MulAdd(src *Vector3, scalar float64) *Vector3

MulAdd scales and adds src to this vector

func (*Vector3) Normalize

func (v *Vector3) Normalize()

Normalize this instance

func (*Vector3) ScaleBy

func (v *Vector3) ScaleBy(s float64) *Vector3

ScaleBy scales this vector by s

func (*Vector3) ScaleBy2Components

func (v *Vector3) ScaleBy2Components(sx, sy float64) *Vector3

ScaleBy2Components scales this vector by sx and sy

func (*Vector3) Set

func (v *Vector3) Set(source *Vector3)

Set modifies x,y,z from source

func (*Vector3) Set2Components

func (v *Vector3) Set2Components(x, y float64)

Set2Components modifies x,y only

func (*Vector3) Set3Components

func (v *Vector3) Set3Components(x, y, z float64)

Set3Components modifies x,y,z

func (Vector3) String

func (v Vector3) String() string

func (*Vector3) Sub

func (v *Vector3) Sub(src *Vector3) *Vector3

Sub subtracts a Vector3 to this vector

func (*Vector3) Sub2Components

func (v *Vector3) Sub2Components(x, y float64) *Vector3

Sub2Components subtracts x and y to this vector

Jump to

Keyboard shortcuts

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