Struct FractionalHexCoord
Represents a fractional (floating-point) coordinate in hexagonal cube space. Used as an intermediate value during world-to-grid conversion and line drawing, then rounded to the nearest HexCoord using the cube rounding algorithm.
Implements
Inherited Members
Namespace: Scylla.Core.Structures
Assembly: ScyllaCore.dll
Syntax
public readonly struct FractionalHexCoord : IEquatable<FractionalHexCoord>
Remarks
FractionalHexCoord follows the same axial convention as HexCoord:
only Q and R are stored; S is computed on demand as -Q - R.
The primary use cases are:
- World-to-grid snapping: WorldToGrid(Vector2) internally creates a FractionalHexCoord and calls Round().
- Hex line drawing: Lerp(FractionalHexCoord, FractionalHexCoord, float) interpolates between two integer endpoints to produce a sequence of fractional samples that are individually rounded.
Equality comparison uses exact floating-point equality (bit-identical Q and R values). For approximate comparison, round both values first and compare the resulting HexCoord instances.
Constructors
FractionalHexCoord(float, float)
Creates a new fractional hex coordinate with the specified axial values.
Declaration
public FractionalHexCoord(float q, float r)
Parameters
| Type | Name | Description |
|---|---|---|
| float | q | The fractional Q axis value (column). May be any finite float. |
| float | r | The fractional R axis value (row). May be any finite float. |
Fields
Q
The fractional Q axis value (column in axial coordinates).
Declaration
public readonly float Q
Field Value
| Type | Description |
|---|---|
| float |
R
The fractional R axis value (row in axial coordinates).
Declaration
public readonly float R
Field Value
| Type | Description |
|---|---|
| float |
Properties
S
The computed fractional S axis value: -Q - R.
Not stored directly; computed on demand to maintain the cube constraint Q + R + S = 0.
Declaration
public float S { get; }
Property Value
| Type | Description |
|---|---|
| float |
Methods
Equals(FractionalHexCoord)
Determines whether this coordinate equals another coordinate using exact float comparison. For approximate comparison, use Round() and compare the resulting HexCoord values.
Declaration
public bool Equals(FractionalHexCoord other)
Parameters
| Type | Name | Description |
|---|---|---|
| FractionalHexCoord | other | The coordinate to compare with. |
Returns
| Type | Description |
|---|---|
| bool | True if both coordinates have identical Q and R values. |
Equals(object)
Determines whether this 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 FractionalHexCoord with equal values. |
Overrides
GetHashCode()
Returns a hash code for this fractional coordinate.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int | A hash code based on Q and R. |
Overrides
Lerp(FractionalHexCoord, FractionalHexCoord, float)
Linearly interpolates between two fractional hex coordinates. Interpolation is applied independently to the Q and R axes. The result can be rounded to the nearest integer hex using Round(), which is the basis for hex line drawing.
Declaration
public static FractionalHexCoord Lerp(FractionalHexCoord a, FractionalHexCoord b, float t)
Parameters
| Type | Name | Description |
|---|---|---|
| FractionalHexCoord | a | The start coordinate ( |
| FractionalHexCoord | b | The end coordinate ( |
| float | t | The interpolation factor. Values outside [0, 1] extrapolate beyond the segment endpoints. |
Returns
| Type | Description |
|---|---|
| FractionalHexCoord | The interpolated fractional coordinate between |
Round()
Rounds this fractional coordinate to the nearest integer hex coordinate using the cube rounding algorithm (Charles Fu, 1994).
Declaration
public HexCoord Round()
Returns
| Type | Description |
|---|---|
| HexCoord | The HexCoord whose center is closest to this fractional position in cube space. |
Remarks
The algorithm rounds all three cube components (Q, R, S) independently, then detects
which component has the largest rounding error. That component is recomputed from the
other two (roundQ = -roundR - roundS, etc.) to satisfy the constraint
Q + R + S = 0. This is the standard robust hex-rounding technique and avoids
bias artifacts that simpler rounding strategies produce near hex boundaries.
ToString()
Returns a string representation of this fractional coordinate.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | A string in the format "(Q, R)". |
Overrides
Operators
operator ==(FractionalHexCoord, FractionalHexCoord)
Returns true if left and right
have bit-identical Q and R values.
Declaration
public static bool operator ==(FractionalHexCoord left, FractionalHexCoord right)
Parameters
| Type | Name | Description |
|---|---|---|
| FractionalHexCoord | left | The left operand. |
| FractionalHexCoord | right | The right operand. |
Returns
| Type | Description |
|---|---|
| bool |
operator !=(FractionalHexCoord, FractionalHexCoord)
Returns true if left and right
differ in Q or R.
Declaration
public static bool operator !=(FractionalHexCoord left, FractionalHexCoord right)
Parameters
| Type | Name | Description |
|---|---|---|
| FractionalHexCoord | left | The left operand. |
| FractionalHexCoord | right | The right operand. |
Returns
| Type | Description |
|---|---|
| bool |