vec3

package
v0.0.0-...-1bde132 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2025 License: MIT Imports: 3 Imported by: 44

Documentation

Overview

Package vec3 contains a 3D float64 vector type T and functions.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Zero holds a zero vector.
	Zero = T{}

	// UnitX holds a vector with X set to one.
	UnitX = T{1, 0, 0}
	// UnitY holds a vector with Y set to one.
	UnitY = T{0, 1, 0}
	// UnitZ holds a vector with Z set to one.
	UnitZ = T{0, 0, 1}
	// UnitXYZ holds a vector with X, Y, Z set to one.
	UnitXYZ = T{1, 1, 1}

	// Red holds the color red.
	Red = T{1, 0, 0}
	// Green holds the color green.
	Green = T{0, 1, 0}
	// Blue holds the color blue.
	Blue = T{0, 0, 1}
	// Black holds the color black.
	Black = T{0, 0, 0}
	// White holds the color white.
	White = T{1, 1, 1}

	// MinVal holds a vector with the smallest possible component values.
	MinVal = T{-math.MaxFloat64, -math.MaxFloat64, -math.MaxFloat64}
	// MaxVal holds a vector with the highest possible component values.
	MaxVal = T{+math.MaxFloat64, +math.MaxFloat64, +math.MaxFloat64}
)
View Source
var Epsilon float64 = 1e-14

Epsilon is the tolerance used for numerical stability in floating-point comparisons. It is used by Normalize(), Normal(), and other methods to determine if a vector is effectively zero or already normalized. Can be adjusted for specific use cases. Default: 1e-14 for float64 precision.

View Source
var (
	// MaxBox holds a box that contains the entire R3 space that can be represented as vec3
	MaxBox = Box{MinVal, MaxVal}
)

Functions

func Angle

func Angle(a, b *T) float64

Angle returns the angle value of the (shortest/smallest) angle between the two vectors a and b. The returned value is in the range 0 ≤ angle ≤ Pi radians.

func Cosine

func Cosine(a, b *T) float64

Cosine returns the cosine value of the angle between the two vectors. The returned cosine value is in the range -1.0 ≤ value ≤ 1.0.

func Distance

func Distance(a, b *T) float64

Distance the distance between two vectors

func Dot

func Dot(a, b *T) float64

Dot returns the dot product of two vectors.

func PracticallyEquals

func PracticallyEquals(v1, v2, allowedDelta float64) bool

PracticallyEquals compares two values if they are equal with each other within a delta tolerance.

func Sinus

func Sinus(a, b *T) float64

Sinus returns the sinus value of the (shortest/smallest) angle between the two vectors a and b. The returned sine value is in the range 0.0 ≤ value ≤ 1.0. The angle is always considered to be in the range 0 to Pi radians and thus the sine value returned is always positive.

func SquareDistance

func SquareDistance(a, b *T) float64

SquareDistance the distance between two vectors squared (= distance*distance)

Types

type Box

type Box struct {
	Min T
	Max T
}

Box is a coordinate system aligned 3D box defined by a Min and Max vector.

func Joined

func Joined(a, b *Box) Box

Joined returns the minimal box containing both a and b.

func ParseBox

func ParseBox(s string) (r Box, err error)

ParseBox parses a Box from a string. See also String()

func (*Box) Center

func (box *Box) Center() T

func (*Box) ContainsPoint

func (box *Box) ContainsPoint(p *T) bool

ContainsPoint returns if a point is contained within the box.

func (*Box) Diagonal

func (box *Box) Diagonal() T

func (*Box) Intersects

func (box *Box) Intersects(other *Box) bool

Intersects returns true if this and the given box intersect. For an explanation of the algorithm, see http://rbrundritt.wordpress.com/2009/10/03/determining-if-two-bounding-boxes-overlap/

func (*Box) Join

func (box *Box) Join(other *Box)

Join enlarges this box to contain also the given box.

func (*Box) String

func (box *Box) String() string

String formats Box as string. See also ParseBox().

type T

type T [3]float64

T represents a 3D vector.

func Add

func Add(a, b *T) T

Add adds the composants of the two vectors and returns a new vector with the sum of the two vectors.

func Cross

func Cross(a, b *T) T

Cross returns the cross product of two vectors.

func From

func From(other generic.T) T

From copies a T from a generic.T implementation.

func Interpolate

func Interpolate(a, b *T, t float64) T

Interpolate interpolates between a and b at t (0,1).

func Max

func Max(a, b *T) T

Max returns the component wise maximum of two vectors.

func Min

func Min(a, b *T) T

Min returns the component wise minimum of two vectors.

func Mul

func Mul(a, b *T) T

Mul returns the component wise product of two vectors.

func Parse

func Parse(s string) (r T, err error)

Parse parses T from a string. See also String()

func Sub

func Sub(a, b *T) T

Sub returns the difference of two vectors.

func (*T) Abs

func (vec *T) Abs() *T

Abs sets every component of the vector to its absolute value.

func (*T) Absed

func (vec *T) Absed() T

Absed returns a copy of the vector containing the absolute values.

func (*T) Add

func (vec *T) Add(v *T) *T

Add adds another vector to vec.

func (*T) Added

func (vec *T) Added(v *T) T

Added adds another vector to vec and returns a copy of the result

func (*T) Clamp

func (vec *T) Clamp(min, max *T) *T

Clamp clamps the vector's components to be in the range of min to max.

func (*T) Clamp01

func (vec *T) Clamp01() *T

Clamp01 clamps the vector's components to be in the range of 0 to 1.

func (*T) Clamped

func (vec *T) Clamped(min, max *T) T

Clamped returns a copy of the vector with the components clamped to be in the range of min to max.

func (*T) Clamped01

func (vec *T) Clamped01() T

Clamped01 returns a copy of the vector with the components clamped to be in the range of 0 to 1.

func (*T) Cols

func (vec *T) Cols() int

Cols returns the number of columns of the vector.

func (*T) Get

func (vec *T) Get(col, row int) float64

Get returns one element of the vector.

func (*T) Invert

func (vec *T) Invert() *T

Invert inverts the vector.

func (*T) Inverted

func (vec *T) Inverted() T

Inverted returns an inverted copy of the vector.

func (*T) IsZero

func (vec *T) IsZero() bool

IsZero checks if all elements of the vector are exactly zero. Uses exact equality comparison, which may not be suitable for floating-point math results. For tolerance-based comparison, use IsZeroEps instead.

func (*T) IsZeroEps

func (vec *T) IsZeroEps(epsilon float64) bool

IsZeroEps checks if all elements of the vector are zero within the given epsilon tolerance. This is the recommended method for comparing floating-point vectors that result from calculations. For exact zero comparison, use IsZero instead.

func (*T) Length

func (vec *T) Length() float64

Length returns the length of the vector. See also LengthSqr and Normalize.

func (*T) LengthSqr

func (vec *T) LengthSqr() float64

LengthSqr returns the squared length of the vector. See also Length and Normalize.

func (*T) Mul

func (vec *T) Mul(v *T) *T

Mul multiplies the components of the vector with the respective components of v.

func (*T) Muled

func (vec *T) Muled(v *T) T

Muled multiplies the components of the vector with the respective components of v and returns a copy of the result

func (*T) Normal

func (vec *T) Normal() T

Normal returns an orthogonal vector. Uses the package Epsilon variable when checking if the cross product is zero, which provides numerical stability when the input vector is parallel to UnitZ. Falls back to UnitX when the cross product is effectively zero.

func (*T) Normalize

func (vec *T) Normalize() *T

Normalize normalizes the vector to unit length. Uses the package Epsilon variable for numerical stability: - Vectors with squared length < Epsilon are considered zero and left unchanged - Vectors with squared length within Epsilon of 1.0 are considered already normalized

func (*T) Normalized

func (vec *T) Normalized() T

Normalized returns a unit length normalized copy of the vector. Uses the package Epsilon variable for numerical stability. See Normalize() for details.

func (*T) PracticallyEquals

func (vec *T) PracticallyEquals(compareVector *T, allowedDelta float64) bool

PracticallyEquals compares two vectors if they are equal with each other within a delta tolerance.

func (*T) Rows

func (vec *T) Rows() int

Rows returns the number of rows of the vector.

func (*T) Scale

func (vec *T) Scale(f float64) *T

Scale multiplies all element of the vector by f and returns vec.

func (*T) Scaled

func (vec *T) Scaled(f float64) T

Scaled returns a copy of vec with all elements multiplies by f.

func (*T) Size

func (vec *T) Size() int

Size returns the number elements of the vector.

func (*T) Slice

func (vec *T) Slice() []float64

Slice returns the elements of the vector as slice.

func (*T) String

func (vec *T) String() string

String formats T as string. See also Parse().

func (*T) Sub

func (vec *T) Sub(v *T) *T

Sub subtracts another vector from vec.

func (*T) Subed

func (vec *T) Subed(v *T) T

Subed subtracts another vector from vec and returns a copy of the result

Jump to

Keyboard shortcuts

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