Struct Vector2D
2-element structure that can be used to represent positions in 2D space or any other pair of numeric values.
[Serializable]
public struct Vector2D : IEquatable<Vector2D>
- Implements
- Inherited Members
Constructors
Vector2D(double, double)
Constructs a new Vector2D with the given components.
public Vector2D(double x, double y)
Parameters
Fields
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
Properties
Inf
Infinity vector, a vector with all components set to PositiveInfinity.
public static Vector2D Inf { get; }
Property Value
- Vector2D
Equivalent to
new Vector2D(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
Exceptions
- ArgumentOutOfRangeException
index
is not 0 or 1.
One
One vector, a vector with all components set to 1
.
public static Vector2D One { get; }
Property Value
- Vector2D
Equivalent to
new Vector2D(1, 1)
.
UnitX
Vector that points +X.
public static Vector2D UnitX { get; }
Property Value
- Vector2D
Equivalent to
new Vector2D(0, -1)
.
UnitY
Vector that points +Y.
public static Vector2D UnitY { get; }
Property Value
- Vector2D
Equivalent to
new Vector2D(0, 1)
.
Zero
Zero vector, a vector with all components set to 0
.
public static Vector2D Zero { get; }
Property Value
- Vector2D
Equivalent to
new Vector2D(0, 0)
.
Methods
Abs()
Returns a new vector with all components in absolute values (i.e. positive).
public readonly Vector2D Abs()
Returns
- Vector2D
A vector with Abs(double) called on each component.
Angle()
Returns this vector's angle with respect to the X axis, or (1, 0) vector, in radians.
Equivalent to the result of Atan2(double, double) when
called with the vector's Y and X as parameters: Math.Atan2(v.Y, v.X)
.
public readonly double Angle()
Returns
- double
The angle of this vector, in radians.
AngleTo(Vector2D)
Returns the angle to the given vector, in radians.
public readonly double AngleTo(Vector2D to)
Parameters
to
Vector2DThe other vector to compare this vector to.
Returns
- double
The angle between the two vectors, in radians.
AngleToPoint(Vector2D)
Returns the angle between the line connecting the two points and the X axis, in radians.
public readonly double AngleToPoint(Vector2D to)
Parameters
to
Vector2DThe other vector to compare this vector to.
Returns
- double
The angle between the two vectors, in radians.
Aspect()
public readonly double Aspect()
Returns
BezierDerivative(Vector2D, Vector2D, Vector2D, 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 Vector2D BezierDerivative(Vector2D control1, Vector2D control2, Vector2D end, double t)
Parameters
control1
Vector2DControl point that defines the bezier curve.
control2
Vector2DControl point that defines the bezier curve.
end
Vector2DThe destination value for the interpolation.
t
doubleA value on the range of 0.0 to 1.0, representing the amount of interpolation.
Returns
- Vector2D
The resulting value of the interpolation.
BezierInterpolate(Vector2D, Vector2D, Vector2D, 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 Vector2D BezierInterpolate(Vector2D control1, Vector2D control2, Vector2D end, double t)
Parameters
control1
Vector2DControl point that defines the bezier curve.
control2
Vector2DControl point that defines the bezier curve.
end
Vector2DThe destination vector.
t
doubleA value on the range of 0.0 to 1.0, representing the amount of interpolation.
Returns
- Vector2D
The interpolated vector.
Ceil()
Returns a new vector with all components rounded up (towards positive infinity).
public readonly Vector2D Ceil()
Returns
- Vector2D
A vector with Ceiling(double) called on each component.
Clamp(Vector2D, Vector2D)
Returns a new vector with all components clamped between the
components of min
and max
using
Clamp(double, double, double).
public readonly Vector2D Clamp(Vector2D min, Vector2D max)
Parameters
min
Vector2DThe vector with minimum allowed values.
max
Vector2DThe vector with maximum allowed values.
Returns
- Vector2D
The vector with all components clamped.
Cross(Vector2D)
Returns the cross product of this vector and with
.
public readonly double Cross(Vector2D with)
Parameters
with
Vector2DThe other vector.
Returns
- double
The cross product value.
CubicInterpolate(Vector2D, Vector2D, Vector2D, double)
Performs a cubic interpolation between vectors preA
, this vector,
b
, and postB
, by the given amount weight
.
public readonly Vector2D CubicInterpolate(Vector2D b, Vector2D preA, Vector2D postB, double weight)
Parameters
b
Vector2DThe destination vector.
preA
Vector2DA vector before this vector.
postB
Vector2DA vector after
b
.weight
doubleA value on the range of 0.0 to 1.0, representing the amount of interpolation.
Returns
- Vector2D
The interpolated vector.
CubicInterpolateInTime(Vector2D, Vector2D, Vector2D, 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(Vector2D, Vector2D, Vector2D, double)
by the time values.
public readonly Vector2D CubicInterpolateInTime(Vector2D b, Vector2D preA, Vector2D postB, double weight, double t, double preAT, double postBT)
Parameters
b
Vector2DThe destination vector.
preA
Vector2DA vector before this vector.
postB
Vector2DA 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
- Vector2D
The interpolated vector.
Deconstruct(out double, out double)
Helper method for deconstruction into a tuple.
public readonly void Deconstruct(out double x, out double y)
Parameters
DirectionTo(Vector2D)
Returns the normalized vector pointing from this vector to to
.
public readonly Vector2D DirectionTo(Vector2D to)
Parameters
to
Vector2DThe other vector to point towards.
Returns
- Vector2D
The direction from this vector to
to
.
DistanceSquaredTo(Vector2D)
Returns the squared distance between this vector and to
.
This method runs faster than DistanceTo(Vector2D), so prefer it if
you need to compare vectors or need the squared distance for some formula.
public readonly double DistanceSquaredTo(Vector2D to)
Parameters
to
Vector2DThe other vector to use.
Returns
- double
The squared distance between the two vectors.
DistanceTo(Vector2D)
Returns the distance between this vector and to
.
public readonly double DistanceTo(Vector2D to)
Parameters
to
Vector2DThe other vector to use.
Returns
- double
The distance between the two vectors.
Dot(Vector2D)
Returns the dot product of this vector and with
.
public readonly double Dot(Vector2D with)
Parameters
with
Vector2DThe other vector to use.
Returns
- double
The dot product of the two vectors.
Equals(Vector2D)
Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector2D) instead, which is more reliable.
public readonly bool Equals(Vector2D other)
Parameters
other
Vector2DThe 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(Vector2D) 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 Vector2D Floor()
Returns
- Vector2D
A vector with Floor(double) called on each component.
FromAngle(double)
Creates a unit Vector2D rotated to the given angle. This is equivalent to doing
Vector2D(Math.Cos(angle), Math.Sin(angle))
or Vector2D.Right.Rotated(angle)
.
public static Vector2D FromAngle(double angle)
Parameters
angle
doubleAngle of the vector, in radians.
Returns
- Vector2D
The resulting vector.
GetHashCode()
Serves as the hash function for Vector2D.
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 Vector2D(1 / v.X, 1 / v.Y)
.
public readonly Vector2D Inverse()
Returns
- Vector2D
The inverse of this vector.
IsEqualApprox(Vector2D)
Returns true if this vector and other
are approximately equal,
by running IsEqualApprox(double, double) on each component.
public readonly bool IsEqualApprox(Vector2D other)
Parameters
other
Vector2DThe 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(Vector2D) 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(Vector2D, double)
Returns the result of the linear interpolation between
this vector and to
by amount weight
.
public readonly Vector2D Lerp(Vector2D to, double weight)
Parameters
to
Vector2DThe destination vector for interpolation.
weight
doubleA value on the range of 0.0 to 1.0, representing the amount of interpolation.
Returns
- Vector2D
The resulting vector of the interpolation.
LimitLength(double)
Returns the vector with a maximum length by limiting its length to length
.
public readonly Vector2D LimitLength(double length = 1)
Parameters
length
doubleThe length to limit to.
Returns
- Vector2D
The vector with its length limited.
MaxAxisIndex()
Returns the axis of the vector's highest value. See Axis. If both 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 both components are equal, this method returns Y.
public readonly Axis MinAxisIndex()
Returns
- Axis
The index of the lowest axis.
MoveToward(Vector2D, double)
Moves this vector toward to
by the fixed delta
amount.
public readonly Vector2D MoveToward(Vector2D to, double delta)
Parameters
Returns
- Vector2D
The resulting vector.
Normalized()
Returns the vector scaled to unit length. Equivalent to v / v.Length()
.
public readonly Vector2D Normalized()
Returns
- Vector2D
A normalized version of the vector.
Orthogonal()
Returns a perpendicular vector rotated 90 degrees counter-clockwise compared to the original, with the same length.
public readonly Vector2D Orthogonal()
Returns
- Vector2D
The perpendicular vector.
Project(Vector2D)
Returns this vector projected onto another vector onNormal
.
public readonly Vector2D Project(Vector2D onNormal)
Parameters
onNormal
Vector2DThe vector to project onto.
Returns
- Vector2D
The projected vector.
Reflect(Vector2D)
Returns this vector reflected from a plane defined by the given normal
.
public readonly Vector2D Reflect(Vector2D normal)
Parameters
normal
Vector2DThe normal vector defining the plane to reflect from. Must be normalized.
Returns
- Vector2D
The reflected vector.
Rotated(double)
Rotates this vector by angle
radians.
public readonly Vector2D Rotated(double angle)
Parameters
angle
doubleThe angle to rotate by, in radians.
Returns
- Vector2D
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 Vector2D Round()
Returns
- Vector2D
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 Vector2D Sign()
Returns
- Vector2D
A vector with all components as either
1
,-1
, or0
.
Slerp(Vector2D, 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(Vector2D, double).
public readonly Vector2D Slerp(Vector2D to, double weight)
Parameters
to
Vector2DThe destination vector for interpolation.
weight
doubleA value on the range of 0.0 to 1.0, representing the amount of interpolation.
Returns
- Vector2D
The resulting vector of the interpolation.
Slide(Vector2D)
Returns this vector slid along a plane defined by the given normal
.
public readonly Vector2D Slide(Vector2D normal)
Parameters
normal
Vector2DThe normal vector defining the plane to slide on.
Returns
- Vector2D
The slid vector.
Snapped(Vector2D)
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 Vector2D Snapped(Vector2D step)
Parameters
step
Vector2DA vector value representing the step size to snap to.
Returns
- Vector2D
The snapped vector.
ToString()
Converts this Vector2D to a string.
public override readonly string ToString()
Returns
- string
A string representation of this vector.
ToString(string?)
Converts this Vector2D 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 +(Vector2D, Vector2D)
public static Vector2D operator +(Vector2D left, Vector2D right)
Parameters
Returns
- Vector2D
The added vector.
operator /(Vector2D, Vector2D)
public static Vector2D operator /(Vector2D vec, Vector2D divisorv)
Parameters
Returns
- Vector2D
The divided vector.
operator /(Vector2D, double)
public static Vector2D operator /(Vector2D vec, double divisor)
Parameters
Returns
- Vector2D
The divided vector.
operator ==(Vector2D, Vector2D)
Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector2D) instead, which is more reliable.
public static bool operator ==(Vector2D left, Vector2D right)
Parameters
Returns
- bool
Whether or not the vectors are exactly equal.
operator >(Vector2D, Vector2D)
Compares two Vector2D 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.
This operator is useful for sorting vectors.
public static bool operator >(Vector2D left, Vector2D right)
Parameters
Returns
- bool
Whether or not the left is greater than the right.
operator >=(Vector2D, Vector2D)
Compares two Vector2D 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.
This operator is useful for sorting vectors.
public static bool operator >=(Vector2D left, Vector2D right)
Parameters
Returns
- bool
Whether or not the left is greater than or equal to the right.
operator !=(Vector2D, Vector2D)
Returns true if the vectors are not equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector2D) instead, which is more reliable.
public static bool operator !=(Vector2D left, Vector2D right)
Parameters
Returns
- bool
Whether or not the vectors are not equal.
operator <(Vector2D, Vector2D)
Compares two Vector2D 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.
This operator is useful for sorting vectors.
public static bool operator <(Vector2D left, Vector2D right)
Parameters
Returns
- bool
Whether or not the left is less than the right.
operator <=(Vector2D, Vector2D)
Compares two Vector2D 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.
This operator is useful for sorting vectors.
public static bool operator <=(Vector2D left, Vector2D right)
Parameters
Returns
- bool
Whether or not the left is less than or equal to the right.
operator %(Vector2D, Vector2D)
Gets the remainder of each component of the Vector2D with the components of the given Vector2D. This operation uses truncated division, which is often not desired as it does not work well with negative numbers.
public static Vector2D operator %(Vector2D vec, Vector2D divisorv)
Parameters
Returns
- Vector2D
The remainder vector.
Examples
GD.Print(new Vector2D(10, -20) % new Vector2D(7, 8)); // Prints "(3, -4)"
operator %(Vector2D, double)
Gets the remainder of each component of the Vector2D 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 Vector2D operator %(Vector2D vec, double divisor)
Parameters
Returns
- Vector2D
The remainder vector.
Examples
GD.Print(new Vector2D(10, -20) % 7); // Prints "(3, -6)"
operator *(Vector2D, Vector2D)
public static Vector2D operator *(Vector2D left, Vector2D right)
Parameters
Returns
- Vector2D
The multiplied vector.
operator *(Vector2D, double)
public static Vector2D operator *(Vector2D vec, double scale)
Parameters
Returns
- Vector2D
The multiplied vector.
operator *(double, Vector2D)
public static Vector2D operator *(double scale, Vector2D vec)
Parameters
Returns
- Vector2D
The multiplied vector.
operator -(Vector2D, Vector2D)
public static Vector2D operator -(Vector2D left, Vector2D right)
Parameters
Returns
- Vector2D
The subtracted vector.
operator -(Vector2D)
Returns the negative value of the Vector2D.
This is the same as writing new Vector2D(-v.X, -v.Y)
.
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 Vector2D operator -(Vector2D vec)
Parameters
vec
Vector2DThe vector to negate/flip.
Returns
- Vector2D
The negated/flipped vector.