Documentation
¶
Overview ¶
Package Vector4 provides a 4D vector using integer coordinates.
Index ¶
- Variables
- func AsArray(vec XYZW) [4]Float.X
- func Distance(v, to XYZW) Float.X
- func DistanceSquared(v, to XYZW) Float.X
- func Dot(a, b XYZW) Float.X
- func Index[I Int.Any](v XYZW, i I) int
- func IsApproximatelyEqual(a, b XYZW) bool
- func IsApproximatelyZero(v XYZW) bool
- func IsFinite(v XYZW) bool
- func IsNormalized(v XYZW) bool
- func Length(v XYZW) Float.X
- func LengthSquared(v XYZW) Float.X
- func Less(a, b XYZW) bool
- type Axis
- type XYZW
- func Abs(v XYZW) XYZW
- func Add(a, b XYZW) XYZW
- func AddX[X Int.Any | Float.Any](a XYZW, b X) XYZW
- func Ceil(v XYZW) XYZW
- func Clamp(v, min, max XYZW) XYZW
- func Clampf[X Float.Any](v XYZW, min, max X) XYZW
- func CubicInterpolate[X Float.Any](v, b, preA, postB XYZW, weight X) XYZW
- func CubicInterpolateInTime[X Float.Any](v, b, preA, postB XYZW, weight, b_t, pre_a_t, post_b_t X) XYZW
- func Direction(v, to XYZW) XYZW
- func Div(a, b XYZW) XYZW
- func DivX[X Int.Any | Float.Any](a XYZW, b X) XYZW
- func Floor(v XYZW) XYZW
- func Inverse(v XYZW) XYZW
- func Lerp[X Float.Any](v, to XYZW, weight X) XYZW
- func Max(a, b XYZW) XYZW
- func Maxf[X Float.Any](v XYZW, with X) XYZW
- func Min(a, b XYZW) XYZW
- func Minf[X Float.Any](v XYZW, with X) XYZW
- func Mul(a, b XYZW) XYZW
- func MulX[X Int.Any | Float.Any](a XYZW, b X) XYZW
- func Neg(v XYZW) XYZW
- func New[X Int.Any | Float.Any](x, y, z, w X) XYZW
- func Normalized(v XYZW) XYZW
- func Posmod(v, mod XYZW) XYZW
- func Posmodf[X Float.Any](v XYZW, mod X) XYZW
- func Round(v XYZW) XYZW
- func Sign(vec XYZW) XYZW
- func Snapped(v, step XYZW) XYZW
- func Snappedf[X Float.Any](v XYZW, step X) XYZW
- func Sub(a, b XYZW) XYZW
- func SubX[X Int.Any | Float.Any](a XYZW, b X) XYZW
Constants ¶
This section is empty.
Variables ¶
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 DistanceSquared ¶
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 ¶
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 IsApproximatelyEqual ¶
IsApproximatelyEqual returns true if this vector and other are approximately equal, by running [Float.IsApproximatelyEqual] on each component.
func IsApproximatelyZero ¶
IsApproximatelyZero returns true if this vector is approximately equal to zero, by running [Float.IsApproximatelyZero] on each component.
func IsFinite ¶
IsFinite returns true if this vector's values are finite, by running [Float.IsFinite] on each component.
func IsNormalized ¶
IsNormalized Returns true if the vector is normalized, i.e. its length is approximately equal to 1.
func LengthSquared ¶
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 ¶
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]. )
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 Clamp ¶
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 ¶
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 ¶
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 ¶
Direction returns the normalized vector pointing from this vector to to. This is equivalent to using Normalized(b - a).
func Floor ¶
Floor returns a new vector with all components rounded down (towards negative infinity).
func Inverse ¶
Inverse returns the inverse of the vector. This is the same as
XYZ{1/v.X,1/v.Y,1/v.Z}.
func Lerp ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 Normalized ¶
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 Posmodf ¶
Posmodf returns a vector composed of the Fposmod of this vector's components and mod.
func Round ¶
Round returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.
func Sign ¶
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 ¶
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.