# Hex node distance formula

**URL:** <https://discussions.unity.com/t/hex-node-distance-formula/861164>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [November 6, 2021, 1:18pm UTC](https://discussions.unity.com/t/hex-node-distance-formula/861164 "2021-11-06T13:18:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fuzzypup](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/fuzzypup/32/405632_2.png) [@Fuzzypup](https://discussions.unity.com/u/Fuzzypup)\
**Post date:** [November 6, 2021, 1:18pm UTC](https://discussions.unity.com/t/hex-node-distance-formula/861164/1 "2021-11-06T13:18:32Z")

</div>

I can’t seem to find a simple formula to calculate this.

This is the closest I can get buy there are 2 paths depending if the row is odd or even on the X axis that calculate half the numbers 1 higher.

```csharp
    public static int HexDistance(int sx, int sy, int ex, int ey) {
        int xd = Math.Abs(sx - ex);
        int yd = (int)Math.Abs(sy - ey);
        int d = xd + Math.Max(0, yd - (xd / 2));
        return d;
    }

```

---

<div class="post-metadata">

**Author:** ![Kurt-Dekker](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kurt-dekker/32/101578_2.png) [@Kurt-Dekker](https://discussions.unity.com/u/Kurt-Dekker)\
**Post date:** [November 6, 2021, 1:39pm UTC](https://discussions.unity.com/t/hex-node-distance-formula/861164/2 "2021-11-06T13:39:43Z")

</div>

This answer is going to be completely dependent on how you order your cell addresses. I’ve not mucked with the Unity hex-mode tilemaps so I can’t speak to how they are laid out, but I do keep these handy-dandy hex links:

Hexagonal maps (hexmaps):

> **[Red Blob Games: Hexagonal Grids](https://www.redblobgames.com/grids/hexagons/)**
>
> Amit's guide to math, algorithms, and code for hexagonal grids

> **[Unity Hex Map Tutorials](https://catlikecoding.com/unity/tutorials/hex-map/)**
>
> A tutorial series about the creation of hex maps in Unity.

---

<div class="post-metadata">

**Author:** ![Fuzzypup](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/fuzzypup/32/405632_2.png) [@Fuzzypup](https://discussions.unity.com/u/Fuzzypup)\
**Post date:** [November 6, 2021, 2:36pm UTC](https://discussions.unity.com/t/hex-node-distance-formula/861164/3 "2021-11-06T14:36:21Z")

</div>

Just need a simple distance formula. Not all this complicated work. Tried these solutions which didn’t work either.

```csharp
        int xd = Math.Abs(sx - ex);
        int dx = (x2 - (int)Mathf.Floor(y2 / 2)) - (x1 - (int)Mathf.Floor(y1 / 2));
        int dy = y2 - y1;
        int dist = Mathf.Max(Mathf.Abs(dx), Mathf.Abs(dy), Mathf.Abs(dx + dy));
        return dist;

//OR

        int dx = x2 - x1;
        int dy = y2 - y1;
        int dist;
        if (Mathf.Sign(dx) == Mathf.Sign(dy)) {
            dist = Math.Max(Mathf.Abs(dx), Mathf.Abs(dy));
        } else {
            dist = Mathf.Abs(dx) + Mathf.Abs(dy);
        }

```

---

<div class="post-metadata">

**Author:** ![Fuzzypup](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/fuzzypup/32/405632_2.png) [@Fuzzypup](https://discussions.unity.com/u/Fuzzypup)\
**Post date:** [November 6, 2021, 6:18pm UTC](https://discussions.unity.com/t/hex-node-distance-formula/861164/4 "2021-11-06T18:18:37Z")

</div>

I have tried every single formula even the double width and all of them come out with errors.  
I have 5 different methods doing this and none work correctly. Of course I have the one model has none of the articles I saw is using which is the 0,0 being lower left and (1,1) is 1 right, 1 up being offset UP half a hex.

This is what it looks like with 0, 0 being the lower left of the map, 1, 1 is offest up from there.

I can’t figure out what I am doing wrong.

![7634692--950581--upload_2021-11-6_14-18-16.png](https://europe1.discourse-cdn.com/unity/original/4X/8/1/0/810859e714333f2ab79c36e734bbe34beb506271.png)

![7634692--950575--upload_2021-11-6_14-14-10.png](https://europe1.discourse-cdn.com/unity/original/4X/8/4/4/84497b726e831c764ef423d7ca907b0e9537c1ca.png)

---

<div class="post-metadata">

**Author:** ![exiguous](https://avatars.discourse-cdn.com/v4/letter/e/47e85d/32.png) [@exiguous](https://discussions.unity.com/u/exiguous)\
**Post date:** [November 6, 2021, 7:47pm UTC](https://discussions.unity.com/t/hex-node-distance-formula/861164/5 "2021-11-06T19:47:03Z")

</div>

> [@""](#):
>
> I can’t figure out what I am doing wrong.

You did not read the resources Kurt provided carefully. Especially in the first link the paragraph about Distances introduces cube coordinates (using 3 axis instead of two) which you should definitely use when you deal with hexagons. I also found [this](http://keekerdc.com/2011/03/hexagon-grids-coordinate-systems-and-distance-calculations/) a usefull resource explaining the concepts and thoughts. But unfortunately the site is down and it’s not available in web archive. But author seems to provide his email adress so maybe you could contact him directly.

---

<div class="post-metadata">

**Author:** ![Kurt-Dekker](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kurt-dekker/32/101578_2.png) [@Kurt-Dekker](https://discussions.unity.com/u/Kurt-Dekker)\
**Post date:** [November 6, 2021, 7:51pm UTC](https://discussions.unity.com/t/hex-node-distance-formula/861164/6 "2021-11-06T19:51:42Z")

</div>

> [@Fuzzypup](#):
>
> I have tried every single formula even the double width and all of them come out with errors.

You really should consider trying the ones I linked in my first response. It’s like @exig says: it literally tells you ALL the ways you can handle these coordinate systems.

You are on extremely well trodden ground: put away the machete and flame thrower and stop chopping / burning a fresh path through virgin jungle wilderness, unless you’re into that and don’t mind the malaria and splinters.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/system/32/97283_2.png) [@system](https://discussions.unity.com/u/system)\
**Post date:** [November 6, 2021, 8:22pm UTC](https://discussions.unity.com/t/hex-node-distance-formula/861164/7 "2021-11-06T20:22:05Z")

</div>

> [@Fuzzypup](#):
>
> Just need a simple distance formula. Not all this complicated work.

You’re at the wrong place then and in the wrong industry.

---

<div class="post-metadata">

**Author:** ![Fuzzypup](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/fuzzypup/32/405632_2.png) [@Fuzzypup](https://discussions.unity.com/u/Fuzzypup)\
**Post date:** [November 6, 2021, 10:37pm UTC](https://discussions.unity.com/t/hex-node-distance-formula/861164/8 "2021-11-06T22:37:23Z")

</div>

In the right industry. I have 2 of 2 successful games that have earned me more than in my previous profession.

There are always easier ways to do things.

The RedBlob games post is confusing and not clear on how to do it. I am finding it very difficult to understand what he is writing.

I converted to the 3 coordinate system following his formulas but it is still not right. The xyz aren’t matching correctly and the distances aren’t.

```csharp
    struct Cube {
        public int x, y, z;
    }

    public static int HexDistance(int x1, int y1, int x2, int y2) {
        Cube cube = new Cube();
        cube.x = x1 - x2;
        cube.y = y1 - y2;
        cube.z = -cube.x - cube.y;
        return (Mathf.Abs(cube.x) + Mathf.Abs(cube.y) + Mathf.Abs(cube.z)) / 2;
    }

```

---

<div class="post-metadata">

**Author:** ![Fuzzypup](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/fuzzypup/32/405632_2.png) [@Fuzzypup](https://discussions.unity.com/u/Fuzzypup)\
**Post date:** [November 7, 2021, 2:23pm UTC](https://discussions.unity.com/t/hex-node-distance-formula/861164/9 "2021-11-07T14:23:10Z")

</div>

I asked Amit from Red Blob games. I had a Homer SImpson moment. While I have a custom made mesh for my wargames in which I odd-q shift it. I forgot to add it to my distance formula. As soon as I read the first line of his email… DOH!..

For those having the same issues this is the formula written in rq.

```csharp
        // convert odd-q to axial
        int q1 = x1;
        int r1 = y1 - (x1 - (x1 & 1)) / 2;

        // convert odd-q to axial
        int q2 = x2;
        int r2 = y2 - (x2 - (x2 & 1)) / 2;

        int qd = q1 - q2;
        int rd = r1 - r2;

        int dist = (Mathf.Abs(qd) + Mathf.Abs(qd + rd) + Mathf.Abs(rd)) / 2;

```

And this is the formula simplified using xy only. 1,0 is shifted up.

FLAT top 2nd column shifted up… odd-q

```csharp
    public static int HexDistance(int x1, int y1, int x2, int y2) {

        int xd = x1 - x2;
        int yd = y1 - ((x1 - (x1 & 1)) / 2) - (y2 - (x2 - (x2 & 1)) / 2);
        int dist = (Mathf.Abs(xd) + Mathf.Abs(xd + yd) + Mathf.Abs(yd)) / 2;
        return dist;
    }

```

FLAT top 2nd column shifted down… even-q

```csharp
    public static int HexDistance(int x1, int y1, int x2, int y2) {

        int xd = x1 - x2;
        int yd = y1 - ((x1 + (x1 & 1)) / 2) - (y2 - (x2 + (x2 & 1)) / 2);
        int dist = (Mathf.Abs(xd) + Mathf.Abs(xd + yd) + Mathf.Abs(yd)) / 2;
        return dist;
    }

```

---

<div class="post-metadata">

**Author:** ![SeerSucker69](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/seersucker69/32/909239_2.png) [@SeerSucker69](https://discussions.unity.com/u/SeerSucker69)\
**Post date:** [September 18, 2022, 11:43am UTC](https://discussions.unity.com/t/hex-node-distance-formula/861164/10 "2022-09-18T11:43:35Z")

</div>

Thanks Fuzzypop, this saved my bacon tonight, I can confirm it works perfectly, I appreciate you taking the time to put them up for us to cut and paste in.
