Struct Vector4D
4-element structure that can be used to represent positions in 4D space or any other pair of numeric values.
[Serializable]
public struct Vector4D : IEquatable<Vector4D>
- Implements
- Inherited Members
- Extension Methods
Constructors
Vector4D(double, double, double, double)
Constructs a new Vector4D with the given components.
public Vector4D(double x, double y, double z, double w)
Parameters
x
doubleThe vector's X component.
y
doubleThe vector's Y component.
z
doubleThe vector's Z component.
w
doubleThe vector's W component.
Vector4D(float, float, float, float)
Constructs a new Vector4D with the given components.
public Vector4D(float x, float y, float z, float w)
Parameters
x
floatThe vector's X component.
y
floatThe vector's Y component.
z
floatThe vector's Z component.
w
floatThe vector's W component.
Fields
W
The vector's W component. Also accessible by using the index position [3]
.
public double W
Field Value
X
The vector's X component. Also accessible by using the index position [0]
.
public double X
Field Value
Y
The vector's Y component. Also accessible by using the index position [1]
.
public double Y
Field Value
Z
The vector's Z component. Also accessible by using the index position [2]
.
public double Z
Field Value
Properties
Inf
Infinity vector, a vector with all components set to PositiveInfinity.
public static Vector4D Inf { get; }
Property Value
- Vector4D
Equivalent to
new Vector4D(Math.Inf, Math.Inf, Math.Inf, Math.Inf)
.
this[int]
Access vector components using their index.
public double this[int index] { readonly get; set; }
Parameters
index
int
Property Value
- double
[0]
is equivalent to X,[1]
is equivalent to Y,[2]
is equivalent to Z.[3]
is equivalent to W.
Exceptions
- ArgumentOutOfRangeException
index
is not 0, 1, 2 or 3.
One
One vector, a vector with all components set to 1
.
public static Vector4D One { get; }
Property Value
- Vector4D
Equivalent to
new Vector4D(1, 1, 1, 1)
.
UnitW
W-facing unit vector. (0 0 0 1)
public static Vector4D UnitW { get; }
Property Value
UnitX
X-facing unit vector. (1 0 0 0)
public static Vector4D UnitX { get; }
Property Value
UnitY
Y-facing unit vector. (0 1 0 0)
public static Vector4D UnitY { get; }
Property Value
UnitZ
Z-facing unit vector. (0 0 1 0)
public static Vector4D UnitZ { get; }
Property Value
Zero
Zero vector, a vector with all components set to 0
.
public static Vector4D Zero { get; }
Property Value
- Vector4D
Equivalent to
new Vector4D(0, 0, 0, 0)
.
Methods
Abs()
Returns a new vector with all components in absolute values (i.e. positive).
public readonly Vector4D Abs()
Returns
- Vector4D
A vector with Abs(double) called on each component.
Ceiling()
Returns a new vector with all components rounded up (towards positive infinity).
public readonly Vector4D Ceiling()
Returns
- Vector4D
A vector with Ceiling(double) called on each component.
Clamp(Vector4D, Vector4D)
Returns a new vector with all components clamped between the
components of min
and max
using
Clamp(double, double, double).
public readonly Vector4D Clamp(Vector4D min, Vector4D max)
Parameters
min
Vector4DThe vector with minimum allowed values.
max
Vector4DThe vector with maximum allowed values.
Returns
- Vector4D
The vector with all components clamped.
CubicInterpolate(Vector4D, Vector4D, Vector4D, double)
Performs a cubic interpolation between vectors preA
, this vector,
b
, and postB
, by the given amount weight
.
public readonly Vector4D CubicInterpolate(Vector4D b, Vector4D preA, Vector4D postB, double weight)
Parameters
b
Vector4DThe destination vector.
preA
Vector4DA vector before this vector.
postB
Vector4DA vector after
b
.weight
doubleA value on the range of 0.0 to 1.0, representing the amount of interpolation.
Returns
- Vector4D
The interpolated vector.
CubicInterpolateInTime(Vector4D, Vector4D, Vector4D, double, double, double, double)
Performs a cubic interpolation between vectors preA
, this vector,
b
, and postB
, by the given amount weight
.
It can perform smoother interpolation than CubicInterpolate(Vector4D, Vector4D, Vector4D, double)
by the time values.
public readonly Vector4D CubicInterpolateInTime(Vector4D b, Vector4D preA, Vector4D postB, double weight, double t, double preAT, double postBT)
Parameters
b
Vector4DThe destination vector.
preA
Vector4DA vector before this vector.
postB
Vector4DA vector after
b
.weight
doubleA value on the range of 0.0 to 1.0, representing the amount of interpolation.
t
doublepreAT
doublepostBT
double
Returns
- Vector4D
The interpolated vector.
Deconstruct(out double, out double, out double, out double)
Helper method for deconstruction into a tuple.
public readonly void Deconstruct(out double x, out double y, out double z, out double w)
Parameters
DirectionTo(Vector4D)
Returns the normalized vector pointing from this vector to to
.
public readonly Vector4D DirectionTo(Vector4D to)
Parameters
to
Vector4DThe other vector to point towards.
Returns
- Vector4D
The direction from this vector to
to
.
DistanceSquaredTo(Vector4D)
Returns the squared distance between this vector and to
.
This method runs faster than DistanceTo(Vector4D), so prefer it if
you need to compare vectors or need the squared distance for some formula.
public readonly double DistanceSquaredTo(Vector4D to)
Parameters
to
Vector4DThe other vector to use.
Returns
- double
The squared distance between the two vectors.
DistanceTo(Vector4D)
Returns the distance between this vector and to
.
public readonly double DistanceTo(Vector4D to)
Parameters
to
Vector4DThe other vector to use.
Returns
- double
The distance between the two vectors.
Dot(Vector4D)
Returns the dot product of this vector and with
.
public readonly double Dot(Vector4D with)
Parameters
with
Vector4DThe other vector to use.
Returns
- double
The dot product of the two vectors.
Equals(Vector4D)
Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector4D) instead, which is more reliable.
public readonly bool Equals(Vector4D other)
Parameters
other
Vector4DThe other vector.
Returns
- bool
Whether or not the vectors are exactly equal.
Equals(object?)
Returns true if the vector is exactly equal
to the given object (obj
).
Note: Due to floating-point precision errors, consider using
IsEqualApprox(Vector4D) instead, which is more reliable.
public override readonly bool Equals(object? obj)
Parameters
obj
objectThe object to compare with.
Returns
- bool
Whether or not the vector and the object are equal.
Floor()
Returns a new vector with all components rounded down (towards negative infinity).
public readonly Vector4D Floor()
Returns
- Vector4D
A vector with Floor(double) called on each component.
GetHashCode()
Serves as the hash function for Vector4D.
public override readonly int GetHashCode()
Returns
- int
A hash code for this vector.
Inverse()
Returns the inverse of this vector. This is the same as new Vector4D(1 / v.X, 1 / v.Y, 1 / v.Z, 1 / v.W)
.
public readonly Vector4D Inverse()
Returns
- Vector4D
The inverse of this vector.
IsEqualApprox(Vector4D)
Returns true if this vector and other
are approximately equal,
by running IsEqualApprox(double, double) on each component.
public readonly bool IsEqualApprox(Vector4D other)
Parameters
other
Vector4DThe other vector to compare.
Returns
- bool
Whether or not the vectors are approximately equal.
IsFinite()
Returns true if this vector is finite, by calling IsFinite(double) on each component.
public readonly bool IsFinite()
Returns
- bool
Whether this vector is finite or not.
IsNormalized()
public readonly bool IsNormalized()
Returns
IsZeroApprox()
Returns true if this vector's values are approximately zero, by running IsZeroApprox(double) on each component. This method is faster than using IsEqualApprox(Vector4D) with one value as a zero vector.
public readonly bool IsZeroApprox()
Returns
- bool
Whether or not the vector is approximately zero.
Length()
Returns the length (magnitude) of this vector.
public readonly double Length()
Returns
- double
The length of this vector.
- See Also
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 length for some formula.
public readonly double LengthSquared()
Returns
- double
The squared length of this vector.
Lerp(Vector4D, double)
Returns the result of the linear interpolation between
this vector and to
by amount weight
.
public readonly Vector4D Lerp(Vector4D to, double weight)
Parameters
to
Vector4DThe destination vector for interpolation.
weight
doubleA value on the range of 0.0 to 1.0, representing the amount of interpolation.
Returns
- Vector4D
The resulting vector of the interpolation.
MaxAxisIndex()
Returns the axis of the vector's highest value. See Axis. If all components are equal, this method returns X.
public readonly Axis MaxAxisIndex()
Returns
- Axis
The index of the highest axis.
MinAxisIndex()
Returns the axis of the vector's lowest value. See Axis. If all components are equal, this method returns W.
public readonly Axis MinAxisIndex()
Returns
- Axis
The index of the lowest axis.
Normalized()
Returns the vector scaled to unit length. Equivalent to v / v.Length()
.
public readonly Vector4D Normalized()
Returns
- Vector4D
A normalized version of the vector.
Round()
Returns this vector with all components rounded to the nearest integer, with halfway cases rounded towards the nearest multiple of two.
public readonly Vector4D Round()
Returns
- Vector4D
The rounded vector.
Sign()
Returns a vector with each component set to one or negative one, depending on the signs of this vector's components, or zero if the component is zero, by calling Sign(double) on each component.
public readonly Vector4D Sign()
Returns
- Vector4D
A vector with all components as either
1
,-1
, or0
.
Snapped(Vector4D)
Returns this vector with each component snapped to the nearest multiple of step
.
This can also be used to round to an arbitrary number of decimals.
public readonly Vector4D Snapped(Vector4D step)
Parameters
step
Vector4DA vector value representing the step size to snap to.
Returns
- Vector4D
The snapped vector.
ToString()
Converts this Vector4D to a string.
public override string ToString()
Returns
- string
A string representation of this vector.
ToString(string?)
Converts this Vector4D to a string with the given format
.
public readonly string ToString(string? format)
Parameters
format
string
Returns
- string
A string representation of this vector.
Operators
operator +(Vector4D, Vector4D)
public static Vector4D operator +(Vector4D left, Vector4D right)
Parameters
Returns
- Vector4D
The added vector.
operator /(Vector4D, Vector4D)
public static Vector4D operator /(Vector4D vec, Vector4D divisorv)
Parameters
Returns
- Vector4D
The divided vector.
operator /(Vector4D, double)
public static Vector4D operator /(Vector4D vec, double divisor)
Parameters
Returns
- Vector4D
The divided vector.
operator ==(Vector4D, Vector4D)
Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector4D) instead, which is more reliable.
public static bool operator ==(Vector4D left, Vector4D right)
Parameters
Returns
- bool
Whether or not the vectors are exactly equal.
operator >(Vector4D, Vector4D)
Compares two Vector4D vectors by first checking if
the X value of the left
vector is greater 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 finally W values of the two vectors.
This operator is useful for sorting vectors.
public static bool operator >(Vector4D left, Vector4D right)
Parameters
Returns
- bool
Whether or not the left is greater than the right.
operator >=(Vector4D, Vector4D)
Compares two Vector4D vectors by first checking if
the X value of the left
vector is greater than
or equal to the X value of the right
vector.
If the X values are exactly equal, then it repeats this check
with the Y, Z and finally W values of the two vectors.
This operator is useful for sorting vectors.
public static bool operator >=(Vector4D left, Vector4D right)
Parameters
Returns
- bool
Whether or not the left is greater than or equal to the right.
operator !=(Vector4D, Vector4D)
Returns true if the vectors are not equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector4D) instead, which is more reliable.
public static bool operator !=(Vector4D left, Vector4D right)
Parameters
Returns
- bool
Whether or not the vectors are not equal.
operator <(Vector4D, Vector4D)
Compares two Vector4D 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 finally W values of the two vectors.
This operator is useful for sorting vectors.
public static bool operator <(Vector4D left, Vector4D right)
Parameters
Returns
- bool
Whether or not the left is less than the right.
operator <=(Vector4D, Vector4D)
Compares two Vector4D vectors by first checking if
the X value of the left
vector is less than
or equal to the X value of the right
vector.
If the X values are exactly equal, then it repeats this check
with the Y, Z and finally W values of the two vectors.
This operator is useful for sorting vectors.
public static bool operator <=(Vector4D left, Vector4D right)
Parameters
Returns
- bool
Whether or not the left is less than or equal to the right.
operator %(Vector4D, Vector4D)
Gets the remainder of each component of the Vector4D with the components of the given Vector4D. This operation uses truncated division, which is often not desired as it does not work well with negative numbers.
public static Vector4D operator %(Vector4D vec, Vector4D divisorv)
Parameters
Returns
- Vector4D
The remainder vector.
Examples
GD.Print(new Vector4D(10, -20, 30, 10) % new Vector4D(7, 8, 9, 10)); // Prints "(3, -4, 3, 0)"
operator %(Vector4D, double)
Gets the remainder of each component of the Vector4D with the components of the given double. This operation uses truncated division, which is often not desired as it does not work well with negative numbers.
public static Vector4D operator %(Vector4D vec, double divisor)
Parameters
Returns
- Vector4D
The remainder vector.
Examples
GD.Print(new Vector4D(10, -20, 30, 40) % 7); // Prints "(3, -6, 2, 5)"
operator *(Vector4D, Vector4D)
public static Vector4D operator *(Vector4D left, Vector4D right)
Parameters
Returns
- Vector4D
The multiplied vector.
operator *(Vector4D, double)
public static Vector4D operator *(Vector4D vec, double scale)
Parameters
Returns
- Vector4D
The multiplied vector.
operator *(double, Vector4D)
public static Vector4D operator *(double scale, Vector4D vec)
Parameters
Returns
- Vector4D
The multiplied vector.
operator -(Vector4D, Vector4D)
public static Vector4D operator -(Vector4D left, Vector4D right)
Parameters
Returns
- Vector4D
The subtracted vector.
operator -(Vector4D)
Returns the negative value of the Vector4D.
This is the same as writing new Vector4D(-v.X, -v.Y, -v.Z, -v.W)
.
This operation flips the direction of the vector while
keeping the same magnitude.
With floats, the number zero can be either positive or negative.
public static Vector4D operator -(Vector4D vec)
Parameters
vec
Vector4DThe vector to negate/flip.
Returns
- Vector4D
The negated/flipped vector.