Re: [TML] Jump Calculations Christopher Sean Hilton 12 Apr 2020 02:51 UTC

On Fri, Mar 27, 2020 at 06:32:37PM -0400, Jeff Zeitlin wrote:
> Given a standard Traveller subsector, with coordinates 0101 to 0810,
> alternate columns staggered, what's the formula/algorithm to calculate the
> jump distance between two hexes?
>

To start, there is a wealth of information on hex grids here:

* [Hexagonal Grids](https://www.redblobgames.com/grids/hexagons/)

Your question is best answered by the sections on Coordinate systems
and distances:

* [Hex Coordinate systems](https://www.redblobgames.com/grids/hexagons/#coordinates)
* [Hex Distances](https://www.redblobgames.com/grids/hexagons/#distances)

----------------------------------------

If you read the sections it will give you the algorithm. Here's some
python that implements it.

```
class Hex(object):

    def __init__(self, hex_label):
        self.hex_label = hex_label

        ## Break a four digit hex label into it's x and y components

        self.x, self.y = divmod(hex_label, 100)

        ## Calculate an alternate (x, y) in this system two hexes
        ## with the same y value will always be in a row.

        self.alt_x = self.x
        self.alt_y = self.y - (self.x // 2)

        ## Calculate a third set of coordinates, (x, y, z)

        self.cube_x = self.alt_x
        self.cube_y = self.alt_y
        self.cube_z = -(self.cube_x + self.cube_y)

    def distance(self, other):
        """Calculate the distance between two hexes"""
        return max(abs(self.cube_x - other.cube_x),
                   abs(self.cube_y - other.cube_y),
                   abs(self.cube_z - other.cube_z))

def main():

    start = Hex(101)
    finish = Hex(801)

    distance = finish.distance(start)
    print("Distance: {}".format(distance))

```

Here's what the output looks like:

```
$ ipython
Python 3.7.6 (default, Dec 22 2019, 01:09:06)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: class Hex(o\bject):
   ...:
   ...:     def __init__(self, hex_label):
   ...:         self.hex_label = hex_label
[ ...snip... ]

In [2]: main()
Distance: 7
```

--
Chris

      __o          "All I was trying to do was get home from work."
    _`\<,_           -Rosa Parks
___(*)/_(*)____.___o____..___..o...________ooO..._____________________
Christopher Sean Hilton                    [chris/at/vindaloo/dot/com]