Struct Vector
Represents a 2D vector with X and Y components, commonly used for geometric operations, layout math, and movement in 2D space.
public struct Vector
- Inherited Members
Constructors
Vector(NFloat, NFloat)
Initializes a new instance of the Vector struct with the specified X and Y components.
public Vector(NFloat x, NFloat y)
Parameters
Fields
Down
A unit vector pointing down (0, 1).
public static readonly Vector Down
Field Value
Left
A unit vector pointing left (-1, 0).
public static readonly Vector Left
Field Value
One
A unit vector (1, 1).
public static readonly Vector One
Field Value
Right
A unit vector pointing right (1, 0).
public static readonly Vector Right
Field Value
Up
A unit vector pointing up (0, -1).
public static readonly Vector Up
Field Value
X
The horizontal component of the vector.
public NFloat X
Field Value
Y
The vertical component of the vector.
public NFloat Y
Field Value
Zero
A zero vector (0, 0).
public static readonly Vector Zero
Field Value
Properties
ArcAngle
Gets the angle in radians between the vector and the positive X-axis, measured counter-clockwise in the range [-π, π].
public NFloat ArcAngle { get; }
Property Value
- NFloat
The angle in radians between this vector and the X-axis.
Remarks
This is equivalent to calling Atan2(Y, X)
and is commonly used to compute
polar angles from directional vectors, such as when determining the angular position
of a point on a circle or ellipse.
Magnitude
Returns the magnitude (length) of the vector.
public NFloat Magnitude { get; }
Property Value
MagnitudeSquared
Returns the squared magnitude (length squared) of the vector. This avoids the square root computation used in Magnitude.
public NFloat MagnitudeSquared { get; }
Property Value
Normalized
Returns a normalized (unit length) version of this vector.
public Vector Normalized { get; }
Property Value
PerpendicularCCW
Returns the vector rotated 90° counter-clockwise (CCW).
public Vector PerpendicularCCW { get; }
Property Value
PerpendicularCW
Returns the vector rotated 90° clockwise (CW).
public Vector PerpendicularCW { get; }
Property Value
Methods
Angle(NFloat)
Returns a unit vector rotated clockwise from the upward direction (0, 1) by the given angle (in radians).
public static Vector Angle(NFloat radians)
Parameters
radians
NFloat
Returns
Angle(Vector)
Returns the clockwise angle (in radians) from the upward direction (0, 1) to the given vector.
public static NFloat Angle(Vector v)
Parameters
v
Vector
Returns
Angle(Vector, Vector)
Returns the signed angle (in radians) from lhs
to rhs
, clockwise from upward.
public static NFloat Angle(Vector lhs, Vector rhs)
Parameters
Returns
Clamp(NFloat)
Returns a new vector with its magnitude limited to the specified maximum.
public Vector Clamp(NFloat max)
Parameters
max
NFloat
Returns
Cross(Vector, Vector)
Returns the 2D cross product (scalar) of two vectors.
public static NFloat Cross(Vector lhs, Vector rhs)
Parameters
Returns
Dot(Vector, Vector)
Returns the dot product of two vectors.
public static NFloat Dot(Vector lhs, Vector rhs)
Parameters
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object? obj)
Parameters
obj
objectThe object to compare with the current instance.
Returns
- bool
true if
obj
and this instance are the same type and represent the same value; otherwise, false.
Equals(Vector)
Returns true if this vector is equal to another vector.
public bool Equals(Vector other)
Parameters
other
Vector
Returns
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
Lerp(Vector, Vector, NFloat)
Linearly interpolates between two vectors by step
.
public static Vector Lerp(Vector start, Vector end, NFloat step)
Parameters
Returns
Project(Vector, Vector)
Projects lhs
onto rhs
.
public static Vector Project(Vector lhs, Vector rhs)
Parameters
Returns
Rotate(NFloat)
Rotates the vector counterclockwise by the given angle (in degrees).
public Vector Rotate(NFloat degrees)
Parameters
degrees
NFloat
Returns
ToString()
Returns the fully qualified type name of this instance.
public override string ToString()
Returns
- string
The fully qualified type name.
Operators
operator +(Vector, Vector)
Adds two vectors.
public static Vector operator +(Vector lhs, Vector rhs)
Parameters
Returns
operator /(Vector, NFloat)
Divides a vector by a scalar.
public static Vector operator /(Vector v, NFloat f)
Parameters
Returns
operator ==(Vector, Vector)
Returns true if the two vectors have equal components.
public static bool operator ==(Vector lhs, Vector rhs)
Parameters
Returns
implicit operator Vector((NFloat, NFloat))
Implicitly converts a tuple to a Vector.
public static implicit operator Vector((NFloat, NFloat) tuple)
Parameters
tuple
(NFloat horizontal, NFloat vertical)
Returns
implicit operator Vector(Point)
public static implicit operator Vector(Point point)
Parameters
point
Point
Returns
operator !=(Vector, Vector)
Returns true if any component of the two vectors is different.
public static bool operator !=(Vector lhs, Vector rhs)
Parameters
Returns
operator *(NFloat, Vector)
Multiplies a scalar by a vector.
public static Vector operator *(NFloat f, Vector v)
Parameters
Returns
operator *(Vector, NFloat)
Multiplies a vector by a scalar.
public static Vector operator *(Vector v, NFloat f)
Parameters
Returns
operator -(Vector, Vector)
Subtracts one vector from another.
public static Vector operator -(Vector lhs, Vector rhs)