math

package
v0.0.2-0...-24379f4 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2022 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package math implements basic math functions which operate directly on float32 numbers without casting and contains types of common entities used in 3D Graphics such as vectors, matrices, quaternions and others.

Index

Constants

View Source
const (
	Pi         = 3.14159265358979323846264338327950288419716939937510582097494459
	HalfPi     = Pi * 0.5
	TwoPi      = Pi * 2
	MaxInt32   = math.MaxInt32
	MaxInt64   = math.MaxInt64
	MaxFloat32 = math.MaxFloat32
	MaxFloat64 = math.MaxFloat64
	MaxUint32  = math.MaxUint32
)

Equivalents to the standard math package.

View Source
const (
	// Epsilon is a default epsilon value for computation.
	Epsilon = 1e-7
)

Variables

View Source
var (
	IsNaN = math.IsNaN
)

Functions

func Abs

func Abs[T Float](x T) T

Abs returns the absolute value of x.

Special cases are:

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

func Acos

func Acos[T Float](x T) T

Acos returns the arccosine, in radians, of x.

Special case is:

Acos(x) = NaN if x < -1 or x > 1

func ApproxEq

func ApproxEq[T Float](v1, v2, epsilon T) bool

ApproxEq compares v1 and v2 approximately.

func ApproxLess

func ApproxLess[T Float](v1, v2, epsilon T) bool

ApproxLess compares whether v1 is less than v2 (v1 < v2) approximately.

func Atan

func Atan[T Float](x T) T

Atan returns the arctangent, in radians, of x.

Special cases are:

Atan(±0) = ±0
Atan(±Inf) = ±Pi/2

func Atan2

func Atan2[T Float](y, x T) T

Atan2 returns the arc tangent of y/x, using the signs of the two to determine the quadrant of the return value.

Special cases are (in order):

Atan2(y, NaN) = NaN
Atan2(NaN, x) = NaN
Atan2(+0, x>=0) = +0
Atan2(-0, x>=0) = -0
Atan2(+0, x<=-0) = +Pi
Atan2(-0, x<=-0) = -Pi
Atan2(y>0, 0) = +Pi/2
Atan2(y<0, 0) = -Pi/2
Atan2(+Inf, +Inf) = +Pi/4
Atan2(-Inf, +Inf) = -Pi/4
Atan2(+Inf, -Inf) = 3Pi/4
Atan2(-Inf, -Inf) = -3Pi/4
Atan2(y, +Inf) = 0
Atan2(y>0, -Inf) = +Pi
Atan2(y<0, -Inf) = -Pi
Atan2(+Inf, x) = +Pi/2
Atan2(-Inf, x) = -Pi/2

func Barycoord

func Barycoord[T Float](p, t1, t2, t3 Vec2[T]) [3]T

Barycoord computes the barycentric coordinates of a given position regards to the given three positions.

func Ceil

func Ceil[T Float](x T) T

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[T ~uint | ~uint8 | ~uint32 | ~uint64 | ~int | ~int32 | ~int64 | Float](n, min, max T) T

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

func Cos

func Cos[T Float](x T) T

Cos returns the cosine of the radian argument x.

Special cases are:

Cos(±Inf) = NaN
Cos(NaN) = NaN

func DegToRad

func DegToRad[T Float](deg T) T

DegToRad converts a number from degrees to radians

func FMA

func FMA[T Float](x, y, z T) T

FMA returns x * y + z, computed with only one rounding. (That is, FMA returns the fused multiply-add of x, y, and z.)

func Floor

func Floor[T Float](x T) T

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 Inf

func Inf(sign int) float32

Inf returns positive infinity if sign >= 0, negative infinity if sign < 0.

func IsInsideTriangle

func IsInsideTriangle[T Float](p, v1, v2, v3 Vec2[T]) bool

IsInsideTriangle tests three given vertices and a position p, returns true if p is inside the three vertices, or false otherwise.

func Lerp

func Lerp[T Float](from, to, t T) T

Lerp computes a linear interpolation between two given numbers regarding the given t parameter.

func LerpC

func LerpC[T Float](from color.RGBA, to color.RGBA, t T) color.RGBA

LerpC computes a linear interpolation between two given colors regarding the given t parameter.

func LerpInt

func LerpInt(from, to int, t float32) int

Lerp computes a linear interpolation between two given integers regarding the given t parameter.

func Log2

func Log2[T Float](x T) T

Log2 returns the binary logarithm of x. The special cases are the same as for Log.

func Max

func Max[T Float](xs ...T) T

Max compares n values and returns the maximum

func Min

func Min[T Float](xs ...T) T

Min compares n values and returns the minimum

func Modf

func Modf[T Float](f T) (T, T)

Modf returns integer and fractional floating-point numbers that sum to f. Both values have the same sign as f.

Special cases are:

Modf(±Inf) = ±Inf, NaN
Modf(NaN) = NaN, NaN

func Pow

func Pow[T Float](x, y T) T

Pow returns x**y, the base-x exponential of y.

Special cases are (in order):

Pow(x, ±0) = 1 for any x
Pow(1, y) = 1 for any y
Pow(x, 1) = x for any x
Pow(NaN, y) = NaN
Pow(x, NaN) = NaN
Pow(±0, y) = ±Inf for y an odd integer < 0
Pow(±0, -Inf) = +Inf
Pow(±0, +Inf) = +0
Pow(±0, y) = +Inf for finite y < 0 and not an odd integer
Pow(±0, y) = ±0 for y an odd integer > 0
Pow(±0, y) = +0 for finite y > 0 and not an odd integer
Pow(-1, ±Inf) = 1
Pow(x, +Inf) = +Inf for |x| > 1
Pow(x, -Inf) = +0 for |x| > 1
Pow(x, +Inf) = +0 for |x| < 1
Pow(x, -Inf) = +Inf for |x| < 1
Pow(+Inf, y) = +Inf for y > 0
Pow(+Inf, y) = +0 for y < 0
Pow(-Inf, y) = Pow(-0, -y)
Pow(x, y) = NaN for finite x < 0 and finite non-integer y

func RadToDeg

func RadToDeg[T Float](rad T) T

RadToDeg converts a number from radians to degrees

func Round

func Round[T Float](x T) T

Round returns the nearest integer, rounding half away from zero.

Special cases are:

Round(±0) = ±0
Round(±Inf) = ±Inf
Round(NaN) = NaN

func Sin

func Sin[T Float](x T) T

Sin returns the sine of the radian argument x.

Special cases are:

Sin(±0) = ±0
Sin(±Inf) = NaN
Sin(NaN) = NaN

func Sqrt

func Sqrt[T Float](x T) T

Sqrt returns the square root of x.

Special cases are:

Sqrt(+Inf) = +Inf
Sqrt(±0) = ±0
Sqrt(x < 0) = NaN
Sqrt(NaN) = NaN

func Tan

func Tan[T Float](x T) T

Tan returns the tangent of the radian argument x.

Special cases are:

Tan(±0) = ±0
Tan(±Inf) = NaN
Tan(NaN) = NaN

func TypeSize

func TypeSize[T Type]() int

TypeSize returns the corresponding type size.

Types

type Float

type Float interface {
	~float32 | ~float64
}

Float is a constraint that permits any floating-point type.

type Int26_6

type Int26_6 int32

Int26_6 is a signed 26.6 fixed-point number.

The integer part ranges from -33554432 to 33554431, inclusive. The fractional part has 6 bits of precision.

For example, the number one-and-a-quarter is Int26_6(1<<6 + 1<<4).

func I

func I(i int) Int26_6

I returns the integer value i as an Int26_6.

For example, passing the integer value 2 yields Int26_6(128).

type Mat

type Mat[T Type] struct {
	Row  int
	Col  int
	Data []T
}

Mat represents a WxH matrix.

func NewMat

func NewMat[T Type](row, col int, xn ...T) Mat[T]

NewMat creates a new WxH matrix.

func NewRandMat

func NewRandMat[T Type](row, col int) Mat[T]

NewRandMat returns a random matrix.

func (Mat[T]) Add

func (m Mat[T]) Add(n Mat[T]) Mat[T]

Add adds two given matrix: m+n

func (Mat[T]) Eq

func (m Mat[T]) Eq(n Mat[T]) bool

Eq returns true if two matrices are equal.

func (Mat[T]) Get

func (m Mat[T]) Get(i, j int) T

Get gets the corresponding element at (i, j)

func (Mat[T]) Index

func (m Mat[T]) Index(i, j int) int

Index returns the element index of Data at (i, j)

func (Mat[T]) Mul

func (m Mat[T]) Mul(n Mat[T]) Mat[T]

Mul applies matrix multiplication of two given matrix, and returns the resulting matrix: r = m*n

func (Mat[T]) Set

func (m Mat[T]) Set(i, j int, v T)

Set sets the given value to matrix at (i, j)

func (Mat[T]) Sqrt

func (m Mat[T]) Sqrt() Mat[T]

Sqrt computes element wise sequare of root.

func (Mat[T]) String

func (m Mat[T]) String() string

String returns a string format of the given Mat4.

func (Mat[T]) Sub

func (m Mat[T]) Sub(n Mat[T]) Mat[T]

Sub subtracts two given matrix: m-n

func (Mat[T]) T

func (m Mat[T]) T() Mat[T]

T returns the transpose of a given matrix.

type Mat2

type Mat2[T Float] struct {
	// This is the best implementation that benefits from compiler
	// optimization, which exports all elements of a 3x4 Mat2.
	// See benchmarks at https://golang.design/research/pointer-params/.
	X00, X01 T
	X10, X11 T
}

Mat2 represents a 2x2 Mat2:

/ X00, X01 \ \ X10, X11 /

func Mat2I

func Mat2I[T Float]() Mat2[T]

Mat2I is an identity Mat2

func Mat2Zero

func Mat2Zero[T Float]() Mat2[T]

Mat2Zero is a zero Mat2

func NewMat2

func NewMat2[T Float](
	X00, X01,
	X10, X11 T) Mat2[T]

NewMat2 returns a new Mat2.

func (Mat2[T]) Add

func (m Mat2[T]) Add(n Mat2[T]) Mat2[T]

func (Mat2[T]) Det

func (m Mat2[T]) Det() T

Det computes the determinant of the Mat2

func (Mat2[T]) Eq

func (m Mat2[T]) Eq(n Mat2[T]) bool

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

func (Mat2[T]) Get

func (m Mat2[T]) Get(i, j int) T

Get gets the Mat2 elements

func (Mat2[T]) MulM

func (m Mat2[T]) MulM(n Mat2[T]) Mat2[T]

Mul implements Mat2 multiplication for two 3x3 matrices and assigns the result to this.

func (Mat2[T]) MulV

func (m Mat2[T]) MulV(v Vec2[T]) Vec2[T]

MulVec implements Mat2 vector multiplication and returns the resulting vector.

func (*Mat2[T]) Set

func (m *Mat2[T]) Set(i, j int, v T)

Set set the Mat2 elements at row i and column j

func (Mat2[T]) String

func (m Mat2[T]) String() string

String returns a string format of the given matrix.

func (Mat2[T]) Sub

func (m Mat2[T]) Sub(n Mat2[T]) Mat2[T]

func (Mat2[T]) T

func (m Mat2[T]) T() Mat2[T]

T computes the transpose Mat2 of a given Mat2

type Mat3

type Mat3[T Float] struct {
	// This is the best implementation that benefits from compiler
	// optimization, which exports all elements of a 3x4 Mat3.
	// See benchmarks at https://golang.design/research/pointer-params/.
	X00, X01, X02 T
	X10, X11, X12 T
	X20, X21, X22 T
}

Mat3 represents a 3x3 Mat3

/ X00, X01, X02 \ | X10, X11, X12 | \ X20, X21, X22 /

func Mat3I

func Mat3I[T Float]() Mat3[T]

Mat3I is an identity Mat3

func Mat3Zero

func Mat3Zero[T Float]() Mat3[T]

Mat3Zero is a zero Mat3

func NewMat3

func NewMat3[T Float](
	X00, X01, X02,
	X10, X11, X12,
	X20, X21, X22 T) Mat3[T]

func (Mat3[T]) Add

func (m Mat3[T]) Add(n Mat3[T]) Mat3[T]

func (Mat3[T]) Det

func (m Mat3[T]) Det() T

Det computes the determinant of the given matrix.

func (Mat3[T]) Eq

func (m Mat3[T]) Eq(n Mat3[T]) bool

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

func (Mat3[T]) Get

func (m Mat3[T]) Get(i, j int) T

Get gets the Mat3 elements

func (Mat3[T]) MulM

func (m Mat3[T]) MulM(n Mat3[T]) Mat3[T]

Mul implements Mat3 multiplication for two 3x3 matrices and assigns the result to this.

func (Mat3[T]) MulV

func (m Mat3[T]) MulV(v Vec3[T]) Vec3[T]

MulVec implements Mat3[T] vector multiplication and returns the resulting vector.

func (*Mat3[T]) Set

func (m *Mat3[T]) Set(i, j int, v T)

Set set the Mat3 elements at row i and column j

func (Mat3[T]) String

func (m Mat3[T]) String() string

String returns a string format of the given Mat3.

func (Mat3[T]) Sub

func (m Mat3[T]) Sub(n Mat3[T]) Mat3[T]

func (Mat3[T]) T

func (m Mat3[T]) T() Mat3[T]

T computes the transpose Mat3[T] of a given Mat3[T]

type Mat4

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

Mat4 represents a 4x4 Mat4

/ X00, X01, X02, X03 \ | X10, X11, X12, X13 | | X20, X21, X22, X23 | \ X30, X31, X32, X33 /

func Mat4I

func Mat4I[T Float]() Mat4[T]

Mat4I is an identity Mat4

func Mat4Zero

func Mat4Zero[T Float]() Mat4[T]

Mat4Zero is a zero Mat4

func NewMat4

func NewMat4[T Float](
	X00, X01, X02, X03,
	X10, X11, X12, X13,
	X20, X21, X22, X23,
	X30, X31, X32, X33 T) Mat4[T]

func ViewportMatrix

func ViewportMatrix[T Float](w, h T) Mat4[T]

ViewportMatrix returns the viewport matrix.

func (Mat4[T]) Add

func (m Mat4[T]) Add(n Mat4[T]) Mat4[T]

func (Mat4[T]) Det

func (m Mat4[T]) Det() T

Det computes the determinant of the Mat4

func (Mat4[T]) Eq

func (m Mat4[T]) Eq(n Mat4[T]) bool

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

func (Mat4[T]) Get

func (m Mat4[T]) Get(i, j int) T

Get gets the Mat4 elements

func (Mat4[T]) Inv

func (m Mat4[T]) Inv() Mat4[T]

Inv computes the inverse Mat4 of a given Mat4

func (Mat4[T]) MulM

func (m Mat4[T]) MulM(n Mat4[T]) Mat4[T]

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

func (Mat4[T]) MulV

func (m Mat4[T]) MulV(v Vec4[T]) Vec4[T]

MulVec implements Mat4 vector multiplication and returns the resulting vector.

func (*Mat4[T]) Set

func (m *Mat4[T]) Set(i, j int, v T)

Set set the Mat4 elements at row i and column j

func (Mat4[T]) String

func (m Mat4[T]) String() string

String returns a string format of the given Mat4.

func (Mat4[T]) Sub

func (m Mat4[T]) Sub(n Mat4[T]) Mat4[T]

func (Mat4[T]) T

func (m Mat4[T]) T() Mat4[T]

T computes the transpose Mat4 of a given Mat4

type Point26_6

type Point26_6 struct {
	X, Y Int26_6
}

Point26_6 is a 26.6 fixed-point coordinate pair.

It is analogous to the image.Point type in the standard library.

func P

func P(x, y int) Point26_6

P returns the integer values x and y as a Point26_6.

For example, passing the integer values (2, -3) yields Point26_6{128, -192}.

type Quaternion

type Quaternion[T Float] struct {
	A T
	V Vec3[T]
}

func NewQuaternion

func NewQuaternion[T Float](a, b, c, d T) Quaternion[T]

func (*Quaternion[T]) Mul

func (q *Quaternion[T]) Mul(p Quaternion[T]) Quaternion[T]

func (*Quaternion[T]) ToRoMat

func (q *Quaternion[T]) ToRoMat() Mat4[T]

type Rectangle26_6

type Rectangle26_6 struct {
	Min, Max Point26_6
}

Rectangle26_6 is a 26.6 fixed-point coordinate rectangle. The Min bound is inclusive and the Max bound is exclusive. It is well-formed if Min.X <= Max.X and likewise for Y.

It is analogous to the image.Rectangle type in the standard library.

func R

func R(minX, minY, maxX, maxY int) Rectangle26_6

R returns the integer values minX, minY, maxX, maxY as a Rectangle26_6.

For example, passing the integer values (0, 1, 2, 3) yields Rectangle26_6{Point26_6{0, 64}, Point26_6{128, 192}}.

Like the image.Rect function in the standard library, the returned rectangle has minimum and maximum coordinates swapped if necessary so that it is well-formed.

type TransformContext

type TransformContext[T Float] struct {
	// contains filtered or unexported fields
}

TransformContext is a transformation context (model matrix) that accumulates applied transformation matrices. The applying order of different types of transformations is:

1. rotation, 2. scaling, and 3. translation

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

A transformation context must be reset before use.

func (*TransformContext[T]) ModelMatrix

func (ctx *TransformContext[T]) ModelMatrix() Mat4[T]

ModelMatrix returns the most recent transformation context.

func (*TransformContext[T]) ResetContext

func (ctx *TransformContext[T]) ResetContext()

ResetContext resets the transformation context.

func (*TransformContext[T]) Rotate

func (ctx *TransformContext[T]) Rotate(dir Vec3[T], angle T)

Rotate applies rotation on an arbitrary direction with an specified angle counterclockwise.

func (*TransformContext[T]) RotateX

func (ctx *TransformContext[T]) RotateX(angle T)

RotateX applies rotation on X-axis direction with an specified angle counterclockwise.

func (*TransformContext[T]) RotateY

func (ctx *TransformContext[T]) RotateY(angle T)

RotateY applies rotation on Y-axis direction with an specified angle counterclockwise.

func (*TransformContext[T]) RotateZ

func (ctx *TransformContext[T]) RotateZ(angle T)

RotateZ applies rotation on Z-axis direction with an specified angle counterclockwise.

func (*TransformContext[T]) Scale

func (ctx *TransformContext[T]) Scale(sx, sy, sz T)

Scale sets the scale matrix.

func (*TransformContext[T]) ScaleX

func (ctx *TransformContext[T]) ScaleX(sx T)

ScaleX sets the scale matrix on X-axis.

func (*TransformContext[T]) ScaleY

func (ctx *TransformContext[T]) ScaleY(sy T)

ScaleY sets the scale matrix on Y-axis.

func (*TransformContext[T]) ScaleZ

func (ctx *TransformContext[T]) ScaleZ(sz T)

ScaleZ sets the scale matrix on Z-axis.

func (*TransformContext[T]) Translate

func (ctx *TransformContext[T]) Translate(tx, ty, tz T)

Translate sets the translate matrix.

func (*TransformContext[T]) TranslateX

func (ctx *TransformContext[T]) TranslateX(tx T)

TranslateX sets the translate matrix on X-axis.

func (*TransformContext[T]) TranslateY

func (ctx *TransformContext[T]) TranslateY(ty T)

TranslateY sets the translate matrix on Y-axis.

func (*TransformContext[T]) TranslateZ

func (ctx *TransformContext[T]) TranslateZ(tz T)

TranslateZ sets the translate matrix on Z-axis.

type Type

type Type interface {
	~uint8 | ~uint32 | ~int32 | ~float32
}

Type defines all supported data types.

type Vec

type Vec[T Float] struct {
	Data []T
}

Vec is an N dimensional vector.

func NewRandVec

func NewRandVec[T Float](n int) Vec[T]

NewRandVec

func NewVec

func NewVec[T Float](data ...T) Vec[T]

NewVec creates a point or a vector with given parameters.

func (*Vec[T]) Add

func (v *Vec[T]) Add(u Vec[T]) (r Vec[T])

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

func (Vec[T]) Dot

func (v Vec[T]) Dot(u Vec[T]) T

Dot implements dot product of two vectors

func (Vec[T]) Eq

func (v Vec[T]) Eq(u Vec[T]) bool

Eq checks whether two vectors are equal.

func (Vec[T]) IsZero

func (v Vec[T]) 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 (Vec[T]) Len

func (v Vec[T]) Len() T

Len computes the length of the given Vector

func (Vec[T]) Scale

func (v Vec[T]) Scale(w T) (r Vec[T])

Scale scales the given vector using given scalars

func (Vec[T]) String

func (m Vec[T]) String() string

String returns a string format of the given Vec.

func (Vec[T]) Sub

func (v Vec[T]) Sub(u Vec[T]) (r Vec[T])

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

func (Vec[T]) Translate

func (v Vec[T]) Translate(u Vec[T]) (r Vec[T])

Translate translates the given position or vector

func (Vec[T]) Unit

func (v Vec[T]) Unit() (r Vec[T])

Unit normalizes this vector to an unit vector

type Vec2

type Vec2[T Float] struct {
	X, Y T
}

Vec2 represents a 2D vector (x, y).

func ClampVec2

func ClampVec2[T Float](v Vec2[T], min, max T) Vec2[T]

ClampVec2 clamps a Vec2 in [min, max].

func LerpVec2

func LerpVec2[T Float](from, to Vec2[T], t T) Vec2[T]

LerpV computes a linear interpolation between two given vectors regarding the given t parameter.

func NewRandVec2

func NewRandVec2[T Float]() Vec2[T]

NewRandVec2 returns a random 2D vector where all components are sitting in range [0, 1].

func NewVec2

func NewVec2[T Float](x, y T) Vec2[T]

NewVec2 creates a 2D vector with given parameters.

func (Vec2[T]) Add

func (v Vec2[T]) Add(u Vec2[T]) Vec2[T]

Add add the two given vectors, and returns the resulting vector.

func (Vec2[T]) Apply

func (v Vec2[T]) Apply(m Mat2[T]) Vec2[T]

Apply applies the 2D matrix multiplication to the given vector on the left side and returns the resulting 2D vector.

func (Vec2[T]) Dot

func (v Vec2[T]) Dot(u Vec2[T]) T

Dot computes the dot product of two given vectors.

func (Vec2[T]) Eq

func (v Vec2[T]) Eq(u Vec2[T]) bool

Eq compares the two given vectors, and returns true if they are equal.

func (Vec2[T]) IsZero

func (v Vec2[T]) IsZero() bool

IsZero checks if the given vector is a zero vector.

func (Vec2[T]) Len

func (v Vec2[T]) Len() T

Len returns the length of the given vector.

func (Vec2[T]) Scale

func (v Vec2[T]) Scale(x, y T) Vec2[T]

Scale scales the given 2D vector and returns the resulting vector.

func (Vec2[T]) String

func (m Vec2[T]) String() string

String returns a string format of the given Vec2.

func (Vec2[T]) Sub

func (v Vec2[T]) Sub(u Vec2[T]) Vec2[T]

Sub subtracts the two given vectors, and returns the resulting vector.

func (Vec2[T]) Translate

func (v Vec2[T]) Translate(x, y T) Vec2[T]

Translate translates the given 2D vector and returns the resulting vector.

func (Vec2[T]) Unit

func (v Vec2[T]) Unit() Vec2[T]

Unit computes the unit vector along the direction of the given vector.

type Vec3

type Vec3[T Float] struct {
	X, Y, Z T
}

Vec3 represents a 3D vector (x, y, z).

func ClampVec3

func ClampVec3[T Float](v Vec3[T], min, max T) Vec3[T]

ClampVec3 clamps a Vec4 in [min, max].

func LerpVec3

func LerpVec3[T Float](from, to Vec3[T], t T) Vec3[T]

LerpV computes a linear interpolation between two given vectors regarding the given t parameter.

func NewRandVec3

func NewRandVec3[T Float]() Vec3[T]

NewRandVec2 returns a random 3D vector where all components are sitting in range [0, 1].

func NewVec3

func NewVec3[T Float](x, y, z T) Vec3[T]

NewVec3 creates a 3D vector with given parameters.

func (Vec3[T]) Add

func (v Vec3[T]) Add(u Vec3[T]) Vec3[T]

Add add the two given vectors, and returns the resulting vector.

func (Vec3[T]) Apply

func (v Vec3[T]) Apply(m Mat3[T]) Vec3[T]

Apply applies the 3D matrix multiplication to the given vector on the left side and returns the resulting 3D vector.

func (Vec3[T]) Cross

func (v Vec3[T]) Cross(u Vec3[T]) Vec3[T]

Cross applies cross product of two given vectors and returns the resulting vector.

func (Vec3[T]) Dot

func (v Vec3[T]) Dot(u Vec3[T]) T

Dot computes the dot product of two given vectors.

func (Vec3[T]) Eq

func (v Vec3[T]) Eq(u Vec3[T]) bool

Eq compares the two given vectors, and returns true if they are equal.

func (Vec3[T]) IsZero

func (v Vec3[T]) IsZero() bool

IsZero checks if the given vector is a zero vector.

func (Vec3[T]) Len

func (v Vec3[T]) Len() T

Len returns the length of the given vector.

func (Vec3[T]) Less

func (v Vec3[T]) Less(u Vec3[T]) bool

Less compares whether all components of v is less than the given u.

func (Vec3[T]) Scale

func (v Vec3[T]) Scale(x, y, z T) Vec3[T]

Scale scales the given 3D vector and returns the resulting vector.

func (Vec3[T]) String

func (m Vec3[T]) String() string

String returns a string format of the given Vec3.

func (Vec3[T]) Sub

func (v Vec3[T]) Sub(u Vec3[T]) Vec3[T]

Sub subtracts the two given vectors, and returns the resulting vector.

func (Vec3[T]) ToVec4

func (v Vec3[T]) ToVec4(w T) Vec4[T]

ToVec4 converts to a Vec3 to Vec4 using the given w component.

func (Vec3[T]) Translate

func (v Vec3[T]) Translate(x, y, z T) Vec3[T]

Translate translates the given 3D vector and returns the resulting vector.

func (Vec3[T]) Unit

func (v Vec3[T]) Unit() Vec3[T]

Unit computes the unit vector along the direction of the given vector.

type Vec4

type Vec4[T Float] struct {
	X, Y, Z, W T
}

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

func ClampVec4

func ClampVec4[T Float](v Vec4[T], min, max T) Vec4[T]

ClampVec clamps a Vec4 in [min, max].

func LerpVec4

func LerpVec4[T Float](from, to Vec4[T], t T) Vec4[T]

LerpV computes a linear interpolation between two given vectors regarding the given t parameter.

func NewRandVec4

func NewRandVec4[T Float]() Vec4[T]

NewRandVec4

func NewVec4

func NewVec4[T Float](x, y, z, w T) Vec4[T]

NewVec4 creates a point or a vector with given parameters.

func (*Vec4[T]) Add

func (v *Vec4[T]) Add(u Vec4[T]) Vec4[T]

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

func (Vec4[T]) Apply

func (v Vec4[T]) Apply(m Mat4[T]) Vec4[T]

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

func (Vec4[T]) Cross

func (v Vec4[T]) Cross(u Vec4[T]) Vec4[T]

Cross applies cross product of two given vectors and returns the resulting vector.

func (Vec4[T]) Dot

func (v Vec4[T]) Dot(u Vec4[T]) T

Dot implements dot product of two vectors

func (Vec4[T]) Eq

func (v Vec4[T]) Eq(u Vec4[T]) bool

Eq checks whether two vectors are equal.

func (Vec4[T]) IsZero

func (v Vec4[T]) 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 (Vec4[T]) Len

func (v Vec4[T]) Len() T

Len computes the length of the given Vector

func (Vec4[T]) Less

func (v Vec4[T]) Less(u Vec4[T]) bool

Less compares whether all components of v is less than the given u.

func (Vec4[T]) Pos

func (v Vec4[T]) Pos() Vec4[T]

Pos converts a homogeneous represented vector to a point

func (Vec4[T]) Scale

func (v Vec4[T]) Scale(x, y, z, w T) Vec4[T]

Scale scales the given vector using given scalars

func (Vec4[T]) String

func (m Vec4[T]) String() string

String returns a string format of the given Vec4.

func (Vec4[T]) Sub

func (v Vec4[T]) Sub(u Vec4[T]) Vec4[T]

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

func (Vec4[T]) ToVec2

func (v Vec4[T]) ToVec2() Vec2[T]

ToVec2 drops the z and w components of the given Vec4[T].

func (Vec4[T]) ToVec3

func (v Vec4[T]) ToVec3() Vec3[T]

ToVec3 drops the w component of the given Vec4[T].

func (Vec4[T]) Translate

func (v Vec4[T]) Translate(x, y, z T) Vec4[T]

Translate translates the given position or vector

func (Vec4[T]) Unit

func (v Vec4[T]) Unit() Vec4[T]

Unit normalizes this vector to an unit vector

func (Vec4[T]) Vec

func (v Vec4[T]) Vec() Vec4[T]

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