Struct SquareCoord
Represents an integer coordinate in a square grid, addressed by column and row.
Implements
Inherited Members
Namespace: Scylla.Core.Structures
Assembly: ScyllaCore.dll
Syntax
public readonly struct SquareCoord : IEquatable<SquareCoord>
Remarks
SquareCoord is an immutable value type used to address cells within a square grid.
The column (Col) increases to the right and the row (Row)
increases downward, following screen-space conventions. The origin (0, 0) is located at the
top-left corner of the grid.
The eight static direction vectors (Up, Down, Left, Right, UpLeft, UpRight, DownLeft, DownRight) can be added to any coordinate to step to an adjacent cell and are used by ScyllaSquareGrid<TCell> internally for neighbor queries.
To convert a SquareCoord to a world-space position, use
GridToWorld(SquareCoord) or the equivalent method on a grid instance.
Constructors
SquareCoord(int, int)
Creates a new square grid coordinate at the specified column and row.
Declaration
public SquareCoord(int col, int row)
Parameters
| Type | Name | Description |
|---|---|---|
| int | col | The column index. Negative values address columns to the left of the origin. |
| int | row | The row index. Negative values address rows above the origin. |
Fields
Col
The column index. Increases to the right; may be negative for coordinates left of the origin.
Declaration
public readonly int Col
Field Value
| Type | Description |
|---|---|
| int |
Down
Direction vector pointing down: (0, +1). Row increases by one.
Declaration
public static readonly SquareCoord Down
Field Value
| Type | Description |
|---|---|
| SquareCoord |
DownLeft
Direction vector pointing diagonally down and left: (-1, +1).
Declaration
public static readonly SquareCoord DownLeft
Field Value
| Type | Description |
|---|---|
| SquareCoord |
DownRight
Direction vector pointing diagonally down and right: (+1, +1).
Declaration
public static readonly SquareCoord DownRight
Field Value
| Type | Description |
|---|---|
| SquareCoord |
Left
Direction vector pointing left: (-1, 0). Column decreases by one.
Declaration
public static readonly SquareCoord Left
Field Value
| Type | Description |
|---|---|
| SquareCoord |
Right
Direction vector pointing right: (+1, 0). Column increases by one.
Declaration
public static readonly SquareCoord Right
Field Value
| Type | Description |
|---|---|
| SquareCoord |
Row
The row index. Increases downward; may be negative for coordinates above the origin.
Declaration
public readonly int Row
Field Value
| Type | Description |
|---|---|
| int |
Up
Direction vector pointing up: (0, -1). Row decreases by one.
Declaration
public static readonly SquareCoord Up
Field Value
| Type | Description |
|---|---|
| SquareCoord |
Remarks
In screen-space row-major layout the row axis points downward, so moving "up" decrements the row index.
UpLeft
Direction vector pointing diagonally up and left: (-1, -1).
Declaration
public static readonly SquareCoord UpLeft
Field Value
| Type | Description |
|---|---|
| SquareCoord |
UpRight
Direction vector pointing diagonally up and right: (+1, -1).
Declaration
public static readonly SquareCoord UpRight
Field Value
| Type | Description |
|---|---|
| SquareCoord |
Zero
The origin coordinate (0, 0), located at the top-left of the grid.
Declaration
public static readonly SquareCoord Zero
Field Value
| Type | Description |
|---|---|
| SquareCoord |
Methods
ChebyshevDistanceTo(SquareCoord)
Computes the Chebyshev distance from this coordinate to other.
Declaration
public int ChebyshevDistanceTo(SquareCoord other)
Parameters
| Type | Name | Description |
|---|---|---|
| SquareCoord | other | The target coordinate to measure distance to. |
Returns
| Type | Description |
|---|---|
| int | The non-negative Chebyshev distance as an int. Returns 0 when
|
Remarks
The Chebyshev distance is defined as max(|Col - other.Col|, |Row - other.Row|).
It represents the minimum number of steps when diagonal moves cost the same as
cardinal moves, and is the correct admissible heuristic for A* on grids that use
Moore movement with uniform diagonal cost.
The subtraction is performed in 64-bit arithmetic to prevent integer overflow when coordinates span the full int range.
See Also
DistanceTo(SquareCoord, SquareDistanceMetric)
Computes the distance from this coordinate to other using
the specified distance metric.
Declaration
public float DistanceTo(SquareCoord other, SquareDistanceMetric metric)
Parameters
| Type | Name | Description |
|---|---|---|
| SquareCoord | other | The target coordinate to measure distance to. |
| SquareDistanceMetric | metric | The distance metric to apply. |
Returns
| Type | Description |
|---|---|
| float | The distance as a float. Always non-negative; returns 0 when
|
Remarks
Each metric is suited to a different movement model:
- Manhattan - optimal heuristic for 4-connected (VonNeumann) grids.
- Chebyshev - optimal heuristic for 8-connected grids where diagonal steps cost the same as cardinal steps.
- Euclidean - straight-line geometric distance; underestimates on grids and should be used with care as an A* heuristic.
-
Octile - recommended A* heuristic for
8-way movement where diagonals cost sqrt(2):
max(dx,dy) + (sqrt(2)-1)*min(dx,dy).
For integer results without floating-point overhead, prefer ManhattanDistanceTo(SquareCoord) or ChebyshevDistanceTo(SquareCoord).
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when |
Equals(SquareCoord)
Determines whether this coordinate is equal to other.
Declaration
public bool Equals(SquareCoord other)
Parameters
| Type | Name | Description |
|---|---|---|
| SquareCoord | other | The coordinate to compare with. |
Returns
| Type | Description |
|---|---|
| bool |
Equals(object)
Determines whether this coordinate is equal to an arbitrary object.
Declaration
public override bool Equals(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | The object to compare with. May be |
Returns
| Type | Description |
|---|---|
| bool |
|
Overrides
GetHashCode()
Returns a hash code for this coordinate based on its column and row values.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int | A hash code suitable for use in hash-based collections such as Dictionary<TKey, TValue>. |
Overrides
ManhattanDistanceTo(SquareCoord)
Computes the Manhattan distance from this coordinate to other.
Declaration
public int ManhattanDistanceTo(SquareCoord other)
Parameters
| Type | Name | Description |
|---|---|---|
| SquareCoord | other | The target coordinate to measure distance to. |
Returns
| Type | Description |
|---|---|
| int | The non-negative Manhattan distance as an int. Returns 0 when
|
Remarks
The Manhattan distance is defined as |Col - other.Col| + |Row - other.Row|.
It represents the minimum number of cardinal-direction (N/S/E/W) steps required to
reach other on a grid with no obstacles, and is the correct
admissible heuristic for A* pathfinding on grids that use
VonNeumann movement.
The subtraction is performed in 64-bit arithmetic to prevent integer overflow when coordinates span the full int range.
See Also
ToString()
Returns a human-readable string representation of this coordinate.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | A string in the format |
Overrides
Operators
operator +(SquareCoord, SquareCoord)
Adds two coordinates component-wise, producing a new coordinate offset from
a by b.
Declaration
public static SquareCoord operator +(SquareCoord a, SquareCoord b)
Parameters
| Type | Name | Description |
|---|---|---|
| SquareCoord | a | |
| SquareCoord | b |
Returns
| Type | Description |
|---|---|
| SquareCoord |
Remarks
This operator is the primary mechanism for stepping in a direction. For example,
coord + SquareCoord.Right moves one cell to the right.
operator ==(SquareCoord, SquareCoord)
Returns true if left and right have
equal column and row values.
Declaration
public static bool operator ==(SquareCoord left, SquareCoord right)
Parameters
| Type | Name | Description |
|---|---|---|
| SquareCoord | left | |
| SquareCoord | right |
Returns
| Type | Description |
|---|---|
| bool |
operator !=(SquareCoord, SquareCoord)
Returns true if left and right differ
in at least one component.
Declaration
public static bool operator !=(SquareCoord left, SquareCoord right)
Parameters
| Type | Name | Description |
|---|---|---|
| SquareCoord | left | |
| SquareCoord | right |
Returns
| Type | Description |
|---|---|
| bool |
operator *(SquareCoord, int)
Scales both components of coord by scalar.
Useful for computing ring radii or region extents in grid units.
Declaration
public static SquareCoord operator *(SquareCoord coord, int scalar)
Parameters
| Type | Name | Description |
|---|---|---|
| SquareCoord | coord | |
| int | scalar |
Returns
| Type | Description |
|---|---|
| SquareCoord |
operator *(int, SquareCoord)
Scales both components of coord by scalar.
Commutative overload of coord * scalar.
Declaration
public static SquareCoord operator *(int scalar, SquareCoord coord)
Parameters
| Type | Name | Description |
|---|---|---|
| int | scalar | |
| SquareCoord | coord |
Returns
| Type | Description |
|---|---|
| SquareCoord |
operator -(SquareCoord, SquareCoord)
Subtracts b from a component-wise,
returning the displacement vector from b to a.
Declaration
public static SquareCoord operator -(SquareCoord a, SquareCoord b)
Parameters
| Type | Name | Description |
|---|---|---|
| SquareCoord | a | |
| SquareCoord | b |
Returns
| Type | Description |
|---|---|
| SquareCoord |
operator -(SquareCoord)
Returns a new coordinate with both components negated, effectively mirroring
coord through the origin.
Declaration
public static SquareCoord operator -(SquareCoord coord)
Parameters
| Type | Name | Description |
|---|---|---|
| SquareCoord | coord |
Returns
| Type | Description |
|---|---|
| SquareCoord |