Class 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.
Namespace: SadRogue.Primitives
Assembly: TheSadRogue.Primitives.dll
Syntax
public sealed class Direction : ValueType, 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
Down
Down direction.
Declaration
public static readonly Direction Down
Field Value
| Type | Description |
|---|---|
| Direction |
DownLeft
Down-left direction.
Declaration
public static readonly Direction DownLeft
Field Value
| Type | Description |
|---|---|
| Direction |
DownRight
Down-right direction.
Declaration
public static readonly Direction DownRight
Field Value
| Type | Description |
|---|---|
| Direction |
Left
Left direction.
Declaration
public static readonly Direction Left
Field Value
| Type | Description |
|---|---|
| Direction |
None
No direction.
Declaration
public static readonly Direction None
Field Value
| Type | Description |
|---|---|
| Direction |
Right
Right direction.
Declaration
public static readonly Direction Right
Field Value
| Type | Description |
|---|---|
| Direction |
Type
Enum type corresponding to direction being represented.
Declaration
public readonly Direction.Types Type
Field Value
| Type | Description |
|---|---|
| Direction.Types |
Up
Up direction.
Declaration
public static readonly Direction Up
Field Value
| Type | Description |
|---|---|
| Direction |
UpLeft
Up-left direction.
Declaration
public static readonly Direction UpLeft
Field Value
| Type | Description |
|---|---|
| Direction |
UpRight
Up-right direction.
Declaration
public static readonly Direction UpRight
Field Value
| Type | Description |
|---|---|
| Direction |
Properties
DeltaX
Change in x-value represented by this direction.
Declaration
public int DeltaX { get; }
Property Value
| Type | Description |
|---|---|
| Int32 |
DeltaY
Change in y-value represented by this direction.
Declaration
public int DeltaY { get; }
Property Value
| Type | Description |
|---|---|
| Int32 |
YIncreasesUpward
Whether or not a positive y-value indicates an upward change. To set this value, use SetYIncreasesUpwardsUnsafe(Boolean), 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 |
|---|---|
| Boolean |
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
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 |
|---|---|
| Boolean | True if the two directions are the same, false if not. |
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 |
|---|---|
| Boolean | True if |
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. |
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. |
GetCardinalDirection(Int32, Int32)
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 |
|---|---|---|
| Int32 | dx | Change in x along the line. |
| Int32 | 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. |
GetCardinalDirection(Int32, Int32, Int32, Int32)
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 |
|---|---|---|
| Int32 | startX | X-value of the starting coordinate of the line. |
| Int32 | startY | Y-value of the starting coordinate of the line. |
| Int32 | endX | X-value of the ending coordinate of the line. |
| Int32 | 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. |
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. |
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. |
GetDirection(Int32, Int32)
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 |
|---|---|---|
| Int32 | dx | Change in x-value across the line. |
| Int32 | 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. |
GetDirection(Int32, Int32, Int32, Int32)
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 |
|---|---|---|
| Int32 | startX | X-value of the starting coordinate of the line. |
| Int32 | startY | Y-value of the starting coordinate of the line. |
| Int32 | endX | X-value of the ending coordinate of the line. |
| Int32 | 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. |
GetHashCode()
Returns a hash-map value for the current object.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| Int32 |
IsCardinal()
Returns true if the current direction is a cardinal direction.
Declaration
public bool IsCardinal()
Returns
| Type | Description |
|---|---|
| Boolean | True if the current direction is a cardinal direction, false otherwise. |
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 |
|---|---|
| Boolean | True if the two directions are the same, false if not. |
SetYIncreasesUpwardsUnsafe(Boolean)
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 |
|---|---|---|
| Boolean | newValue | New value to assign to YIncreasesUpward. |
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. |
Operators
Addition(Direction, Int32)
Moves the direction clockwise i times.
Declaration
public static Direction operator +(Direction d, int i)
Parameters
| Type | Name | Description |
|---|---|---|
| Direction | d | |
| Int32 | i |
Returns
| Type | Description |
|---|---|
| Direction | The given direction moved clockwise |
Addition((Int32, Int32), Direction)
Translates the given position by one unit in the given direction.
Declaration
public static (int, int) operator +((int, int) tuple, Direction d)
Parameters
| Type | Name | Description |
|---|---|---|
| (, )<Int32, Int32> | tuple | |
| Direction | d |
Returns
| Type | Description |
|---|---|
| (, )<Int32, Int32> | Tuple (tuple.y + d.DeltaX, tuple.y + d.DeltaY). |
Decrement(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 |
Equality(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 |
|---|---|
| Boolean | True if the two directions are equal, false if not. |
Implicit(Direction to Direction.Types)
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 |
Implicit(Direction.Types to Direction)
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 |
Increment(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 |
Inequality(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 |
|---|---|
| Boolean | True if the types are not equal, false if they are both equal. |
Subtraction(Direction, Int32)
Moves the direction counter-clockwise i times.
Declaration
public static Direction operator -(Direction d, int i)
Parameters
| Type | Name | Description |
|---|---|---|
| Direction | d | |
| Int32 | i |
Returns
| Type | Description |
|---|---|
| Direction | The given direction moved counter-clockwise |