math

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2021 License: GPL-3.0 Imports: 2 Imported by: 1

Documentation

Index

Constants

View Source
const DefaultEpsilon = 1e-7

DefaultEpsilon is a default epsilon value for computation.

Variables

View Source
var (
	// MatI is an identity matrix
	MatI = Matrix{
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		0, 0, 0, 1,
	}
	// MatZero is a zero matrix
	MatZero = Matrix{
		0, 0, 0, 0,
		0, 0, 0, 0,
		0, 0, 0, 0,
		0, 0, 0, 0,
	}
)
View Source
var (
	Cos        = math.Cos
	Sin        = math.Sin
	Tan        = math.Tan
	Abs        = math.Abs
	Atan       = math.Atan
	Atan2      = math.Atan2
	Pi         = math.Pi
	MaxInt64   = math.MaxInt64
	MaxFloat64 = math.MaxFloat64
	Round      = math.Round
	Floor      = math.Floor
	Log2       = math.Log2
	Pow        = math.Pow
	Sqrt       = math.Sqrt
	IsNaN      = math.IsNaN
	Modf       = math.Modf
)

Functions

func ApproxEq

func ApproxEq(v1, v2, epsilon float64) bool

ApproxEq approximately compares v1 and v2.

func Clamp

func Clamp(n, min, max float64) float64

Clamp clamps a given value in [min, max].

func Lerp

func Lerp(from float64, to float64, t float64) float64

func LerpC

func LerpC(from color.RGBA, to color.RGBA, t float64) color.RGBA

func Max

func Max(xs ...float64) float64

Max compares n values and returns the maximum

func Min

func Min(xs ...float64) float64

Min compares n values and returns the minimum

Types

type Matrix

type Matrix struct {
	// This is the best implementation that benefits from compiler
	// optimization, which exports all elements of a 4x4 Matrix.
	// See benchmarks at https://golang.design/research/pointer-params/.
	X00, X01, X02, X03 float64
	X10, X11, X12, X13 float64
	X20, X21, X22, X23 float64
	X30, X31, X32, X33 float64
}

Matrix represents a 4x4 matrix

func NewMatrix

func NewMatrix(X00, X01, X02, X03, X10, X11, X12, X13, X20, X21, X22, X23, X30, X31, X32, X33 float64) Matrix

func ViewportMatrix

func ViewportMatrix(w, h float64) Matrix

ViewportMatrix returns the viewport matrix.

func (Matrix) Add

func (m Matrix) Add(n Matrix) Matrix

func (Matrix) Det

func (m Matrix) Det() float64

Det computes the determinant of the matrix

func (Matrix) Eq

func (m Matrix) Eq(n Matrix) bool

Eq checks whether the given two matrices are equal or not.

func (Matrix) Get

func (m Matrix) Get(i, j int) float64

Get gets the matrix elements

func (Matrix) Inv

func (m Matrix) Inv() Matrix

Inv computes the inverse matrix of a given Matrix

func (Matrix) MulM

func (m Matrix) MulM(n Matrix) Matrix

Mul implements matrix multiplication for two 4x4 matrices and assigns the result to this.

func (Matrix) MulV

func (m Matrix) MulV(v Vector) Vector

MulVec implements matrix vector multiplication and returns the resulting vector.

func (Matrix) Set

func (m Matrix) Set(i, j int, v float64)

Set set the matrix elements at row i and column j

func (Matrix) Sub

func (m Matrix) Sub(n Matrix) Matrix

func (Matrix) T

func (m Matrix) T() Matrix

T computes the transpose matrix of a given Matrix

type Quaternion

type Quaternion struct {
	A float64
	V Vector
}

func NewQuaternion

func NewQuaternion(a, b, c, d float64) Quaternion

func (Quaternion) Mul

func (q Quaternion) Mul(p Quaternion) Quaternion

func (Quaternion) ToRoMat

func (q Quaternion) ToRoMat() Matrix

type TransformContext

type TransformContext struct {
	// contains filtered or unexported fields
}

TransformContext is a transformation context (model matrix) that accumulates applied transformation matrices (multiplied from left side).

context is a persistant status for the given mesh and can be reused for each of the rendering frame unless the mesh intentionally calls ResetContext() method.

func (*TransformContext) ModelMatrix

func (ctx *TransformContext) ModelMatrix() Matrix

func (*TransformContext) ResetContext

func (ctx *TransformContext) ResetContext()

func (*TransformContext) Rotate

func (ctx *TransformContext) Rotate(dir Vector, angle float64)

func (*TransformContext) RotateX

func (ctx *TransformContext) RotateX(angle float64)

func (*TransformContext) RotateY

func (ctx *TransformContext) RotateY(angle float64)

func (*TransformContext) RotateZ

func (ctx *TransformContext) RotateZ(angle float64)

func (*TransformContext) Scale

func (ctx *TransformContext) Scale(sx, sy, sz float64)

Scale sets the scale matrix.

func (*TransformContext) Translate

func (ctx *TransformContext) Translate(tx, ty, tz float64)

SetTranslate sets the translate matrix.

type Vector

type Vector struct {
	X, Y, Z, W float64
}

Vector uses homogeneous coordinates (x, y, z, w) that represents either a point or a vector.

func ClampV

func ClampV(v Vector, min, max float64) Vector

ClampV clamps a vector in [min, max].

func LerpV

func LerpV(from Vector, to Vector, t float64) Vector

func NewVector

func NewVector(x, y, z, w float64) Vector

NewVector creates a point or a vector with given parameters.

func (Vector) Add

func (v Vector) Add(u Vector) Vector

Add adds the given two vectors, or point and vector, or two points

func (Vector) Apply

func (v Vector) Apply(m Matrix) Vector

ApplyMatrix applies 4x4 matrix and 4x1 vector multiplication. the given matrix multiplies v from the left.

func (Vector) Cross

func (v Vector) Cross(u Vector) Vector

Cross implements cross product for two given vectors and assign the result to this.

func (Vector) Dot

func (v Vector) Dot(u Vector) float64

Dot implements dot product of two vectors

func (Vector) Eq

func (v Vector) Eq(u Vector) bool

Eq checks whether two vectors are equal.

func (Vector) IsZero

func (v Vector) IsZero() bool

IsZero asserts the x, y, z components of the given vector, and returns true if it is a zero vector or point.

func (Vector) Len

func (v Vector) Len() float64

Len computes the length of the given Vector

func (Vector) Pos

func (v Vector) Pos() Vector

Pos converts a homogeneous represented vector to a point

func (Vector) Scale

func (v Vector) Scale(x, y, z, w float64) Vector

Scale scales the given vector using given scalars

func (Vector) Sub

func (v Vector) Sub(u Vector) Vector

Sub subtracts the given two vectors, or point and vector, or two points

func (Vector) Translate

func (v Vector) Translate(x, y, z float64) Vector

Translate translates the given position or vector

func (Vector) Unit

func (v Vector) Unit() Vector

Unit normalizes this vector to an unit vector

func (Vector) Vec

func (v Vector) Vec() Vector

Vec converts the a homogeneous represented point to a vector

Jump to

Keyboard shortcuts

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