Class Distance
Class representing methods of calculating distance on a grid. You cannot create instances of this class using a constructor -- instead this class contains static instances representing the various distance calculations.
Implements
Namespace: SadRogue.Primitives
Assembly: TheSadRogue.Primitives.dll
Syntax
public abstract class Distance : Object, IMatchable<Distance>
Remarks
Provides functions that calculate the distance between two points according to the distance measurement being used. Instances of Distance are also implicitly convertible to both Radius and AdjacencyRule (since both a method of determining adjacent locations and a radius shape are implied by a distance calculation).
Note that, although this class is abstract, you cannot practically create your own subclasses. Each subclass requires an entry in Distance.Types to function properly. The class is only abstract in order to allow an internal implementation which will maximize performance.
Constructors
Distance(Distance.Types)
Creates a new Distance class to represent the distance calculation specified by the type.
Declaration
protected Distance(Distance.Types type)
Parameters
Type | Name | Description |
---|---|---|
Distance.Types | type |
Fields
Chebyshev
Represents chebyshev distance (equivalent to 8-way movement with no extra cost for diagonals).
Declaration
public static ChebyshevDistance Chebyshev
Field Value
Type | Description |
---|---|
ChebyshevDistance |
Euclidean
Represents euclidean distance (equivalent to 8-way movement with ~1.41 movement cost for diagonals).
Declaration
public static EuclideanDistance Euclidean
Field Value
Type | Description |
---|---|
EuclideanDistance |
Manhattan
Represents manhattan distance (equivalent to 4-way, cardinal-only movement).
Declaration
public static ManhattanDistance Manhattan
Field Value
Type | Description |
---|---|
ManhattanDistance |
Properties
Type
Enum value representing the method of calculating distance -- useful for using Distance types in switch statements.
Declaration
public Distance.Types Type { get; }
Property Value
Type | Description |
---|---|
Distance.Types |
Methods
Calculate(Point)
Returns the distance between two locations, given the change in X and change in Y value (specified by the X and Y values of the given vector).
Declaration
public double Calculate(Point deltaChange)
Parameters
Type | Name | Description |
---|---|---|
Point | deltaChange | The delta-x and delta-y between the two locations. |
Returns
Type | Description |
---|---|
Double | The distance between two locations withe the given delta-change values. |
Calculate(Point, Point)
Returns the distance between the two (2D) points specified.
Declaration
public double Calculate(Point start, Point end)
Parameters
Type | Name | Description |
---|---|---|
Point | start | Starting point. |
Point | end | Ending point. |
Returns
Type | Description |
---|---|
Double | The distance between the two points. |
Calculate(Double, Double)
Returns the distance between two locations, given the change in X and change in Y value.
Declaration
public abstract double Calculate(double dx, double dy)
Parameters
Type | Name | Description |
---|---|---|
Double | dx | The delta-x between the two locations. |
Double | dy | The delta-y between the two locations. |
Returns
Type | Description |
---|---|
Double | The distance between two locations with the given delta-change values. |
Calculate(Double, Double, Double, Double)
Returns the distance between the two (2D) points specified.
Declaration
public double Calculate(double startX, double startY, double endX, double endY)
Parameters
Type | Name | Description |
---|---|---|
Double | startX | X-Coordinate of the starting point. |
Double | startY | Y-Coordinate of the starting point. |
Double | endX | X-Coordinate of the ending point. |
Double | endY | Y-Coordinate of the ending point. |
Returns
Type | Description |
---|---|
Double | The distance between the two points. |
GetHashCode()
Returns a hash-map value for the current object.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
Int32 |
Matches(Distance)
True if the given Distance has the same Type the current one.
Declaration
public bool Matches(Distance other)
Parameters
Type | Name | Description |
---|---|---|
Distance | other | Distance to compare. |
Returns
Type | Description |
---|---|
Boolean | True if the two distance calculation methods are the same, false if not. |
ToString()
Returns a string representation of the distance calculation method represented.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
String | A string representation of the distance method represented. |
Operators
Implicit(Distance to AdjacencyRule)
Allows implicit casting to the AdjacencyRule type.
Declaration
public static implicit operator AdjacencyRule(Distance distance)
Parameters
Type | Name | Description |
---|---|---|
Distance | distance |
Returns
Type | Description |
---|---|
AdjacencyRule |
Remarks
The adjacency rule corresponding to the definition of a adjacency according to the distance calculation casted will be returned.
Implicit(Distance to Distance.Types)
Implicitly converts a Distance to its corresponding Type.
Declaration
public static implicit operator Distance.Types(Distance distance)
Parameters
Type | Name | Description |
---|---|---|
Distance | distance |
Returns
Type | Description |
---|---|
Distance.Types |
Implicit(Distance to Radius)
Allows explicit casting to Radius type.
Declaration
public static implicit operator Radius(Distance distance)
Parameters
Type | Name | Description |
---|---|---|
Distance | distance |
Returns
Type | Description |
---|---|
Radius |
Remarks
The 2D radius shape corresponding to the definition of a radius according to the distance calculation casted will be returned.
Implicit(Distance.Types to Distance)
Implicitly converts an Distance.Types enum value to its corresponding Distance.
Declaration
public static implicit operator Distance(Distance.Types type)
Parameters
Type | Name | Description |
---|---|---|
Distance.Types | type |
Returns
Type | Description |
---|---|
Distance |