Show / Hide Table of Contents

Struct Direction

Structure representing the concept of a "direction" on a grid, and "defines" the coordinate plane via the YIncreasesUpward flag. Interacts with Point to allow easy translation of positions in a direction, and contains numerous helper functions for retrieving directions in various orders, getting direction closest to a line, etc.

Implements
IEquatable<Direction>
IMatchable<Direction>
Inherited Members
object.GetType()
object.Equals(object, object)
object.ReferenceEquals(object, object)
Namespace: SadRogue.Primitives
Assembly: TheSadRogue.Primitives.dll
Syntax
[DataContract]
public readonly struct Direction : IEquatable<Direction>, IMatchable<Direction>
Remarks

The static YIncreasesUpward flag defines the way that many algorithms interpret the coordinate plane. By default, this flag is false, meaning that the y-value of positions is assumed to DECREASE as you proceed in the direction defined by Up, and increase as you go downward. If the coordinate plane is displayed on the screen, the origin would be the top left corner. This default setting matches the typical console/computer graphic definition of the coordinate plane. Setting the flag to true inverts this, so that the y-value of positions INCREASES as you proceed in the direction defined by Up. This places the origin in the bottom left corner, and matches a typical mathematical definition of a euclidean coordinate plane, as well as the scene coordinate plane defined by Unity and other game engines.

Fields

View Source

Down

Down direction.

Declaration
public static readonly Direction Down
Field Value
Type Description
Direction
View Source

DownLeft

Down-left direction.

Declaration
public static readonly Direction DownLeft
Field Value
Type Description
Direction
View Source

DownRight

Down-right direction.

Declaration
public static readonly Direction DownRight
Field Value
Type Description
Direction
View Source

Left

Left direction.

Declaration
public static readonly Direction Left
Field Value
Type Description
Direction
View Source

None

No direction.

Declaration
public static readonly Direction None
Field Value
Type Description
Direction
View Source

Right

Right direction.

Declaration
public static readonly Direction Right
Field Value
Type Description
Direction
View Source

Type

Enum type corresponding to direction being represented.

Declaration
[DataMember]
public readonly Direction.Types Type
Field Value
Type Description
Direction.Types
View Source

Up

Up direction.

Declaration
public static readonly Direction Up
Field Value
Type Description
Direction
View Source

UpLeft

Up-left direction.

Declaration
public static readonly Direction UpLeft
Field Value
Type Description
Direction
View Source

UpRight

Up-right direction.

Declaration
public static readonly Direction UpRight
Field Value
Type Description
Direction

Properties

View Source

DeltaX

Change in x-value represented by this direction.

Declaration
public int DeltaX { get; }
Property Value
Type Description
int
View Source

DeltaY

Change in y-value represented by this direction.

Declaration
public int DeltaY { get; }
Property Value
Type Description
int
View Source

YIncreasesUpward

Whether or not a positive y-value indicates an upward change. To set this value, use SetYIncreasesUpwardsUnsafe(bool), however note that this is an unsafe operation in a multi-threaded environment where one or more threads may be using Directions. It is intended that this configuration be done as part of an initialization routine.

Declaration
public static bool YIncreasesUpward { get; }
Property Value
Type Description
bool
Remarks

If true, directions with an upwards component represent a positive change in y-value, and ones with downward components represent a negative change in y-value. Changing this to false (which is the default) inverts this.

Methods

View Source

Equals(Direction)

True if the given direction has the same Type the current one.

Declaration
public bool Equals(Direction other)
Parameters
Type Name Description
Direction other

Direction to compare.

Returns
Type Description
bool

True if the two directions are the same, false if not.

View Source

Equals(object?)

Same as operator == in this case; returns false if obj is not a Direction.

Declaration
public override bool Equals(object? obj)
Parameters
Type Name Description
object obj

The object to compare the current Direction to.

Returns
Type Description
bool

True if obj is a Direction, and the two directions are equal, false otherwise.

Overrides
ValueType.Equals(object)
View Source

GetCardinalDirection(Point)

Returns the cardinal direction that most closely matches the degree heading of a line with the given delta-change values. Rounds clockwise if exactly on a diagonal. Similar to GetDirection(Point), except this function returns only cardinal directions.

Declaration
public static Direction GetCardinalDirection(Point deltaChange)
Parameters
Type Name Description
Point deltaChange

Vector representing the change in x and change in y across the line (deltaChange.X is the change in x, deltaChange.Y is the change in y).

Returns
Type Description
Direction

The cardinal direction that most closely matches the degree heading of the given line.

View Source

GetCardinalDirection(Point, Point)

Returns the cardinal direction that most closely matches the degree heading of the given line. Rounds clockwise if the heading is exactly on a diagonal direction. Similar to GetDirection(Point, Point), except this function returns only cardinal directions.

Declaration
public static Direction GetCardinalDirection(Point start, Point end)
Parameters
Type Name Description
Point start

Starting coordinate of the line.

Point end

Ending coordinate of the line.

Returns
Type Description
Direction

The cardinal direction that most closely matches the heading indicated by the given line.

View Source

GetCardinalDirection(int, int)

Returns the cardinal direction that most closely matches the degree heading of a line with the given delta-change values. Rounds clockwise if exactly on a diagonal. Similar to GetDirection(Point), except this function returns only cardinal directions.

Declaration
public static Direction GetCardinalDirection(int dx, int dy)
Parameters
Type Name Description
int dx

Change in x along the line.

int dy

Change in y along the line.

Returns
Type Description
Direction

The cardinal direction that most closely matches the degree heading of the given line.

View Source

GetCardinalDirection(int, int, int, int)

Returns the cardinal direction that most closely matches the degree heading of the given line. Rounds clockwise if the heading is exactly on a diagonal direction. Similar to GetDirection(Point, Point), except this function returns only cardinal directions.

Declaration
public static Direction GetCardinalDirection(int startX, int startY, int endX, int endY)
Parameters
Type Name Description
int startX

X-value of the starting coordinate of the line.

int startY

Y-value of the starting coordinate of the line.

int endX

X-value of the ending coordinate of the line.

int endY

Y-value of the ending coordinate of the line.

Returns
Type Description
Direction

The cardinal direction that most closely matches the heading indicated by the given line.

View Source

GetDirection(Point)

Returns the direction that most closely matches the degree heading of a line with the given delta-change values. Rounds clockwise if the heading is exactly between two directions.

Declaration
public static Direction GetDirection(Point deltaChange)
Parameters
Type Name Description
Point deltaChange

Vector representing the change in x and change in y across the line (deltaChange.X is the change in x, deltaChange.Y is the change in y).

Returns
Type Description
Direction

The direction that most closely matches the heading indicated by the given input.

View Source

GetDirection(Point, Point)

Returns the direction that most closely matches the degree heading of the given line. Rounds clockwise if the heading is exactly between two directions.

Declaration
public static Direction GetDirection(Point start, Point end)
Parameters
Type Name Description
Point start

Starting coordinate of the line.

Point end

Ending coordinate of the line.

Returns
Type Description
Direction

The direction that most closely matches the heading indicated by the given line.

View Source

GetDirection(int, int)

Returns the direction that most closely matches the degree heading of a line with the given delta-change values. Rounds clockwise if the heading is exactly between two directions.

Declaration
public static Direction GetDirection(int dx, int dy)
Parameters
Type Name Description
int dx

Change in x-value across the line.

int dy

Change in y-value across the line.

Returns
Type Description
Direction

The direction that most closely matches the heading indicated by the given input.

View Source

GetDirection(int, int, int, int)

Returns the direction that most closely matches the degree heading of the given line. Rounds clockwise if the heading is exactly between two directions.

Declaration
public static Direction GetDirection(int startX, int startY, int endX, int endY)
Parameters
Type Name Description
int startX

X-value of the starting coordinate of the line.

int startY

Y-value of the starting coordinate of the line.

int endX

X-value of the ending coordinate of the line.

int endY

Y-value of the ending coordinate of the line.

Returns
Type Description
Direction

The direction that most closely matches the heading indicated by the given line.

View Source

GetHashCode()

Returns a hash-map value for the current object.

Declaration
public override int GetHashCode()
Returns
Type Description
int
Overrides
ValueType.GetHashCode()
View Source

IsCardinal()

Returns true if the current direction is a cardinal direction.

Declaration
public bool IsCardinal()
Returns
Type Description
bool

True if the current direction is a cardinal direction, false otherwise.

View Source

Matches(Direction)

True if the given direction has the same Type the current one.

Declaration
public bool Matches(Direction other)
Parameters
Type Name Description
Direction other

Direction to compare.

Returns
Type Description
bool

True if the two directions are the same, false if not.

View Source

SetYIncreasesUpwardsUnsafe(bool)

Changes the value of YIncreasesUpward. This operation is not safe to perform if another thread may be directly or indirectly using Directions. It is intended that this value be set once to match your environment as part of an initialization routine.

Declaration
public static void SetYIncreasesUpwardsUnsafe(bool newValue)
Parameters
Type Name Description
bool newValue

New value to assign to YIncreasesUpward.

View Source

ToString()

Writes the string (eg. "UP", "UP_RIGHT", etc.) for the direction.

Declaration
public override string ToString()
Returns
Type Description
string

String representation of the direction.

Overrides
ValueType.ToString()

Operators

View Source

operator +(Direction, int)

Moves the direction clockwise i times.

Declaration
public static Direction operator +(Direction d, int i)
Parameters
Type Name Description
Direction d
int i
Returns
Type Description
Direction

The given direction moved clockwise i times.

View Source

operator +((int x, int y), Direction)

Translates the given position by one unit in the given direction.

Declaration
public static (int x, int y) operator +((int x, int y) tuple, Direction d)
Parameters
Type Name Description
(int x, int y) tuple
Direction d
Returns
Type Description
(int x, int y)

Tuple (tuple.y + d.DeltaX, tuple.y + d.DeltaY).

View Source

operator --(Direction)

Moves the direction counter-clockwise by one.

Declaration
public static Direction operator --(Direction d)
Parameters
Type Name Description
Direction d
Returns
Type Description
Direction

The direction one unit counterclockwise of d.

View Source

operator ==(Direction, Direction)

True if the two directions have the same Type.

Declaration
public static bool operator ==(Direction lhs, Direction rhs)
Parameters
Type Name Description
Direction lhs
Direction rhs
Returns
Type Description
bool

True if the two directions are equal, false if not.

View Source

implicit operator Types(Direction)

Implicitly converts a Direction to its corresponding Type.

Declaration
public static implicit operator Direction.Types(Direction direction)
Parameters
Type Name Description
Direction direction
Returns
Type Description
Direction.Types
View Source

implicit operator Direction(Types)

Implicitly converts an Direction.Types enum value to its corresponding Direction.

Declaration
public static implicit operator Direction(Direction.Types type)
Parameters
Type Name Description
Direction.Types type
Returns
Type Description
Direction
View Source

operator ++(Direction)

Moves the direction clockwise by one.

Declaration
public static Direction operator ++(Direction d)
Parameters
Type Name Description
Direction d
Returns
Type Description
Direction

The direction one unit clockwise of d.

View Source

operator !=(Direction, Direction)

True if the types are not equal.

Declaration
public static bool operator !=(Direction lhs, Direction rhs)
Parameters
Type Name Description
Direction lhs
Direction rhs
Returns
Type Description
bool

True if the types are not equal, false if they are both equal.

View Source

operator -(Direction, int)

Moves the direction counter-clockwise i times.

Declaration
public static Direction operator -(Direction d, int i)
Parameters
Type Name Description
Direction d
int i
Returns
Type Description
Direction

The given direction moved counter-clockwise i times.

Implements

IEquatable<T>
IMatchable<T>

Extension Methods

PropertyChangedEventHelpers.SafelySetProperty<TObject, TProperty>(TObject, ref TProperty, TProperty, EventHandler<ValueChangedEventArgs<TProperty>>?)
PropertyChangedEventHelpers.SafelySetProperty<TObject, TProperty>(TObject, ref TProperty, TProperty, EventHandler<ValueChangedEventArgs<TProperty>>?, EventHandler<ValueChangedEventArgs<TProperty>>?)
  • View Source
In this article
Back to top Generated by DocFX