Struct Direction2
Represents a normalized 2D direction — a unit vector on the plane, combined with the angle it makes with the positive X-axis (measured counter-clockwise).
A Direction2 always stores its angle in the range [0°, 360°). The
Direction is always a unit vector (magnitude 1). Both values are
derived from each other: Direction = (cos θ, sin θ).
Use Direction2 when you need to express "which way something is facing" or
"which way to move" independently of how far to move. Combine with a
UnitsNet.Length or LengthVector2 to produce a displacement.
public readonly record struct Direction2 : IEquatable<Direction2>
- Implements
- Inherited Members
- Extension Methods
Examples
// Face right, move 5 metres in that direction
var facing = Direction2.FromDegrees(0);
LengthVector2 displacement = facing * Length.FromMeters(5); // (5 m, 0 m)
Constructors
Direction2(DoubleVector2)
Constructs a Direction2 from a DoubleVector2.
The vector is normalized to unit length; the angle is derived from its direction.
The vector does not need to be a unit vector — any non-zero vector is accepted and normalized automatically.
public Direction2(DoubleVector2 vector)
Parameters
vectorDoubleVector2A 2D vector whose direction is used. Does not need to be normalized.
Examples
var dir = new Direction2(new DoubleVector2(3, 4)); // normalized to (0.6, 0.8)
Direction2(LengthPoint2, LengthPoint2)
Constructs a Direction2 pointing from from to
to.
public Direction2(LengthPoint2 from, LengthPoint2 to)
Parameters
fromLengthPoint2The origin point.
toLengthPoint2The target point.
Examples
var origin = LengthPoint2.FromMeters(0, 0);
var target = LengthPoint2.FromMeters(1, 0);
var dir = new Direction2(origin, target); // 0° — pointing right
Direction2(LengthVector2)
Constructs a Direction2 from a LengthVector2 by using its
direction component (discarding magnitude).
public Direction2(LengthVector2 vector)
Parameters
vectorLengthVector2A 2D displacement vector. Its magnitude is ignored; only its direction is used.
Examples
var velocity = new LengthVector2(Length.FromMeters(0), Length.FromMeters(10));
var facing = new Direction2(velocity); // 90° — pointing up
Direction2(Angle)
Constructs a Direction2 from an angle measured counter-clockwise from the
positive X-axis. The angle is normalized to [0°, 360°).
public Direction2(Angle angle)
Parameters
angleAngleThe direction angle. Negative angles and angles ≥ 360° are wrapped automatically.
Examples
var up = new Direction2(Angle.FromDegrees(90)); // points along positive Y
var same = new Direction2(Angle.FromDegrees(-270)); // also points up
Properties
Angle
The angle this direction makes with the positive X-axis, measured counter-clockwise, always in the range [0°, 360°).
public Angle Angle { get; }
Property Value
- Angle
Direction
The unit vector representing this direction.
The vector has magnitude 1 and is derived from Angle via
(cos θ, sin θ).
public DoubleVector2 Direction { get; }
Property Value
East
Points along the positive X-axis (0° CCW from +X). Equivalent to Zero.
public static Direction2 East { get; }
Property Value
Examples
// Move a point 5 m due east
var displacement = Direction2.East * Length.FromMeters(5);
EastNorthEast
Points 22.5° counter-clockwise from East (ENE). Between east and north-east.
public static Direction2 EastNorthEast { get; }
Property Value
EastSouthEast
Points 337.5° counter-clockwise from East (ESE). Between south-east and east.
public static Direction2 EastSouthEast { get; }
Property Value
North
Points along the positive Y-axis (90° CCW from +X). In a standard 2D coordinate system this is "up".
public static Direction2 North { get; }
Property Value
Examples
var up = Direction2.North;
// up.Direction ≈ (0, 1)
NorthEast
public static Direction2 NorthEast { get; }
Property Value
Examples
// The NE diagonal has equal X and Y components
var d = Direction2.NorthEast.Direction; // ≈ (0.707, 0.707)
NorthNorthEast
Points 67.5° counter-clockwise from East (NNE). Between north-east and north.
public static Direction2 NorthNorthEast { get; }
Property Value
NorthNorthWest
Points 112.5° counter-clockwise from East (NNW). Between north and north-west.
public static Direction2 NorthNorthWest { get; }
Property Value
NorthWest
public static Direction2 NorthWest { get; }
Property Value
South
Points along the negative Y-axis (270° CCW from +X). In a standard 2D coordinate system this is "down".
public static Direction2 South { get; }
Property Value
SouthEast
public static Direction2 SouthEast { get; }
Property Value
SouthSouthEast
Points 292.5° counter-clockwise from East (SSE). Between south and south-east.
public static Direction2 SouthSouthEast { get; }
Property Value
SouthSouthWest
Points 247.5° counter-clockwise from East (SSW). Between south-west and south.
public static Direction2 SouthSouthWest { get; }
Property Value
SouthWest
public static Direction2 SouthWest { get; }
Property Value
West
Points along the negative X-axis (180° CCW from +X). The opposite of East.
public static Direction2 West { get; }
Property Value
WestNorthWest
Points 157.5° counter-clockwise from East (WNW). Between north-west and west.
public static Direction2 WestNorthWest { get; }
Property Value
WestSouthWest
Points 202.5° counter-clockwise from East (WSW). Between west and south-west.
public static Direction2 WestSouthWest { get; }
Property Value
Zero
A Direction2 pointing along the positive X-axis (0°, direction (1, 0)).
This is the "default" direction — right on screen in most 2D coordinate systems.
public static Direction2 Zero { get; }
Property Value
Methods
CrossProduct(Direction2)
Returns the 2D cross product (perpendicular dot product) of this direction with
other: Direction.X * other.Direction.Y − Direction.Y * other.Direction.X.
Because both directions are unit vectors, the result is the sine of the angle from
this direction to other. A positive result means
other is counter-clockwise (CCW) from this direction; negative
means clockwise (CW); zero means they are parallel or anti-parallel.
public double CrossProduct(Direction2 other)
Parameters
otherDirection2The second direction.
Returns
- double
A dimensionless scalar in [−1, 1]: positive =
otheris CCW, negative = CW, zero = parallel.
Examples
var right = Direction2.FromDegrees(0);
var up = Direction2.FromDegrees(90);
double cross = right.CrossProduct(up); // +1.0 — up is CCW from right
FromDegrees(double)
Creates a Direction2 from an angle in degrees. Equivalent to
new Direction2(Angle.FromDegrees(degrees)).
public static Direction2 FromDegrees(double degrees)
Parameters
degreesdoubleThe angle in degrees, measured CCW from the positive X-axis.
Returns
- Direction2
A
Direction2pointing in the given direction.
Examples
var up = Direction2.FromDegrees(90);
var left = Direction2.FromDegrees(180);
IsBetween(Direction2, Direction2)
Returns true when this direction falls within the arc from
start to end going counter-clockwise.
Handles wrap-around: if end is less than start
(the arc crosses 0°/360°), an angle is valid if it is ≥ start OR
≤ end.
public bool IsBetween(Direction2 start, Direction2 end)
Parameters
startDirection2The start of the arc (inclusive), measured CCW.
endDirection2The end of the arc (inclusive), measured CCW.
Returns
Examples
var test = Direction2.FromDegrees(0);
bool inArc = test.IsBetween(Direction2.FromDegrees(315), Direction2.FromDegrees(45)); // true
Opposite()
Returns the opposite direction — rotated 180° from this direction.
If this direction is 45°, the opposite is 225°. The Direction vector components are both negated.
public Direction2 Opposite()
Returns
- Direction2
A
Direction2pointing in the exact opposite direction.
Examples
var right = Direction2.FromDegrees(0);
var left = right.Opposite(); // 180°
Rotate(Angle)
Returns a new Direction2 rotated by rotation degrees
counter-clockwise from this direction. The result is normalized to [0°, 360°).
public Direction2 Rotate(Angle rotation)
Parameters
rotationAngleThe angle to rotate by. Positive = CCW, negative = CW.
Returns
- Direction2
A new
Direction2atAngle + rotation(normalized).
Examples
var dir = Direction2.FromDegrees(270);
var rotated = dir.Rotate(Angle.FromDegrees(180)); // 90°
Operators
operator *(Direction2, LengthVector2)
Applies this direction as a component-wise scale to a LengthVector2.
Each component of right is multiplied by the corresponding
component of Direction.
This is a projection-style operation: it keeps the component of
right that aligns with each axis of this direction.
public static LengthVector2 operator *(Direction2 left, LengthVector2 right)
Parameters
leftDirection2The direction (used as a scale mask).
rightLengthVector2The displacement vector to scale.
Returns
- LengthVector2
A component-wise product of Direction and
right.
Examples
var dir = new Direction2(new DoubleVector2(0.6, 0.8));
var v = new LengthVector2(Length.FromMeters(10), Length.FromMeters(10));
LengthVector2 result = dir * v; // (6 m, 8 m)
operator *(Direction2, Length)
Scales this direction by a scalar length, producing a LengthVector2 pointing in this direction with the given magnitude.
public static LengthVector2 operator *(Direction2 left, Length right)
Parameters
leftDirection2The direction.
rightLengthThe scalar length (the distance to travel in this direction).
Returns
- LengthVector2
A LengthVector2 with components
(Direction.X * right, Direction.Y * right).
Examples
var dir = Direction2.FromDegrees(0);
LengthVector2 move = dir * Length.FromMeters(5); // (5 m, 0 m)
operator *(LengthVector2, Direction2)
Applies this direction as a component-wise scale to a LengthVector2
(vector on the left). Equivalent to right * left.
public static LengthVector2 operator *(LengthVector2 left, Direction2 right)
Parameters
leftLengthVector2The displacement vector.
rightDirection2The direction scale mask.
Returns
- LengthVector2
A component-wise product of
leftand Direction.
operator *(Length, Direction2)
Scales this direction by a scalar length (scalar on the left).
Equivalent to right * left.
public static LengthVector2 operator *(Length left, Direction2 right)
Parameters
leftLengthThe scalar length.
rightDirection2The direction.
Returns
- LengthVector2
A LengthVector2 pointing in the given direction.
operator -(Direction2)
Returns the opposite direction. Equivalent to Opposite().
public static Direction2 operator -(Direction2 value)
Parameters
valueDirection2The direction to negate.
Returns
- Direction2
The opposite direction.