Struct HexEdgeCoord
Represents a canonical edge in a hexagonal grid, identified by the anchor hex coordinate and the edge direction (E, NE, or NW).
Implements
Inherited Members
Namespace: Scylla.Core.Structures
Assembly: ScyllaCore.dll
Syntax
public readonly struct HexEdgeCoord : IEquatable<HexEdgeCoord>
Remarks
Every interior edge of a hex grid is shared by two hexes. The canonical form avoids duplication by always expressing the edge in terms of the three outward-facing directions of a chosen anchor hex (E, NE, NW). The other three directions (W, SW, SE) are represented as the E, NE, or NW edge of a neighboring cell respectively. See HexEdgeDirection for the exact equivalences.
Use GetCellsNonAlloc(HexEdgeCoord, Span<HexCoord>) to retrieve the two hexes that share this edge, GetCornersNonAlloc(HexEdgeCoord, Span<HexCornerCoord>) to retrieve the two corner vertices at either end, and GetEdgesOfCellNonAlloc(HexCoord, Span<HexEdgeCoord>) to enumerate all six edges of a given cell.
HexEdgeCoord is a value type safe for use as a dictionary key. Use HexEdgeMap<TData> for per-edge data storage.
Constructors
HexEdgeCoord(int, int, HexEdgeDirection)
Creates a new hex edge coordinate from an anchor hex and a canonical edge direction.
Declaration
public HexEdgeCoord(int q, int r, HexEdgeDirection direction)
Parameters
| Type | Name | Description |
|---|---|---|
| int | q | The Q axis value of the anchor hex. |
| int | r | The R axis value of the anchor hex. |
| HexEdgeDirection | direction | The edge direction relative to the anchor hex. Should be one of E, NE, or NW to be in canonical form; other values are accepted but may break uniqueness guarantees. |
Fields
Direction
The edge direction relative to the anchor hex. Always one of E, NE, or NW; never W, SW, or SE (those are represented as the canonical edge of a neighboring cell).
Declaration
public readonly HexEdgeDirection Direction
Field Value
| Type | Description |
|---|---|
| HexEdgeDirection |
Q
The Q axis value of the anchor hex that owns this edge in the canonical representation.
Declaration
public readonly int Q
Field Value
| Type | Description |
|---|---|
| int |
R
The R axis value of the anchor hex that owns this edge in the canonical representation.
Declaration
public readonly int R
Field Value
| Type | Description |
|---|---|
| int |
Methods
Equals(HexEdgeCoord)
Determines whether this edge coordinate equals another edge coordinate.
Declaration
public bool Equals(HexEdgeCoord other)
Parameters
| Type | Name | Description |
|---|---|---|
| HexEdgeCoord | other | The edge coordinate to compare with. |
Returns
| Type | Description |
|---|---|
| bool | True if all fields are equal. |
Equals(object)
Determines whether this edge coordinate equals another object.
Declaration
public override bool Equals(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | The object to compare with. |
Returns
| Type | Description |
|---|---|
| bool | True if the object is a HexEdgeCoord with equal values. |
Overrides
GetCellsNonAlloc(HexEdgeCoord, Span<HexCoord>)
Writes the two hex cells that share this edge into the provided buffer.
Declaration
public static int GetCellsNonAlloc(HexEdgeCoord edge, Span<HexCoord> buffer)
Parameters
| Type | Name | Description |
|---|---|---|
| HexEdgeCoord | edge | The canonical edge coordinate. |
| Span<HexCoord> | buffer | A span of at least 2 elements to receive the cell coordinates.
|
Returns
| Type | Description |
|---|---|
| int | The number of cells written: 2 if the buffer is large enough, 0 otherwise. |
Remarks
The two cells for each direction are:
- E edge of (q, r): cells (q, r) and (q+1, r).
- NE edge of (q, r): cells (q, r) and (q+1, r-1).
- NW edge of (q, r): cells (q, r) and (q, r-1).
If buffer has fewer than 2 elements, no data is written.
GetCornersNonAlloc(HexEdgeCoord, Span<HexCornerCoord>)
Writes the two HexCornerCoord vertices that form the endpoints of this edge into the provided buffer.
Declaration
public static int GetCornersNonAlloc(HexEdgeCoord edge, Span<HexCornerCoord> buffer)
Parameters
| Type | Name | Description |
|---|---|---|
| HexEdgeCoord | edge | The canonical edge coordinate. |
| Span<HexCornerCoord> | buffer | A span of at least 2 elements to receive the corner coordinates. |
Returns
| Type | Description |
|---|---|
| int | The number of corners written: 2 if the buffer is large enough, 0 otherwise. |
Remarks
The corner-to-edge mapping for each canonical direction is:
- (q, r, E): corners (q+1, r-1, S) and (q, r+1, N).
- (q, r, NE): corners (q, r, N) and (q+1, r-1, S).
- (q, r, NW): corners (q, r, N) and (q, r-1, S).
If buffer has fewer than 2 elements, no data is written.
GetEdgesOfCellNonAlloc(HexCoord, Span<HexEdgeCoord>)
Writes the six canonical HexEdgeCoord values for all edges of the given hex cell into the provided buffer. Each of the six edges is returned in its canonical form (anchor hex with an E, NE, or NW direction).
Declaration
public static int GetEdgesOfCellNonAlloc(HexCoord cell, Span<HexEdgeCoord> buffer)
Parameters
| Type | Name | Description |
|---|---|---|
| HexCoord | cell | The hex cell whose edges to enumerate. |
| Span<HexEdgeCoord> | buffer | A span of at least 6 elements to receive the edge coordinates. |
Returns
| Type | Description |
|---|---|
| int | The number of edges written: 6 if the buffer is large enough, 0 otherwise. |
Remarks
The six canonical edges of cell (q, r) are:
- (q, r, E) - east edge.
- (q, r, NE) - northeast edge.
- (q, r, NW) - northwest edge.
- (q-1, r, E) - west edge (E of western neighbor).
- (q-1, r+1, NE) - southwest edge (NE of SW neighbor).
- (q, r+1, NW) - southeast edge (NW of SE neighbor).
If buffer has fewer than 6 elements, no data is written.
GetHashCode()
Returns a hash code for this edge coordinate.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int | A hash code based on Q, R, and direction. |
Overrides
ToString()
Returns a string representation of this edge coordinate.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | A string in the format "(Q, R, Direction)". |
Overrides
Operators
operator ==(HexEdgeCoord, HexEdgeCoord)
Returns true if left and right
have equal Q, R, and Direction values.
Declaration
public static bool operator ==(HexEdgeCoord left, HexEdgeCoord right)
Parameters
| Type | Name | Description |
|---|---|---|
| HexEdgeCoord | left | The left operand. |
| HexEdgeCoord | right | The right operand. |
Returns
| Type | Description |
|---|---|
| bool |
operator !=(HexEdgeCoord, HexEdgeCoord)
Returns true if left and right
differ in Q, R, or Direction.
Declaration
public static bool operator !=(HexEdgeCoord left, HexEdgeCoord right)
Parameters
| Type | Name | Description |
|---|---|---|
| HexEdgeCoord | left | The left operand. |
| HexEdgeCoord | right | The right operand. |
Returns
| Type | Description |
|---|---|
| bool |