FR: Convert from worldspace coordinate to closest hexspace coordinate
did:plc:zviscnpwyvj6y32agi5davn5 opened this 18d ago 1 comments
did:plc:zviscnpwyvj6y32agi5davn5 opened 18d ago
This should be available as <(HexUnitSize, Vec2) as Into<HexCoord>>::into() -> HexCoord. Math-wise, we should be able to calculate the exact cubic hexspace coordinates of the worldspace coordinates as:
q = -x - y / sqrt(3)
r = 2 * y / sqrt(3)
s = x - y / sqrt(3)Then, we take the two closest coordinates to their rounded forms and calculate the third based on their rounded forms. For example:
(x, y) = (1, 2)
q = -1 - 2 / sqrt(3) = -2.1547
r = 2 * 2 / sqrt(3) = 2.3094
s = 1 - 2 / sqrt(3) = -0.1547Here, q and s are closest at q = -2 and s = 0, so by the cubic coordinate axiom q + r + s = 0, r = 2.
Technically all calculations thus far have involved a unit size k = 1. This can be generalized by multiplying all cubic coordinates by 1 / k before the rounding step.
No activity yet.