Table of Contents

Struct Direction2

Namespace
Thunder.UnitsNET.Vectors.Geometry
Assembly
Thunder.UnitsNET.Vectors.Geometry.dll

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

vector DoubleVector2

A 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

from LengthPoint2

The origin point.

to LengthPoint2

The 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

vector LengthVector2

A 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

angle Angle

The 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

DoubleVector2

East

Points along the positive X-axis (0° CCW from +X). Equivalent to Zero.

public static Direction2 East { get; }

Property Value

Direction2

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

Direction2

EastSouthEast

Points 337.5° counter-clockwise from East (ESE). Between south-east and east.

public static Direction2 EastSouthEast { get; }

Property Value

Direction2

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

Direction2

Examples

var up = Direction2.North;
// up.Direction ≈ (0, 1)

NorthEast

Points 45° counter-clockwise from East (NE). Exactly half-way between East and North.

public static Direction2 NorthEast { get; }

Property Value

Direction2

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

Direction2

NorthNorthWest

Points 112.5° counter-clockwise from East (NNW). Between north and north-west.

public static Direction2 NorthNorthWest { get; }

Property Value

Direction2

NorthWest

Points 135° counter-clockwise from East (NW). Exactly half-way between North and West.

public static Direction2 NorthWest { get; }

Property Value

Direction2

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

Direction2

SouthEast

Points 315° counter-clockwise from East (SE). Exactly half-way between South and East.

public static Direction2 SouthEast { get; }

Property Value

Direction2

SouthSouthEast

Points 292.5° counter-clockwise from East (SSE). Between south and south-east.

public static Direction2 SouthSouthEast { get; }

Property Value

Direction2

SouthSouthWest

Points 247.5° counter-clockwise from East (SSW). Between south-west and south.

public static Direction2 SouthSouthWest { get; }

Property Value

Direction2

SouthWest

Points 225° counter-clockwise from East (SW). Exactly half-way between West and South.

public static Direction2 SouthWest { get; }

Property Value

Direction2

West

Points along the negative X-axis (180° CCW from +X). The opposite of East.

public static Direction2 West { get; }

Property Value

Direction2

WestNorthWest

Points 157.5° counter-clockwise from East (WNW). Between north-west and west.

public static Direction2 WestNorthWest { get; }

Property Value

Direction2

WestSouthWest

Points 202.5° counter-clockwise from East (WSW). Between west and south-west.

public static Direction2 WestSouthWest { get; }

Property Value

Direction2

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

Direction2

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

other Direction2

The second direction.

Returns

double

A dimensionless scalar in [−1, 1]: positive = other is 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

degrees double

The angle in degrees, measured CCW from the positive X-axis.

Returns

Direction2

A Direction2 pointing 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

start Direction2

The start of the arc (inclusive), measured CCW.

end Direction2

The end of the arc (inclusive), measured CCW.

Returns

bool

true if this direction is within [start, end] (CCW), handling the 0°/360° wrap.

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 Direction2 pointing 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

rotation Angle

The angle to rotate by. Positive = CCW, negative = CW.

Returns

Direction2

A new Direction2 at Angle + 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

left Direction2

The direction (used as a scale mask).

right LengthVector2

The 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

left Direction2

The direction.

right Length

The 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

left LengthVector2

The displacement vector.

right Direction2

The direction scale mask.

Returns

LengthVector2

A component-wise product of left and 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

left Length

The scalar length.

right Direction2

The 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

value Direction2

The direction to negate.

Returns

Direction2

The opposite direction.

latest ▼