# Hexagon turning

from Red Blob Games
16 Mar 2022

Question in email: given a direction vector, can we figure out how many turns are needed to reach another hex? Let's start by assuming the facing vector is (+1, -1, 0). Here are the number of turns needed to get to any hex:

The logic for the direction vector pointing towards (+1, -1, 0) is:

```function turns_ne(hex) {
if (hex.q === -hex.r && hex.q >= 0)     return 0;
if (hex.q >= -hex.r && hex.q >= -hex.s) return 1;
if (hex.r <= -hex.s && hex.r <= -hex.q) return 1;
if (hex.s >= -hex.r && hex.s >= -hex.q) return 2;
if (hex.s <= -hex.r && hex.s <= -hex.q) return 2;
return 3;
}
```

To make it work for all other directions, we can rotate both the unit vector and the hexagon in question until the unit vector faces (+1, -1, 0). Something like this (untested):

```function turns(hex, facing) {
while (!facing.equals(Hex(+1, -1, 0))) {
facing = facing.rotate_left();
hex    = hex.rotate_left();
}
return turns_ne(hex);
}
```

There's probably a cleaner way to do this that doesn't involve rotation, but since the rotation code is so short that's probably what I'd start with.

Email me , or tweet @redblobgames, or comment: