Struct TriGridLayout
Defines the spatial layout for a triangular grid, encapsulating the triangle edge
length and world-space origin required to convert between TriCoord lane
coordinates and Unity world-space Vector2 positions.
Implements
Inherited Members
Namespace: Scylla.Core.Structures
Assembly: ScyllaCore.dll
Syntax
public readonly struct TriGridLayout : IEquatable<TriGridLayout>
Remarks
The forward transform (GridToWorld(TriCoord)) computes the centroid of a
triangle from its lane coordinate using the following formulae, where
L is EdgeLength:
x = (0.5·A - 0.5·C) · L + OriginX
y = (-√3/6·A + √3/3·B - √3/6·C) · L + OriginY
The inverse transform (WorldToGrid(Vector2)) maps a world position back to the nearest triangle by ceiling/floor lane detection.
All three constructor parameters are validated to be finite numbers; non-finite or zero EdgeLength values throw ArgumentOutOfRangeException.
Constructors
TriGridLayout(float, float, float)
Creates a new triangle grid layout with the specified edge length and world-space origin.
Declaration
public TriGridLayout(float edgeLength, float originX = 0, float originY = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| float | edgeLength | The edge length of each equilateral triangle in Unity world units. Must be a finite number strictly greater than zero. |
| float | originX | The X world-space position of the grid origin. Defaults to |
| float | originY | The Y world-space position of the grid origin. Defaults to |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when |
Fields
EdgeLength
The edge length of each equilateral triangle, measured in Unity world units. This value scales all coordinate-to-world conversions uniformly.
Declaration
public readonly float EdgeLength
Field Value
| Type | Description |
|---|---|
| float |
OriginX
The X component of the world-space origin of the grid's coordinate system. Triangle centroids are offset by this amount along the X axis.
Declaration
public readonly float OriginX
Field Value
| Type | Description |
|---|---|
| float |
OriginY
The Y component of the world-space origin of the grid's coordinate system. Triangle centroids are offset by this amount along the Y axis.
Declaration
public readonly float OriginY
Field Value
| Type | Description |
|---|---|
| float |
Methods
Equals(TriGridLayout)
Determines whether this layout is identical to another layout by performing exact floating-point equality on all three parameters.
Declaration
public bool Equals(TriGridLayout other)
Parameters
| Type | Name | Description |
|---|---|---|
| TriGridLayout | other | The layout to compare with. |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
This comparison uses exact == equality on floating-point values, which is
intentional - layouts are considered equal only when constructed with the same
literal values. Use this when matching cached layout instances, not for detecting
"approximately same" layouts.
Equals(object)
Determines whether this layout 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 TriGridLayout with equal values. |
Overrides
GetHashCode()
Returns a hash code for this layout.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int | A hash code based on all layout parameters. |
Overrides
GridToWorld(TriCoord)
Converts a triangle lane coordinate to the world-space position of the triangle's centroid.
Declaration
public Vector2 GridToWorld(TriCoord coord)
Parameters
| Type | Name | Description |
|---|---|---|
| TriCoord | coord | The triangle lane coordinate to convert. |
Returns
| Type | Description |
|---|---|
| Vector2 | The world-space |
Remarks
The conversion applies the following formulae (where L = EdgeLength):
x = (0.5·A - 0.5·C) · L + OriginX
y = (-√3/6·A + √3/3·B - √3/6·C) · L + OriginY
Both upward and downward triangles produce their respective centroid positions.
ToString()
Returns a human-readable string representation of this layout.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | A string in the format
|
Overrides
WorldToGrid(Vector2)
Converts a world-space position to the TriCoord of the triangle that contains or is nearest to that position.
Declaration
public TriCoord WorldToGrid(Vector2 worldPos)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | worldPos | The world-space position to convert. |
Returns
| Type | Description |
|---|---|
| TriCoord | The TriCoord of the triangle that contains |
Remarks
The algorithm normalises the world position by the edge length and origin, then uses ceiling and floor lane detection to snap to the enclosing triangle:
px = (worldPos.x - OriginX) / EdgeLength
py = (worldPos.y - OriginY) / EdgeLength
A = ceil(px - √3/3 · py)
B = floor(2√3/3 · py) + 1
C = ceil(-px - √3/3 · py)
The returned coordinate is always a valid triangle (A + B + C equals 1 or 2) for any finite input position.
Operators
operator ==(TriGridLayout, TriGridLayout)
Returns true when left and right
have identical edge length and origin values.
Declaration
public static bool operator ==(TriGridLayout left, TriGridLayout right)
Parameters
| Type | Name | Description |
|---|---|---|
| TriGridLayout | left | The left operand. |
| TriGridLayout | right | The right operand. |
Returns
| Type | Description |
|---|---|
| bool |
|
operator !=(TriGridLayout, TriGridLayout)
Returns true when left and right
differ in at least one parameter.
Declaration
public static bool operator !=(TriGridLayout left, TriGridLayout right)
Parameters
| Type | Name | Description |
|---|---|---|
| TriGridLayout | left | The left operand. |
| TriGridLayout | right | The right operand. |
Returns
| Type | Description |
|---|---|
| bool |
|