Vector4

package
v0.0.0-...-5fa07e4 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package Vector4 provides a 4D vector using integer coordinates.

Index

Constants

This section is empty.

Variables

View Source
var (
	Zero = XYZW{0, 0, 0, 0}                                 // Zero vector, a vector with all components set to 0.
	One  = XYZW{1, 1, 1, 1}                                 // One vector, a vector with all components set to 1.
	Inf  = XYZW{Float.Inf, Float.Inf, Float.Inf, Float.Inf} // Inf vector, a vector with all components set to positive infinity.
)

Functions

func AsArray

func AsArray(vec XYZW) [4]Float.X

func Distance

func Distance(v, to XYZW) Float.X

Distance returns the distance between this vector and to.

func DistanceSquared

func DistanceSquared(v, to XYZW) Float.X

DistanceSquared returns the squared distance between this vector and to.

This method runs faster than [Vector3.DistanceTo], so prefer it if you need to compare vectors or need the squared distance for some formula.

func Dot

func Dot(a, b XYZW) Float.X

Dot returns the dot product of this vector and with. This can be used to compare the angle between two vectors. For example, this can be used to determine whether an enemy is facing the player.

The dot product will be 0 for a straight angle (90 degrees), greater than 0 for angles narrower than 90 degrees and lower than 0 for angles wider than 90 degrees.

When using unit (normalized) vectors, the result will always be between -1.0 (180 degree angle) when the vectors are facing opposite directions, and 1.0 (0 degree angle) when the vectors are aligned.

Note: a.Dot(b) is equivalent to b.Dot(a).

func Index

func Index[I Int.Any](v XYZW, i I) int

func IsApproximatelyEqual

func IsApproximatelyEqual(a, b XYZW) bool

IsApproximatelyEqual returns true if this vector and other are approximately equal, by running [Float.IsApproximatelyEqual] on each component.

func IsApproximatelyZero

func IsApproximatelyZero(v XYZW) bool

IsApproximatelyZero returns true if this vector is approximately equal to zero, by running [Float.IsApproximatelyZero] on each component.

func IsFinite

func IsFinite(v XYZW) bool

IsFinite returns true if this vector's values are finite, by running [Float.IsFinite] on each component.

func IsNormalized

func IsNormalized(v XYZW) bool

IsNormalized Returns true if the vector is normalized, i.e. its length is approximately equal to 1.

func Length

func Length(v XYZW) Float.X

Length the length (magnitude) of this vector.

func LengthSquared

func LengthSquared(v XYZW) Float.X

LengthSquared returns the squared length (squared magnitude) of this vector.

This method runs faster than length, so prefer it if you need to compare vectors or need the squared distance for some formula.

func Less

func Less(a, b XYZW) bool

Less compares two Vector4 vectors by first checking if the X value of the left vector is less than the X value of the right vector. If the X values are exactly equal, then it repeats this check with the Y, Z and W values of the two vectors. This operator is useful for sorting vectors.

Note: Vectors with NaN elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.

Types

type Axis

type Axis int
const (
	X Axis = iota // Enumerated value for the X axis. Returned by [MaxAxis] and [MinAxis].
	Y             // Enumerated value for the Y axis. Returned by [MaxAxis] and [MinAxis].
	Z             // Enumerated value for the Z axis. Returned by [MaxAxis] and [MinAxis].
	W             // Enumerated value for the W axis. Returned by [MaxAxis] and [MinAxis].
)

func MaxAxis

func MaxAxis(v XYZW) Axis

MaxAxis returns the axis of the vector's highest value. See Axis constants. If all components are equal, this method returns X.

func MinAxis

func MinAxis(v XYZW) Axis

MinAxis returns the axis of the vector's lowest value. See Axis constants. If all components are equal, this method returns Z.

type XYZW

type XYZW = struct {
	X Float.X // The vector's X component.
	Y Float.X // The vector's Y component.
	Z Float.X // The vector's Z component.
	W Float.X // The vector's W component.
}

XYZW is a 4-element structure that can be used to represent 4D coordinates or any other quadruplet of numeric values.

It uses floating-point coordinates. By default, these floating-point values use 32-bit precision, unlike float which is always 64-bit. If double precision is needed, compile with the Go build tag 'precision_double'.

See [Vector4i.XYZW] for its integer counterpart.

func Abs

func Abs(v XYZW) XYZW

Abs returns a new vector with all components in absolute values (i.e. positive).

func Add

func Add(a, b XYZW) XYZW

func AddX

func AddX[X Int.Any | Float.Any](a XYZW, b X) XYZW

func Ceil

func Ceil(v XYZW) XYZW

Ceil returns a new vector with all components rounded up (towards positive infinity).

func Clamp

func Clamp(v, min, max XYZW) XYZW

Clamp returns a new vector with all components clamped between the components of min and max, by running [Float.Clamp] on each component.

func Clampf

func Clampf[X Float.Any](v XYZW, min, max X) XYZW

Clamp returns a new vector with all components clamped between the components of min and max, by running [Float.Clamp] on each component.

func CubicInterpolate

func CubicInterpolate[X Float.Any](v, b, preA, postB XYZW, weight X) XYZW

CubicInterpolate performs a cubic interpolation between this vector and b using pre_a and post_b as handles and returns the result at position weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.

func CubicInterpolateInTime

func CubicInterpolateInTime[X Float.Any](v, b, preA, postB XYZW, weight, b_t, pre_a_t, post_b_t X) XYZW

CubicInterpolateInTime performs a cubic interpolation between this vector and b using pre_a and post_b as handles and returns the result at position weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.

It can perform smoother interpolation than [Vector3.CubicInterpolate] by the time values.

func Direction

func Direction(v, to XYZW) XYZW

Direction returns the normalized vector pointing from this vector to to. This is equivalent to using Normalized(b - a).

func Div

func Div(a, b XYZW) XYZW

func DivX

func DivX[X Int.Any | Float.Any](a XYZW, b X) XYZW

func Floor

func Floor(v XYZW) XYZW

Floor returns a new vector with all components rounded down (towards negative infinity).

func Inverse

func Inverse(v XYZW) XYZW

Inverse returns the inverse of the vector. This is the same as

XYZ{1/v.X,1/v.Y,1/v.Z}.

func Lerp

func Lerp[X Float.Any](v, to XYZW, weight X) XYZW

Lerp returns the result of the linear interpolation between this vector and to by amount weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.

func Max

func Max(a, b XYZW) XYZW

Max returns the component-wise maximum of this and with, equivalent to

Vector4(max(x, with.x), max(y, with.y), max(z, with.z), max(w, with.w))

func Maxf

func Maxf[X Float.Any](v XYZW, with X) XYZW

Maxf returns the component-wise maximum of this and with, equivalent to

Vector4(max(x, with), max(y, with), max(z, with), max(w, with)).

func Min

func Min(a, b XYZW) XYZW

Min returns the component-wise minimum of this and with, equivalent to

Vector4(min(x, with.x), min(y, with.y), min(z, with.z), min(w, with.w))

func Minf

func Minf[X Float.Any](v XYZW, with X) XYZW

Minf returns the component-wise minimum of this and with, equivalent to

Vector4(min(x, with), min(y, with), min(z, with), min(w, with)).

func Mul

func Mul(a, b XYZW) XYZW

func MulX

func MulX[X Int.Any | Float.Any](a XYZW, b X) XYZW

func Neg

func Neg(v XYZW) XYZW

func New

func New[X Int.Any | Float.Any](x, y, z, w X) XYZW

New returns a XYZW with the given components.

func Normalized

func Normalized(v XYZW) XYZW

Normalized returns the result of scaling the vector to unit length. Equivalent to v / v.Length(). See also IsNormalized.

Note: This function may return incorrect values if the input vector length is near zero.

func Posmod

func Posmod(v, mod XYZW) XYZW

Posmod returns a vector composed of the Fposmod of this vector's components and mod.

func Posmodf

func Posmodf[X Float.Any](v XYZW, mod X) XYZW

Posmodf returns a vector composed of the Fposmod of this vector's components and mod.

func Round

func Round(v XYZW) XYZW

Round returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.

func Sign

func Sign(vec XYZW) XYZW

Sign returns a new vector with each component set to 1.0 if it's positive, -1.0 if it's negative, and 0.0 if it's zero. The result is identical to calling [Float.Sign] on each component.

func Snapped

func Snapped(v, step XYZW) XYZW

Snapped returns a new vector with each component snapped to the nearest multiple of the corresponding component in step. This can also be used to round the components to an arbitrary number of decimals.

func Snappedf

func Snappedf[X Float.Any](v XYZW, step X) XYZW

Snappedf returns a new vector with each component snapped to the nearest multiple of step. This can also be used to round the components to an arbitrary number of decimals.

func Sub

func Sub(a, b XYZW) XYZW

func SubX

func SubX[X Int.Any | Float.Any](a XYZW, b X) XYZW

Jump to

Keyboard shortcuts

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