Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6983129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:22:41+00:00 2026-05-27T18:22:41+00:00

To start, let me say that I’m a beginner and this program is my

  • 0

To start, let me say that I’m a beginner and this program is my first attempt at “flying solo” as it were, so please be patient if I sound like a moron.

I have a grid of hexes, and I want to use A* to find a path from one to another.

The hex tiles fit inside an 80 by 80 square, are stored internally as a 2 dimensional array, and referenced in the code by those 2D co-ordinates (i.e, hex[0,0], hex[1,0] etc).

They are displayed to the screen with the “staggering” transformation:

if (X % 2 == 0)
{
    X = (X / 2) * 120;
    Y = Y * 80;
}
else
{
    X = ((X / 2) * 120);
    Y = (Y * 80) + 40;
}

I have my A* implementation set up, but obviously, two hexes out of the 6 adjacent to each hex are counted as being 2 away rather than 1, and which two differs depending on whether X is odd or even.

I’ve tried to read up on ways to calculate the correct difference, but I’m not sure where to begin implementing any of the varying methods I’ve seen around. Is there a simple transformation that I can make to the co-ordinate system I already have purely for the purposes of calculating distance between hexes, or a formula I can use?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:22:42+00:00Added an answer on May 27, 2026 at 6:22 pm

    If I am not mistaken, this should do it:

    int HexDistance(int x1, int y1, int x2, int y2) {
      int y1d = (y1 << 1) | (x1 & 1);
      int y2d = (y2 << 1) | (x2 & 1);
      int dx = Math.Abs(x2 - x1);
      int dyd = Math.Abs(y2d - y1d);
      return (dx < dyd) ? (dyd - dx) / 2 + dx : dx;
    }
    

    Assuming the grid is like this:

    X:   0  1  2  3  4  5  6                                                    
    ------------------------
    Y:   0     0     0     0
            0     0     0
         1     1     1     1
            1     1     1
         2     2     2     2
    

    where verticals and diagonals are adjacent, but horizontals aren’t.

    Basically, first renumber the y coordinates: in even columns, double them; in odd columns, double and add 1. This gives the following grid:

    X:   0  1  2  3  4  5  6                                                    
    ------------------------
    Y:   0     0     0     0
            1     1     1
         2     2     2     2
            3     3     3
         4     4     4     4
    

    Now, if things are on diagonal, it’s easy. If horizontal is bigger, then just count the horizontal difference; if vertical is bigger, count the vertical difference till diagonal, then add the horizontal difference.

    EDIT: I had an error again. Serves me right for trying to code at 5am. Apologies; hope this is okay.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, let me say that this is not homework (I am an A-Level
Before I start with the real question, let me just say that I might
Let me start by saying that I do not advocate this approach, but I
Let's say that in the main thread of a C++11 program I build an
First let me say that I know it's better to use the subprocess module,
To start let me just say that I am new to jQuery as well
Let's say that I have a 4KB buffer that contains at the start a
To start off, let me say that i'm ignorant of both Java, JSP and
Before I start, let me say that I looked at all the similar questions
Let's say that you will start a new ASP.NET web site/application tomorrow. Would you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.