Documentation ¶
Overview ¶
Package math implements some custom math utilities, along with 32 bit wrappers for some Go math functions.
Index ¶
- func Abs(v float32) float32
- func Ceil(v float32) float32
- func Clamp(x, min, max float32) float32
- func Extract3DScale(m Mat4) (x, y, z float32)
- func ExtractMaxScale(m Mat4) float32
- func FloatEqual(a, b float32) bool
- func FloatEqualFunc(epsilon float32) func(float32, float32) bool
- func FloatEqualThreshold(a, b, epsilon float32) bool
- func Floor(v float32) float32
- func IsPow2(n int) bool
- func Pow2(n int) int
- func Remainder(a, b float32) float32
- type Mat4
- func Frustum(left, right, bottom, top, near, far float32) Mat4
- func HomogRotate3D(angle float32, axis Vec3) Mat4
- func HomogRotate3DX(angle float32) Mat4
- func HomogRotate3DY(angle float32) Mat4
- func HomogRotate3DZ(angle float32) Mat4
- func Ident4() Mat4
- func LookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ float32) Mat4
- func LookAtV(eye, center, up Vec3) Mat4
- func Ortho(left, right, bottom, top, near, far float32) Mat4
- func Ortho2D(left, right, bottom, top float32) Mat4
- func Perspective(fovy, aspect, near, far float32) Mat4
- func Scale3D(scaleX, scaleY, scaleZ float32) Mat4
- func ShearX3D(shearY, shearZ float32) Mat4
- func ShearY3D(shearX, shearZ float32) Mat4
- func ShearZ3D(shearX, shearY float32) Mat4
- func Translate3D(Tx, Ty, Tz float32) Mat4
- func (m1 Mat4) Abs() Mat4
- func (m1 Mat4) Add(m2 Mat4) Mat4
- func (m1 Mat4) ApproxEqual(m2 Mat4) bool
- func (m1 Mat4) ApproxEqualThreshold(m2 Mat4, threshold float32) bool
- func (m1 Mat4) ApproxFuncEqual(m2 Mat4, eq func(float32, float32) bool) bool
- func (m1 Mat4) At(row, col int) float32
- func (m1 Mat4) Col(col int) Vec4
- func (m1 Mat4) Cols() (col0, col1, col2, col3 Vec4)
- func (m1 Mat4) Det() float32
- func (m1 Mat4) Diag() Vec4
- func (m1 Mat4) Index(row, col int) int
- func (m1 Mat4) Inv() Mat4
- func (m1 Mat4) Mul(c float32) Mat4
- func (m1 Mat4) Mul4(m2 Mat4) Mat4
- func (m1 Mat4) Mul4x1(m2 Vec4) Vec4
- func (m1 Mat4) Row(row int) Vec4
- func (m1 Mat4) Rows() (row0, row1, row2, row3 Vec4)
- func (m1 *Mat4) Set(row, col int, value float32)
- func (m1 *Mat4) SetCol(col int, v Vec4)
- func (m1 *Mat4) SetRow(row int, v Vec4)
- func (m1 Mat4) String() string
- func (m1 Mat4) Sub(m2 Mat4) Mat4
- func (m1 Mat4) Trace() float32
- func (m1 Mat4) Transpose() Mat4
- type Transform
- type Vec2
- func (v1 Vec2) Add(v2 Vec2) Vec2
- func (v1 Vec2) ApproxEqual(v2 Vec2) bool
- func (v1 Vec2) ApproxEqualThreshold(v2 Vec2, threshold float32) bool
- func (v1 Vec2) ApproxFuncEqual(v2 Vec2, eq func(float32, float32) bool) bool
- func (v Vec2) Clamp(min, max float32) Vec2
- func (a Vec2) Div(b Vec2) Vec2
- func (a Vec2) DivFloor(b Vec2) Vec2
- func (v Vec2) DivScalar(f float32) Vec2
- func (v1 Vec2) Dot(v2 Vec2) float32
- func (v Vec2) Elem() (x, y float32)
- func (v1 Vec2) Len() float32
- func (v1 Vec2) LenSqr() float32
- func (a Vec2) Mul(b Vec2) Vec2
- func (v1 Vec2) MulScalar(c float32) Vec2
- func (v1 Vec2) Normalize() Vec2
- func (a Vec2) Remainder(b Vec2) Vec2
- func (v1 Vec2) Sub(v2 Vec2) Vec2
- func (v Vec2) Vec3(z float32) Vec3
- func (v Vec2) Vec4(z, w float32) Vec4
- func (v Vec2) X() float32
- func (v Vec2) Y() float32
- type Vec3
- func Project(obj Vec3, modelview, projection Mat4, initialX, initialY, width, height int) (win Vec3)
- func TransformCoordinate(v Vec3, m Mat4) Vec3
- func TransformNormal(v Vec3, m Mat4) Vec3
- func UnProject(win Vec3, modelview, projection Mat4, initialX, initialY, width, height int) (obj Vec3, err error)
- func (v1 Vec3) Add(v2 Vec3) Vec3
- func (v1 Vec3) ApproxEqual(v2 Vec3) bool
- func (v1 Vec3) ApproxEqualThreshold(v2 Vec3, threshold float32) bool
- func (v1 Vec3) ApproxFuncEqual(v2 Vec3, eq func(float32, float32) bool) bool
- func (v Vec3) Clamp(min, max float32) Vec3
- func (v1 Vec3) Cross(v2 Vec3) Vec3
- func (a Vec3) Div(b Vec3) Vec3
- func (a Vec3) DivFloor(b Vec3) Vec3
- func (v Vec3) DivScalar(f float32) Vec3
- func (v1 Vec3) Dot(v2 Vec3) float32
- func (v Vec3) Elem() (x, y, z float32)
- func (v1 Vec3) Len() float32
- func (v1 Vec3) LenSqr() float32
- func (a Vec3) Mul(b Vec4) Vec3
- func (v1 Vec3) MulScalar(c float32) Vec3
- func (v1 Vec3) Normalize() Vec3
- func (a Vec3) Remainder(b Vec3) Vec3
- func (v1 Vec3) Sub(v2 Vec3) Vec3
- func (v Vec3) Vec2() Vec2
- func (v Vec3) Vec4(w float32) Vec4
- func (v Vec3) X() float32
- func (v Vec3) Y() float32
- func (v Vec3) Z() float32
- type Vec4
- func (v1 Vec4) Add(v2 Vec4) Vec4
- func (v1 Vec4) ApproxEqual(v2 Vec4) bool
- func (v1 Vec4) ApproxEqualThreshold(v2 Vec4, threshold float32) bool
- func (v1 Vec4) ApproxFuncEqual(v2 Vec4, eq func(float32, float32) bool) bool
- func (v Vec4) Clamp(min, max float32) Vec4
- func (a Vec4) Div(b Vec4) Vec4
- func (a Vec4) DivFloor(b Vec4) Vec4
- func (v Vec4) DivScalar(f float32) Vec4
- func (v1 Vec4) Dot(v2 Vec4) float32
- func (v Vec4) Elem() (x, y, z, w float32)
- func (v1 Vec4) Len() float32
- func (v1 Vec4) LenSqr() float32
- func (a Vec4) Mul(b Vec4) Vec4
- func (v1 Vec4) MulScalar(c float32) Vec4
- func (v1 Vec4) Normalize() Vec4
- func (v1 Vec4) OuterProd4(v2 Vec4) Mat4
- func (a Vec4) Remainder(b Vec4) Vec4
- func (v1 Vec4) Sub(v2 Vec4) Vec4
- func (v Vec4) Vec2() Vec2
- func (v Vec4) Vec3() Vec3
- func (v Vec4) W() float32
- func (v Vec4) X() float32
- func (v Vec4) Y() float32
- func (v Vec4) Z() float32
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Ceil ¶
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 Extract3DScale ¶
Extract3DScale extracts the 3d scaling from a homogeneous matrix
func ExtractMaxScale ¶
ExtractMaxScale extracts the maximum scaling from a homogeneous matrix
func FloatEqual ¶
FloatEqual is a safe utility function to compare floats. It's Taken from http://floating-point-gui.de/errors/comparison/
It is slightly altered to not call Abs when not needed.
func FloatEqualFunc ¶
FloatEqualFunc is a utility closure that will generate a function that always approximately compares floats like FloatEqualThreshold with a different threshold.
func FloatEqualThreshold ¶
FloatEqualThreshold is a utility function to compare floats. It's Taken from http://floating-point-gui.de/errors/comparison/
It is slightly altered to not call Abs when not needed.
This differs from FloatEqual in that it lets you pass in your comparison threshold, so that you can adjust the comparison value to your specific needs
func Floor ¶
Floor returns the greatest integer value less than or equal to x.
Special cases are:
Floor(±0) = ±0 Floor(±Inf) = ±Inf Floor(NaN) = NaN
Types ¶
type Mat4 ¶
type Mat4 [16]float32
Mat4 is a 4x4 matrix in row major order.
m[4*r + c] is the element in the r'th row and c'th column.
func HomogRotate3D ¶
HomogRotate3D creates a 3D rotation Matrix that rotates by (radian) angle about some arbitrary axis given by a normalized Vector. It produces a homogeneous matrix (4x4)
Where c is cos(angle) and s is sin(angle), and x, y, and z are the first, second, and third elements of the axis vector (respectively):
[[ x^2(1-c)+c, xy(1-c)-zs, xz(1-c)+ys, 0 ]] [[ xy(1-c)+zs, y^2(1-c)+c, yz(1-c)-xs, 0 ]] [[ xz(1-c)-ys, yz(1-c)+xs, z^2(1-c)+c, 0 ]] [[ 0 , 0 , 0 , 1 ]]
func HomogRotate3DX ¶
HomogRotate3DX is the same as Rotate3DX, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate3DY ¶
HomogRotate3DY is the same as Rotate3DY, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate3DZ ¶
HomogRotate3DZ is the same as Rotate3DZ, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)
func Ident4 ¶
func Ident4() Mat4
Ident4 returns the 4x4 identity matrix. The identity matrix is a square matrix with the value 1 on its diagonals. The characteristic property of the identity matrix is that any matrix multiplied by it is itself. (MI = M; IN = N)
func Ortho2D ¶
Ortho2D is equivalent to Ortho with the near and far planes being -1 and 1, respectively.
func Perspective ¶
Perspective generates a Perspective Matrix.
func Scale3D ¶
Scale3D creates a homogeneous 3D scaling matrix [[ scaleX, 0 , 0 , 0 ]] [[ 0 , scaleY, 0 , 0 ]] [[ 0 , 0 , scaleZ, 0 ]] [[ 0 , 0 , 0 , 1 ]]
func Translate3D ¶
Translate3D returns a homogeneous (4x4 for 3D-space) Translation matrix that moves a point by Tx units in the x-direction, Ty units in the y-direction, and Tz units in the z-direction
[[1, 0, 0, Tx]] [[0, 1, 0, Ty]] [[0, 0, 1, Tz]] [[0, 0, 0, 1 ]]
func (Mat4) Add ¶
Add performs an element-wise addition of two matrices, this is equivalent to iterating over every element of m1 and adding the corresponding value of m2.
func (Mat4) ApproxEqual ¶
ApproxEqual performs an element-wise approximate equality test between two matrices, as if FloatEqual had been used.
func (Mat4) ApproxEqualThreshold ¶
ApproxEqualThreshold performs an element-wise approximate equality test between two matrices with a given epsilon threshold, as if FloatEqualThreshold had been used.
func (Mat4) ApproxFuncEqual ¶
ApproxFuncEqual performs an element-wise approximate equality test between two matrices with a given equality functions, intended to be used with FloatEqualFunc; although and comparison function may be used in practice.
func (Mat4) At ¶
At returns the matrix element at the given row and column. This is equivalent to mat[col * numRow + row] where numRow is constant (E.G. for a Mat3x2 it's equal to 3)
This method is garbage-in garbage-out. For instance, on a Mat4 asking for At(5,0) will work just like At(1,1). Or it may panic if it's out of bounds.
func (Mat4) Col ¶
Col returns a vector representing the corresponding column (starting at col 0). This package makes no distinction between row and column vectors, so it will be a normal VecN for a MxN matrix.
func (Mat4) Cols ¶
Cols decomposes a matrix into its corresponding column vectors. This is equivalent to calling mat.Col for each column.
func (Mat4) Det ¶
Det returns the determinant of a matrix. It is a measure of a square matrix's singularity and invertability, among other things. In this library, the determinant is hard coded based on pre-computed cofactor expansion, and uses no loops. Of course, the addition and multiplication must still be done.
func (Mat4) Diag ¶
Diag is a basic operation on a square matrix that simply returns main diagonal (meaning all elements such that row==col).
func (Mat4) Index ¶
Index returns the index of the given row and column, to be used with direct access. E.G. Index(0,0) = 0.
This is a garbage-in garbage-out method. For instance, on a Mat4 asking for the index of (5,0) will work the same as asking for (1,1). Or it may give you a value that will cause a panic if you try to access the array with it if it's truly out of bounds.
func (Mat4) Inv ¶
Inv computes the inverse of a square matrix. An inverse is a square matrix such that when multiplied by the original, yields the identity.
M_inv * M = M * M_inv = I
In this library, the math is precomputed, and uses no loops, though the multiplications, additions, determinant calculation, and scaling are still done. This can still be (relatively) expensive for a 4x4.
This function checks the determinant to see if the matrix is invertible. If the determinant is 0.0, this function returns the zero matrix. However, due to floating point errors, it is entirely plausible to get a false positive or negative. In the future, an alternate function may be written which takes in a pre-computed determinant.
func (Mat4) Mul ¶
Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating over every element of the matrix and multiply it by c.
func (Mat4) Mul4 ¶
Mul4 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.
func (Mat4) Mul4x1 ¶
Mul4x1 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.
func (Mat4) Row ¶
Row returns a vector representing the corresponding row (starting at row 0). This package makes no distinction between row and column vectors, so it will be a normal VecM for a MxN matrix.
func (Mat4) Rows ¶
Rows decomposes a matrix into its corresponding row vectors. This is equivalent to calling mat.Row for each row.
func (*Mat4) Set ¶
Set sets the corresponding matrix element at the given row and column. This has a pointer receiver because it mutates the matrix.
This method is garbage-in garbage-out. For instance, on a Mat4 asking for Set(5,0,val) will work just like Set(1,1,val). Or it may panic if it's out of bounds.
func (Mat4) Sub ¶
Sub performs an element-wise subtraction of two matrices, this is equivalent to iterating over every element of m1 and subtracting the corresponding value of m2.
func (Mat4) Trace ¶
Trace is a basic operation on a square matrix that simply sums up all elements on the main diagonal (meaning all elements such that row==col).
type Transform ¶
type Transform struct { Translate Vec2 // Translation offsets. Scale Vec2 // Scale factors. Rotate float32 // Rotation angle in radians. }
Transform defines transformation state.
func (*Transform) ComputeModel ¶
ComputeModel computes and returns a model matrix from the transform state.
type Vec2 ¶
type Vec2 [2]float32
Vec2 defines a vector.
func (Vec2) Add ¶
Add performs element-wise addition between two vectors. It is equivalent to iterating over every element of v1 and adding the corresponding element of v2 to it.
func (Vec2) ApproxEqual ¶
ApproxEqual takes in a vector and does an element-wise approximate float comparison as if FloatEqual had been used
func (Vec2) ApproxEqualThreshold ¶
ApproxEqualThreshold takes in a threshold for comparing two floats, and uses it to do an element-wise comparison of the vector to another.
func (Vec2) ApproxFuncEqual ¶
ApproxFuncEqual takes in a func that compares two floats, and uses it to do an element-wise comparison of the vector to another. This is intended to be used with FloatEqualFunc
func (Vec2) Dot ¶
Dot returns the dot product of this vector with another. There are multiple ways to describe this value. One is the multiplication of their lengths and cos(theta) where theta is the angle between the vectors: v1.v2 = |v1||v2|cos(theta).
The other (and what is actually done) is the sum of the element-wise multiplication of all elements. So for instance, two Vec3s would yield v1.x * v2.x + v1.y * v2.y + v1.z * v2.z.
This means that the dot product of a vector and itself is the square of its Len (within the bounds of floating points error).
The dot product is roughly a measure of how closely two vectors are to pointing in the same direction. If both vectors are normalized, the value will be -1 for opposite pointing, one for same pointing, and 0 for perpendicular vectors.
func (Vec2) Len ¶
Len returns the vector's length. Note that this is NOT the dimension of the vector (len(v)), but the mathematical length. This is equivalent to the square root of the sum of the squares of all elements. E.G. for a Vec2 it's math.Hypot(v[0], v[1]).
func (Vec2) LenSqr ¶
LenSqr returns the vector's square length. This is equivalent to the sum of the squares of all elements.
func (Vec2) MulScalar ¶
MulScalar performs a scalar multiplication between the vector and some constant value c. This is equivalent to iterating over every vector element and multiplying by c.
func (Vec2) Normalize ¶
Normalize normalizes the vector. Normalization is (1/|v|)*v, making this equivalent to v.Scale(1/v.Len()). If the len is 0.0, this function will return an infinite value for all elements due to how floating point division works in Go (n/0.0 = math.Inf(Sign(n))).
Normalization makes a vector's Len become 1.0 (within the margin of floating point error), while maintaining its directionality.
(Can be seen here: http://play.golang.org/p/Aaj7SnbqIp )
func (Vec2) Sub ¶
Sub performs element-wise subtraction between two vectors. It is equivalent to iterating over every element of v1 and subtracting the corresponding element of v2 from it.
func (Vec2) X ¶
X is an element access func, it is equivalent to v[n] where n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks show that this is more or less as fast as direct acces, probably due to inlining, so use v[0] or v.X() depending on personal preference.
type Vec3 ¶
type Vec3 [3]float32
Vec3 defines a vector.
func Project ¶
func Project(obj Vec3, modelview, projection Mat4, initialX, initialY, width, height int) (win Vec3)
Project transforms a set of coordinates from object space (in obj) to window coordinates (with depth).
Window coordinates are continuous, not discrete (well, as continuous as an IEEE Floating Point can be), so you won't get exact pixel locations without rounding or similar
func TransformCoordinate ¶
TransformCoordinate multiplies a 3D vector by a transformation given by the homogeneous 4D matrix m, applying any translation. If this transformation is non-affine, it will project this vector onto the plane w=1 before returning the result.
This is similar to saying you're transforming and projecting a point.
This is effectively equivalent to the GLSL code
vec4 r = (m * vec4(v,1.)); r = r/r.w; vec3 newV = r.xyz;
func TransformNormal ¶
TransformNormal multiplies a 3D vector by a transformation given by the homogeneous 4D matrix m, NOT applying any translations.
This is similar to saying you're applying a transformation to a direction or normal. Rotation still applies (as does scaling), but translating a direction or normal is meaningless.
This is effectively equivalent to the GLSL code
vec4 r = (m * vec4(v,0.)); vec3 newV = r.xyz
func UnProject ¶
func UnProject(win Vec3, modelview, projection Mat4, initialX, initialY, width, height int) (obj Vec3, err error)
UnProject transforms a set of window coordinates to object space. If your MVP (projection.Mul(modelview) matrix is not invertible, this will return an error.
Note that the projection may not be perfect if you use strict pixel locations rather than the exact values given by Projectf. (It's still unlikely to be perfect due to precision errors, but it will be closer)
func (Vec3) Add ¶
Add performs element-wise addition between two vectors. It is equivalent to iterating over every element of v1 and adding the corresponding element of v2 to it.
func (Vec3) ApproxEqual ¶
ApproxEqual takes in a vector and does an element-wise approximate float comparison as if FloatEqual had been used
func (Vec3) ApproxEqualThreshold ¶
ApproxEqualThreshold takes in a threshold for comparing two floats, and uses it to do an element-wise comparison of the vector to another.
func (Vec3) ApproxFuncEqual ¶
ApproxFuncEqual takes in a func that compares two floats, and uses it to do an element-wise comparison of the vector to another. This is intended to be used with FloatEqualFunc
func (Vec3) Cross ¶
Cross is the vector cross product. This operation is only defined on 3D vectors. It is equivalent to Vec3{v1[1]*v2[2]-v1[2]*v2[1], v1[2]*v2[0]-v1[0]*v2[2], v1[0]*v2[1] - v1[1]*v2[0]}. Another interpretation is that it's the vector whose magnitude is |v1||v2|sin(theta) where theta is the angle between v1 and v2.
The cross product is most often used for finding surface normals. The cross product of vectors will generate a vector that is perpendicular to the plane they form.
Technically, a generalized cross product exists as an "(N-1)ary" operation (that is, the 4D cross product requires 3 4D vectors). But the binary 3D (and 7D) cross product is the most important. It can be considered the area of a parallelogram with sides v1 and v2.
Like the dot product, the cross product is roughly a measure of directionality. Two normalized perpendicular vectors will return a vector with a magnitude of 1.0 or -1.0 and two parallel vectors will return a vector with magnitude 0.0. The cross product is "anticommutative" meaning v1.Cross(v2) = -v2.Cross(v1), this property can be useful to know when finding normals, as taking the wrong cross product can lead to the opposite normal of the one you want.
func (Vec3) Dot ¶
Dot returns the dot product of this vector with another. There are multiple ways to describe this value. One is the multiplication of their lengths and cos(theta) where theta is the angle between the vectors: v1.v2 = |v1||v2|cos(theta).
The other (and what is actually done) is the sum of the element-wise multiplication of all elements. So for instance, two Vec3s would yield v1.x * v2.x + v1.y * v2.y + v1.z * v2.z.
This means that the dot product of a vector and itself is the square of its Len (within the bounds of floating points error).
The dot product is roughly a measure of how closely two vectors are to pointing in the same direction. If both vectors are normalized, the value will be -1 for opposite pointing, one for same pointing, and 0 for perpendicular vectors.
func (Vec3) Len ¶
Len returns the vector's length. Note that this is NOT the dimension of the vector (len(v)), but the mathematical length. This is equivalent to the square root of the sum of the squares of all elements. E.G. for a Vec2 it's math.Hypot(v[0], v[1]).
func (Vec3) LenSqr ¶
LenSqr returns the vector's square length. This is equivalent to the sum of the squares of all elements.
func (Vec3) MulScalar ¶
MulScalar performs a scalar multiplication between the vector and some constant value c. This is equivalent to iterating over every vector element and multiplying by c.
func (Vec3) Normalize ¶
Normalize normalizes the vector. Normalization is (1/|v|)*v, making this equivalent to v.Scale(1/v.Len()). If the len is 0.0, this function will return an infinite value for all elements due to how floating point division works in Go (n/0.0 = math.Inf(Sign(n))).
Normalization makes a vector's Len become 1.0 (within the margin of floating point error), while maintaining its directionality.
(Can be seen here: http://play.golang.org/p/Aaj7SnbqIp )
func (Vec3) Sub ¶
Sub performs element-wise subtraction between two vectors. It is equivalent to iterating over every element of v1 and subtracting the corresponding element of v2 from it.
func (Vec3) X ¶
X is an element access func, it is equivalent to v[n] where n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks show that this is more or less as fast as direct acces, probably due to inlining, so use v[0] or v.X() depending on personal preference.
func (Vec3) Y ¶
Y is an element access func, it is equivalent to v[n] where n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks show that this is more or less as fast as direct acces, probably due to inlining, so use v[0] or v.X() depending on personal preference.
type Vec4 ¶
type Vec4 [4]float32
Vec4 defines a vector.
func (Vec4) Add ¶
Add performs element-wise addition between two vectors. It is equivalent to iterating over every element of v1 and adding the corresponding element of v2 to it.
func (Vec4) ApproxEqual ¶
ApproxEqual takes in a vector and does an element-wise approximate float comparison as if FloatEqual had been used
func (Vec4) ApproxEqualThreshold ¶
ApproxEqualThreshold takes in a threshold for comparing two floats, and uses it to do an element-wise comparison of the vector to another.
func (Vec4) ApproxFuncEqual ¶
ApproxFuncEqual takes in a func that compares two floats, and uses it to do an element-wise comparison of the vector to another. This is intended to be used with FloatEqualFunc
func (Vec4) Dot ¶
Dot returns the dot product of this vector with another. There are multiple ways to describe this value. One is the multiplication of their lengths and cos(theta) where theta is the angle between the vectors: v1.v2 = |v1||v2|cos(theta).
The other (and what is actually done) is the sum of the element-wise multiplication of all elements. So for instance, two Vec3s would yield v1.x * v2.x + v1.y * v2.y + v1.z * v2.z.
This means that the dot product of a vector and itself is the square of its Len (within the bounds of floating points error).
The dot product is roughly a measure of how closely two vectors are to pointing in the same direction. If both vectors are normalized, the value will be -1 for opposite pointing, one for same pointing, and 0 for perpendicular vectors.
func (Vec4) Len ¶
Len returns the vector's length. Note that this is NOT the dimension of the vector (len(v)), but the mathematical length. This is equivalent to the square root of the sum of the squares of all elements. E.G. for a Vec2 it's math.Hypot(v[0], v[1]).
func (Vec4) LenSqr ¶
LenSqr returns the vector's square length. This is equivalent to the sum of the squares of all elements.
func (Vec4) MulScalar ¶
MulScalar performs a scalar multiplication between the vector and some constant value c. This is equivalent to iterating over every vector element and multiplying by c.
func (Vec4) Normalize ¶
Normalize normalizes the vector. Normalization is (1/|v|)*v, making this equivalent to v.Scale(1/v.Len()). If the len is 0.0, this function will return an infinite value for all elements due to how floating point division works in Go (n/0.0 = math.Inf(Sign(n))).
Normalization makes a vector's Len become 1.0 (within the margin of floating point error), while maintaining its directionality.
(Can be seen here: http://play.golang.org/p/Aaj7SnbqIp )
func (Vec4) OuterProd4 ¶
OuterProd4 does the vector outer product of two vectors. The outer product produces an 4x4 matrix. E.G. a Vec4 * Vec4 = Mat4.
The outer product can be thought of as the "opposite" of the Dot product. The Dot product treats both vectors like matrices oriented such that the left one has N columns and the right has N rows. So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
The outer product orients it so they're facing "outward": Vec2*Vec3 = Mat2x1*Mat1x3 = Mat2x3.
func (Vec4) Sub ¶
Sub performs element-wise subtraction between two vectors. It is equivalent to iterating over every element of v1 and subtracting the corresponding element of v2 from it.
func (Vec4) W ¶
W is an element access func, it is equivalent to v[n] where n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks show that this is more or less as fast as direct acces, probably due to inlining, so use v[0] or v.X() depending on personal preference.
func (Vec4) X ¶
X is an element access func, it is equivalent to v[n] where n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks show that this is more or less as fast as direct acces, probably due to inlining, so use v[0] or v.X() depending on personal preference.
func (Vec4) Y ¶
Y is an element access func, it is equivalent to v[n] where n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks show that this is more or less as fast as direct acces, probably due to inlining, so use v[0] or v.X() depending on personal preference.