Struct Vector3D

Namespace
Elegy.Common.Maths
Assembly
Elegy.Common.dll

3-element structure that can be used to represent positions in 3D space or any other pair of numeric values.

[Serializable]
public struct Vector3D : IEquatable<Vector3D>
Implements
Inherited Members
Extension Methods

Constructors

Vector3D(double, double, double)

Constructs a new Vector3D with the given components.

public Vector3D(double x, double y, double z)

Parameters

x double

The vector's X component.

y double

The vector's Y component.

z double

The vector's Z component.

Vector3D(int, int, int)

Constructs a new Vector3D with the given components.

public Vector3D(int x, int y, int z)

Parameters

x int

The vector's X component.

y int

The vector's Y component.

z int

The vector's Z component.

Vector3D(float, float, float)

Constructs a new Vector3D with the given components.

public Vector3D(float x, float y, float z)

Parameters

x float

The vector's X component.

y float

The vector's Y component.

z float

The vector's Z component.

Fields

X

The vector's X component. Also accessible by using the index position [0].

public double X

Field Value

double

Y

The vector's Y component. Also accessible by using the index position [1].

public double Y

Field Value

double

Z

The vector's Z component. Also accessible by using the index position [2].

public double Z

Field Value

double

Properties

Inf

Infinity vector, a vector with all components set to PositiveInfinity.

public static Vector3D Inf { get; }

Property Value

Vector3D

Equivalent to new Vector3D(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.

Exceptions

ArgumentOutOfRangeException

index is not 0, 1 or 2.

One

One vector, a vector with all components set to 1.

public static Vector3D One { get; }

Property Value

Vector3D

Equivalent to new Vector3D(1, 1, 1).

UnitX

Unit vector along the X axis: (1 0 0).

public static Vector3D UnitX { get; }

Property Value

Vector3D

UnitY

Unit vector along the Y axis: (0 1 0).

public static Vector3D UnitY { get; }

Property Value

Vector3D

UnitZ

Unit vector along the Z axis: (0 0 1).

public static Vector3D UnitZ { get; }

Property Value

Vector3D

Zero

Zero vector, a vector with all components set to 0.

public static Vector3D Zero { get; }

Property Value

Vector3D

Equivalent to new Vector3D(0, 0, 0).

Methods

Abs()

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

public readonly Vector3D Abs()

Returns

Vector3D

A vector with Abs(double) called on each component.

AngleTo(Vector3D)

Returns the unsigned minimum angle to the given vector, in radians.

public readonly double AngleTo(Vector3D to)

Parameters

to Vector3D

The other vector to compare this vector to.

Returns

double

The unsigned angle between the two vectors, in radians.

BezierDerivative(Vector3D, Vector3D, Vector3D, double)

Returns the derivative at the given t on the Bezier curve defined by this vector and the given control1, control2, and end points.

public readonly Vector3D BezierDerivative(Vector3D control1, Vector3D control2, Vector3D end, double t)

Parameters

control1 Vector3D

Control point that defines the bezier curve.

control2 Vector3D

Control point that defines the bezier curve.

end Vector3D

The destination value for the interpolation.

t double

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

Vector3D

The resulting value of the interpolation.

BezierInterpolate(Vector3D, Vector3D, Vector3D, double)

Returns the point at the given t on a one-dimensional Bezier curve defined by this vector and the given control1, control2, and end points.

public readonly Vector3D BezierInterpolate(Vector3D control1, Vector3D control2, Vector3D end, double t)

Parameters

control1 Vector3D

Control point that defines the bezier curve.

control2 Vector3D

Control point that defines the bezier curve.

end Vector3D

The destination vector.

t double

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

Vector3D

The interpolated vector.

Ceiling()

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

public readonly Vector3D Ceiling()

Returns

Vector3D

A vector with Ceiling(double) called on each component.

Clamp(Vector3D, Vector3D)

Returns a new vector with all components clamped between the components of min and max using Clamp(double, double, double).

public readonly Vector3D Clamp(Vector3D min, Vector3D max)

Parameters

min Vector3D

The vector with minimum allowed values.

max Vector3D

The vector with maximum allowed values.

Returns

Vector3D

The vector with all components clamped.

Cross(Vector3D)

Returns the cross product of this vector and with.

public readonly Vector3D Cross(Vector3D with)

Parameters

with Vector3D

The other vector.

Returns

Vector3D

The cross product vector.

CubicInterpolate(Vector3D, Vector3D, Vector3D, double)

Performs a cubic interpolation between vectors preA, this vector, b, and postB, by the given amount weight.

public readonly Vector3D CubicInterpolate(Vector3D b, Vector3D preA, Vector3D postB, double weight)

Parameters

b Vector3D

The destination vector.

preA Vector3D

A vector before this vector.

postB Vector3D

A vector after b.

weight double

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

Vector3D

The interpolated vector.

CubicInterpolateInTime(Vector3D, Vector3D, Vector3D, 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(Vector3D, Vector3D, Vector3D, double) by the time values.

public readonly Vector3D CubicInterpolateInTime(Vector3D b, Vector3D preA, Vector3D postB, double weight, double t, double preAT, double postBT)

Parameters

b Vector3D

The destination vector.

preA Vector3D

A vector before this vector.

postB Vector3D

A vector after b.

weight double

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

t double
preAT double
postBT double

Returns

Vector3D

The interpolated vector.

Deconstruct(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)

Parameters

x double
y double
z double

DirectionTo(Vector3D)

Returns the normalized vector pointing from this vector to to.

public readonly Vector3D DirectionTo(Vector3D to)

Parameters

to Vector3D

The other vector to point towards.

Returns

Vector3D

The direction from this vector to to.

DistanceSquaredTo(Vector3D)

Returns the squared distance between this vector and to. This method runs faster than DistanceTo(Vector3D), so prefer it if you need to compare vectors or need the squared distance for some formula.

public readonly double DistanceSquaredTo(Vector3D to)

Parameters

to Vector3D

The other vector to use.

Returns

double

The squared distance between the two vectors.

DistanceTo(Vector3D)

Returns the distance between this vector and to.

public readonly double DistanceTo(Vector3D to)

Parameters

to Vector3D

The other vector to use.

Returns

double

The distance between the two vectors.

See Also

Dot(Vector3D)

Returns the dot product of this vector and with.

public readonly double Dot(Vector3D with)

Parameters

with Vector3D

The other vector to use.

Returns

double

The dot product of the two vectors.

Equals(Vector3D)

Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector3D) instead, which is more reliable.

public readonly bool Equals(Vector3D other)

Parameters

other Vector3D

The 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(Vector3D) instead, which is more reliable.

public override readonly bool Equals(object? obj)

Parameters

obj object

The 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 Vector3D Floor()

Returns

Vector3D

A vector with Floor(double) called on each component.

GetHashCode()

Serves as the hash function for Vector3D.

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 Vector3D(1 / v.X, 1 / v.Y, 1 / v.Z).

public readonly Vector3D Inverse()

Returns

Vector3D

The inverse of this vector.

IsEqualApprox(Vector3D)

Returns true if this vector and other are approximately equal, by running IsEqualApprox(double, double) on each component.

public readonly bool IsEqualApprox(Vector3D other)

Parameters

other Vector3D

The 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()

Returns true if the vector is normalized, and false otherwise.

public readonly bool IsNormalized()

Returns

bool

A bool indicating whether or not the vector is normalized.

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(Vector3D) 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(Vector3D, double)

Returns the result of the linear interpolation between this vector and to by amount weight.

public readonly Vector3D Lerp(Vector3D to, double weight)

Parameters

to Vector3D

The destination vector for interpolation.

weight double

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

Vector3D

The resulting vector of the interpolation.

LimitLength(double)

Returns the vector with a maximum length by limiting its length to length.

public readonly Vector3D LimitLength(double length = 1)

Parameters

length double

The length to limit to.

Returns

Vector3D

The vector with its length limited.

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 Z.

public readonly Axis MinAxisIndex()

Returns

Axis

The index of the lowest axis.

MoveToward(Vector3D, double)

Moves this vector toward to by the fixed delta amount.

public readonly Vector3D MoveToward(Vector3D to, double delta)

Parameters

to Vector3D

The vector to move towards.

delta double

The amount to move towards by.

Returns

Vector3D

The resulting vector.

Normalized()

Returns the vector scaled to unit length. Equivalent to v / v.Length().

public readonly Vector3D Normalized()

Returns

Vector3D

A normalized version of the vector.

Project(Vector3D)

Returns this vector projected onto another vector onNormal.

public readonly Vector3D Project(Vector3D onNormal)

Parameters

onNormal Vector3D

The vector to project onto.

Returns

Vector3D

The projected vector.

Reflect(Vector3D)

Returns this vector reflected from a plane defined by the given normal.

public readonly Vector3D Reflect(Vector3D normal)

Parameters

normal Vector3D

The normal vector defining the plane to reflect from. Must be normalized.

Returns

Vector3D

The reflected vector.

Rotated(Vector3D, double)

Rotates this vector around a given axis vector by angle (in radians). The axis vector must be a normalized vector. Warning: uses floating-point maths internally.

public readonly Vector3D Rotated(Vector3D axis, double angle)

Parameters

axis Vector3D

The vector to rotate around. Must be normalized.

angle double

The angle to rotate by, in radians.

Returns

Vector3D

The rotated 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 Vector3D Round()

Returns

Vector3D

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 Vector3D Sign()

Returns

Vector3D

A vector with all components as either 1, -1, or 0.

SignedAngleTo(Vector3D, Vector3D)

Returns the signed angle to the given vector, in radians. The sign of the angle is positive in a counter-clockwise direction and negative in a clockwise direction when viewed from the side specified by the axis.

public readonly double SignedAngleTo(Vector3D to, Vector3D axis)

Parameters

to Vector3D

The other vector to compare this vector to.

axis Vector3D

The reference axis to use for the angle sign.

Returns

double

The signed angle between the two vectors, in radians.

Slerp(Vector3D, double)

Returns the result of the spherical linear interpolation between this vector and to by amount weight.

This method also handles interpolating the lengths if the input vectors have different lengths. For the special case of one or both input vectors having zero length, this method behaves like Lerp(Vector3D, double).

public readonly Vector3D Slerp(Vector3D to, double weight)

Parameters

to Vector3D

The destination vector for interpolation.

weight double

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

Vector3D

The resulting vector of the interpolation.

Slide(Vector3D)

Returns this vector slid along a plane defined by the given normal.

public readonly Vector3D Slide(Vector3D normal)

Parameters

normal Vector3D

The normal vector defining the plane to slide on.

Returns

Vector3D

The slid vector.

Snapped(Vector3D)

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 Vector3D Snapped(Vector3D step)

Parameters

step Vector3D

A vector value representing the step size to snap to.

Returns

Vector3D

The snapped vector.

ToString()

Converts this Vector3D to a string.

public override readonly string ToString()

Returns

string

A string representation of this vector.

ToString(string?)

Converts this Vector3D 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 +(Vector3D, Vector3D)

Adds each component of the Vector3D with the components of the given Vector3D.

public static Vector3D operator +(Vector3D left, Vector3D right)

Parameters

left Vector3D

The left vector.

right Vector3D

The right vector.

Returns

Vector3D

The added vector.

operator /(Vector3D, Vector3D)

Divides each component of the Vector3D by the components of the given Vector3D.

public static Vector3D operator /(Vector3D vec, Vector3D divisorv)

Parameters

vec Vector3D

The dividend vector.

divisorv Vector3D

The divisor vector.

Returns

Vector3D

The divided vector.

operator /(Vector3D, double)

Divides each component of the Vector3D by the given double.

public static Vector3D operator /(Vector3D vec, double divisor)

Parameters

vec Vector3D

The dividend vector.

divisor double

The divisor value.

Returns

Vector3D

The divided vector.

operator ==(Vector3D, Vector3D)

Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector3D) instead, which is more reliable.

public static bool operator ==(Vector3D left, Vector3D right)

Parameters

left Vector3D

The left vector.

right Vector3D

The right vector.

Returns

bool

Whether or not the vectors are exactly equal.

operator >(Vector3D, Vector3D)

Compares two Vector3D 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 values of the two vectors, and then with the Z values. This operator is useful for sorting vectors.

public static bool operator >(Vector3D left, Vector3D right)

Parameters

left Vector3D

The left vector.

right Vector3D

The right vector.

Returns

bool

Whether or not the left is greater than the right.

operator >=(Vector3D, Vector3D)

Compares two Vector3D 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 values of the two vectors, and then with the Z values. This operator is useful for sorting vectors.

public static bool operator >=(Vector3D left, Vector3D right)

Parameters

left Vector3D

The left vector.

right Vector3D

The right vector.

Returns

bool

Whether or not the left is greater than or equal to the right.

operator !=(Vector3D, Vector3D)

Returns true if the vectors are not equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector3D) instead, which is more reliable.

public static bool operator !=(Vector3D left, Vector3D right)

Parameters

left Vector3D

The left vector.

right Vector3D

The right vector.

Returns

bool

Whether or not the vectors are not equal.

operator <(Vector3D, Vector3D)

Compares two Vector3D 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 values of the two vectors, and then with the Z values. This operator is useful for sorting vectors.

public static bool operator <(Vector3D left, Vector3D right)

Parameters

left Vector3D

The left vector.

right Vector3D

The right vector.

Returns

bool

Whether or not the left is less than the right.

operator <=(Vector3D, Vector3D)

Compares two Vector3D 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 values of the two vectors, and then with the Z values. This operator is useful for sorting vectors.

public static bool operator <=(Vector3D left, Vector3D right)

Parameters

left Vector3D

The left vector.

right Vector3D

The right vector.

Returns

bool

Whether or not the left is less than or equal to the right.

operator %(Vector3D, Vector3D)

Gets the remainder of each component of the Vector3D with the components of the given Vector3D. This operation uses truncated division, which is often not desired as it does not work well with negative numbers.

public static Vector3D operator %(Vector3D vec, Vector3D divisorv)

Parameters

vec Vector3D

The dividend vector.

divisorv Vector3D

The divisor vector.

Returns

Vector3D

The remainder vector.

Examples

GD.Print(new Vector3D(10, -20, 30) % new Vector3D(7, 8, 9)); // Prints "(3, -4, 3)"

operator %(Vector3D, double)

Gets the remainder of each component of the Vector3D 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 Vector3D operator %(Vector3D vec, double divisor)

Parameters

vec Vector3D

The dividend vector.

divisor double

The divisor value.

Returns

Vector3D

The remainder vector.

Examples

GD.Print(new Vector3D(10, -20, 30) % 7); // Prints "(3, -6, 2)"

operator *(Vector3D, Vector3D)

Multiplies each component of the Vector3D by the components of the given Vector3D.

public static Vector3D operator *(Vector3D left, Vector3D right)

Parameters

left Vector3D

The left vector.

right Vector3D

The right vector.

Returns

Vector3D

The multiplied vector.

operator *(Vector3D, double)

Multiplies each component of the Vector3D by the given double.

public static Vector3D operator *(Vector3D vec, double scale)

Parameters

vec Vector3D

The vector to multiply.

scale double

The scale to multiply by.

Returns

Vector3D

The multiplied vector.

operator *(double, Vector3D)

Multiplies each component of the Vector3D by the given double.

public static Vector3D operator *(double scale, Vector3D vec)

Parameters

scale double

The scale to multiply by.

vec Vector3D

The vector to multiply.

Returns

Vector3D

The multiplied vector.

operator -(Vector3D, Vector3D)

Subtracts each component of the Vector3D by the components of the given Vector3D.

public static Vector3D operator -(Vector3D left, Vector3D right)

Parameters

left Vector3D

The left vector.

right Vector3D

The right vector.

Returns

Vector3D

The subtracted vector.

operator -(Vector3D)

Returns the negative value of the Vector3D. This is the same as writing new Vector3D(-v.X, -v.Y, -v.Z). 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 Vector3D operator -(Vector3D vec)

Parameters

vec Vector3D

The vector to negate/flip.

Returns

Vector3D

The negated/flipped vector.