math

package module
v0.0.0-...-8caa130 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2020 License: BSD-3-Clause Imports: 5 Imported by: 0

README

Math

This package implements a host of 32-bit math functions and types, related to OpenGL programs.

Some of it wraps Go's math package. Some of it borrows directly from go-gl/mathgl

License

Unless otherwise states, this code is provided under a 3-close BSD license. Its contents can be found in the LICENSE file.

Documentation

Overview

Package math implements some custom math utilities, along with 32 bit wrappers for some Go math functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(v float32) float32

Abs returns the absolute value of x.

Special cases are:

Abs(±Inf) = +Inf
Abs(NaN) = NaN

func Ceil

func Ceil(v float32) float32

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 Clamp

func Clamp(x, min, max float32) float32

Clamp returns x, clamped to the range [min,max].

func Extract3DScale

func Extract3DScale(m Mat4) (x, y, z float32)

Extract3DScale extracts the 3d scaling from a homogeneous matrix

func ExtractMaxScale

func ExtractMaxScale(m Mat4) float32

ExtractMaxScale extracts the maximum scaling from a homogeneous matrix

func FloatEqual

func FloatEqual(a, b float32) bool

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

func FloatEqualFunc(epsilon float32) func(float32, float32) bool

FloatEqualFunc is a utility closure that will generate a function that always approximately compares floats like FloatEqualThreshold with a different threshold.

func FloatEqualThreshold

func FloatEqualThreshold(a, b, epsilon float32) bool

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

func Floor(v float32) float32

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 IsPow2

func IsPow2(n int) bool

IsPow2 returns true if the given value is a power-of-two.

func Pow2

func Pow2(n int) int

Pow2 returns the first power-of-two value >= to n. This can be used to create suitable texture dimensions.

func Remainder

func Remainder(a, b float32) float32

Remainder returns the IEEE 754 floating-point remainder of x/y.

Special cases are:

Remainder(±Inf, y) = NaN
Remainder(NaN, y) = NaN
Remainder(x, 0) = NaN
Remainder(x, ±Inf) = x
Remainder(x, 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 Frustum

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

Frustum generates a Frustum Matrix.

func HomogRotate3D

func HomogRotate3D(angle float32, axis Vec3) Mat4

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

func HomogRotate3DX(angle float32) Mat4

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

func HomogRotate3DY(angle float32) Mat4

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

func HomogRotate3DZ(angle float32) Mat4

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 LookAt

func LookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ float32) Mat4

LookAt generates a transform matrix from world space to the given eye space.

func LookAtV

func LookAtV(eye, center, up Vec3) Mat4

LookAtV generates a transform matrix from world space into the specific eye space.

func Ortho

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

Ortho generates an Ortho Matrix.

func Ortho2D

func Ortho2D(left, right, bottom, top float32) Mat4

Ortho2D is equivalent to Ortho with the near and far planes being -1 and 1, respectively.

func Perspective

func Perspective(fovy, aspect, near, far float32) Mat4

Perspective generates a Perspective Matrix.

func Scale3D

func Scale3D(scaleX, scaleY, scaleZ float32) Mat4

Scale3D creates a homogeneous 3D scaling matrix [[ scaleX, 0 , 0 , 0 ]] [[ 0 , scaleY, 0 , 0 ]] [[ 0 , 0 , scaleZ, 0 ]] [[ 0 , 0 , 0 , 1 ]]

func ShearX3D

func ShearX3D(shearY, shearZ float32) Mat4

ShearX3D creates a homogeneous 3D shear matrix along the X-axis

func ShearY3D

func ShearY3D(shearX, shearZ float32) Mat4

ShearY3D creates a homogeneous 3D shear matrix along the Y-axis

func ShearZ3D

func ShearZ3D(shearX, shearY float32) Mat4

ShearZ3D creates a homogeneous 3D shear matrix along the Z-axis

func Translate3D

func Translate3D(Tx, Ty, Tz float32) Mat4

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

func (m1 Mat4) Abs() Mat4

Abs returns the element-wise absolute value of this matrix

func (Mat4) Add

func (m1 Mat4) Add(m2 Mat4) Mat4

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

func (m1 Mat4) ApproxEqual(m2 Mat4) bool

ApproxEqual performs an element-wise approximate equality test between two matrices, as if FloatEqual had been used.

func (Mat4) ApproxEqualThreshold

func (m1 Mat4) ApproxEqualThreshold(m2 Mat4, threshold float32) bool

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

func (m1 Mat4) ApproxFuncEqual(m2 Mat4, eq func(float32, float32) bool) bool

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

func (m1 Mat4) At(row, col int) float32

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

func (m1 Mat4) Col(col int) Vec4

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

func (m1 Mat4) Cols() (col0, col1, col2, col3 Vec4)

Cols decomposes a matrix into its corresponding column vectors. This is equivalent to calling mat.Col for each column.

func (Mat4) Det

func (m1 Mat4) Det() float32

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

func (m1 Mat4) Diag() Vec4

Diag is a basic operation on a square matrix that simply returns main diagonal (meaning all elements such that row==col).

func (Mat4) Index

func (m1 Mat4) Index(row, col int) int

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

func (m1 Mat4) Inv() Mat4

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

func (m1 Mat4) Mul(c float32) Mat4

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

func (m1 Mat4) Mul4(m2 Mat4) Mat4

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

func (m1 Mat4) Mul4x1(m2 Vec4) Vec4

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

func (m1 Mat4) Row(row int) Vec4

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

func (m1 Mat4) Rows() (row0, row1, row2, row3 Vec4)

Rows decomposes a matrix into its corresponding row vectors. This is equivalent to calling mat.Row for each row.

func (*Mat4) Set

func (m1 *Mat4) Set(row, col int, value float32)

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

func (m1 *Mat4) SetCol(col int, v Vec4)

SetCol sets a Column within the Matrix, so it mutates the calling matrix.

func (*Mat4) SetRow

func (m1 *Mat4) SetRow(row int, v Vec4)

SetRow sets a Row within the Matrix, so it mutates the calling matrix.

func (Mat4) String

func (m1 Mat4) String() string

Pretty prints the matrix

func (Mat4) Sub

func (m1 Mat4) Sub(m2 Mat4) Mat4

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

func (m1 Mat4) Trace() float32

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

func (Mat4) Transpose

func (m1 Mat4) Transpose() Mat4

Transpose produces the transpose of this matrix. For any MxN matrix the transpose is an NxM matrix with the rows swapped with the columns. For instance the transpose of the Mat3x2 is a Mat2x3 like so:

[[a b]]    [[a c e]]
[[c d]] =  [[b d f]]
[[e f]]

type Transform

type Transform struct {
	Translate Vec2    // Translation offsets.
	Scale     Vec2    // Scale factors.
	Rotate    float32 // Rotation angle in radians.
}

Transform defines transformation state.

func NewTransform

func NewTransform() *Transform

NewTransform returns a new, default transform.

func (*Transform) ComputeModel

func (t *Transform) ComputeModel() Mat4

ComputeModel computes and returns a model matrix from the transform state.

type Vec2

type Vec2 [2]float32

Vec2 defines a vector.

func (Vec2) Add

func (v1 Vec2) Add(v2 Vec2) Vec2

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

func (v1 Vec2) ApproxEqual(v2 Vec2) bool

ApproxEqual takes in a vector and does an element-wise approximate float comparison as if FloatEqual had been used

func (Vec2) ApproxEqualThreshold

func (v1 Vec2) ApproxEqualThreshold(v2 Vec2, threshold float32) bool

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

func (v1 Vec2) ApproxFuncEqual(v2 Vec2, eq func(float32, float32) bool) bool

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

func (v Vec2) Clamp(min, max float32) Vec2

Clamp returns v, with its components clamped to the range [min,max].

func (Vec2) Div

func (a Vec2) Div(b Vec2) Vec2

Div returns a / b.

func (Vec2) DivFloor

func (a Vec2) DivFloor(b Vec2) Vec2

DivFloor returns floor(a / b).

func (Vec2) DivScalar

func (v Vec2) DivScalar(f float32) Vec2

DivScalar returns v / f.

func (Vec2) Dot

func (v1 Vec2) Dot(v2 Vec2) float32

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

func (v Vec2) Elem() (x, y float32)

Elem extracts the elements of the vector for direct value assignment.

func (Vec2) Len

func (v1 Vec2) Len() float32

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

func (v1 Vec2) LenSqr() float32

LenSqr returns the vector's square length. This is equivalent to the sum of the squares of all elements.

func (Vec2) Mul

func (a Vec2) Mul(b Vec2) Vec2

Mul returns a * b.

func (Vec2) MulScalar

func (v1 Vec2) MulScalar(c float32) Vec2

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

func (v1 Vec2) Normalize() Vec2

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

func (a Vec2) Remainder(b Vec2) Vec2

Remainder returns a % b.

func (Vec2) Sub

func (v1 Vec2) Sub(v2 Vec2) Vec2

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

func (v Vec2) Vec3(z float32) Vec3

Vec3 constructs a 3-dimensional vector by appending the given coordinates.

func (Vec2) Vec4

func (v Vec2) Vec4(z, w float32) Vec4

Vec4 constructs a 4-dimensional vector by appending the given coordinates.

func (Vec2) X

func (v Vec2) X() float32

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 (Vec2) Y

func (v Vec2) Y() float32

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

func TransformCoordinate(v Vec3, m Mat4) Vec3

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

func TransformNormal(v Vec3, m Mat4) Vec3

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

func (v1 Vec3) Add(v2 Vec3) Vec3

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

func (v1 Vec3) ApproxEqual(v2 Vec3) bool

ApproxEqual takes in a vector and does an element-wise approximate float comparison as if FloatEqual had been used

func (Vec3) ApproxEqualThreshold

func (v1 Vec3) ApproxEqualThreshold(v2 Vec3, threshold float32) bool

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

func (v1 Vec3) ApproxFuncEqual(v2 Vec3, eq func(float32, float32) bool) bool

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

func (v Vec3) Clamp(min, max float32) Vec3

Clamp returns v, with its components clamped to the range [min,max].

func (Vec3) Cross

func (v1 Vec3) Cross(v2 Vec3) Vec3

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

func (a Vec3) Div(b Vec3) Vec3

Div returns a / b.

func (Vec3) DivFloor

func (a Vec3) DivFloor(b Vec3) Vec3

DivFloor returns floor(a / b).

func (Vec3) DivScalar

func (v Vec3) DivScalar(f float32) Vec3

DivScalar returns v / f.

func (Vec3) Dot

func (v1 Vec3) Dot(v2 Vec3) float32

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

func (v Vec3) Elem() (x, y, z float32)

Elem extracts the elements of the vector for direct value assignment.

func (Vec3) Len

func (v1 Vec3) Len() float32

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

func (v1 Vec3) LenSqr() float32

LenSqr returns the vector's square length. This is equivalent to the sum of the squares of all elements.

func (Vec3) Mul

func (a Vec3) Mul(b Vec4) Vec3

Mul returns a * b.

func (Vec3) MulScalar

func (v1 Vec3) MulScalar(c float32) Vec3

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

func (v1 Vec3) Normalize() Vec3

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

func (a Vec3) Remainder(b Vec3) Vec3

Remainder returns a % b.

func (Vec3) Sub

func (v1 Vec3) Sub(v2 Vec3) Vec3

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

func (v Vec3) Vec2() Vec2

Vec2 constructs a 2-dimensional vector by discarding coordinates.

func (Vec3) Vec4

func (v Vec3) Vec4(w float32) Vec4

Vec4 constructs a 4-dimensional vector by appending the given coordinates.

func (Vec3) X

func (v Vec3) X() float32

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

func (v Vec3) Y() float32

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.

func (Vec3) Z

func (v Vec3) Z() float32

Z 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

func (v1 Vec4) Add(v2 Vec4) Vec4

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

func (v1 Vec4) ApproxEqual(v2 Vec4) bool

ApproxEqual takes in a vector and does an element-wise approximate float comparison as if FloatEqual had been used

func (Vec4) ApproxEqualThreshold

func (v1 Vec4) ApproxEqualThreshold(v2 Vec4, threshold float32) bool

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

func (v1 Vec4) ApproxFuncEqual(v2 Vec4, eq func(float32, float32) bool) bool

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

func (v Vec4) Clamp(min, max float32) Vec4

Clamp returns v, with its components clamped to the range [min,max].

func (Vec4) Div

func (a Vec4) Div(b Vec4) Vec4

Div returns a / b.

func (Vec4) DivFloor

func (a Vec4) DivFloor(b Vec4) Vec4

DivFloor returns floor(a / b).

func (Vec4) DivScalar

func (v Vec4) DivScalar(f float32) Vec4

DivScalar returns v / f.

func (Vec4) Dot

func (v1 Vec4) Dot(v2 Vec4) float32

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

func (v Vec4) Elem() (x, y, z, w float32)

Elem extracts the elements of the vector for direct value assignment.

func (Vec4) Len

func (v1 Vec4) Len() float32

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

func (v1 Vec4) LenSqr() float32

LenSqr returns the vector's square length. This is equivalent to the sum of the squares of all elements.

func (Vec4) Mul

func (a Vec4) Mul(b Vec4) Vec4

Mul returns a * b.

func (Vec4) MulScalar

func (v1 Vec4) MulScalar(c float32) Vec4

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

func (v1 Vec4) Normalize() Vec4

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

func (v1 Vec4) OuterProd4(v2 Vec4) Mat4

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

func (a Vec4) Remainder(b Vec4) Vec4

Remainder returns a % b.

func (Vec4) Sub

func (v1 Vec4) Sub(v2 Vec4) Vec4

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

func (v Vec4) Vec2() Vec2

Vec2 constructs a 2-dimensional vector by discarding coordinates.

func (Vec4) Vec3

func (v Vec4) Vec3() Vec3

Vec3 constructs a 3-dimensional vector by discarding coordinates.

func (Vec4) W

func (v Vec4) W() float32

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

func (v Vec4) X() float32

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

func (v Vec4) Y() float32

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.

func (Vec4) Z

func (v Vec4) Z() float32

Z 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.

Jump to

Keyboard shortcuts

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