Thread: Hex distance

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    Hex distance

    I have a hex grid with xy coordinates and am trying to find the shortest hop distance between to hexagons. Is there a way to edit the basic distance formula to do this? or is there more I'll need to do? Thanks

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Just a guess. You could build a graph, where the nodes are the hexagons and each node points to 6 other nodes, then use a breadth search (is that the correct name? It's the opposite of a depth search) to find the closest way between two nodes (hexagons).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    10
    a breadth search? a couple questions for that. 1 - what is it? 2 - would I actually need to create the grid in memory, or would the x,y coordinates be enough? thanks

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I can't say for sure, since I have no experience in hex grids.

    Here is an explanation of the breadth-first search algorithm:
    http://marina.fortunecity.com/nelson/457/search.html

    (If the info is not enough, do a search on google for more)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    You you could use the excessively OO apporach, make each hexagon an independant object with pointers to it's six neighbors, and then send a broadcast ping message, that would remember the series of nodes it had passed through on its way, and once it hit a target node it would backtrack along the message path until it got home, and the distance would be a factor of the number of hops of the shourtest route...


  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Basicly you create a double linked list, except that instead of two links, you have six. (As per the previous replies.)
    Code:
    struct hex
    {
        struct hex *dir[6];
        int used;
        int start;
        int end;
    };
    Then, you basicly tag one node as start, and one as end. On your way down the path, you mark the node as used. Then whenever you can't move any more, you backtrack, or once you've reached the end, you unmark all of your nodes and try another way. It's really up to you at that point.

    Recursion works nicely for this. You also basicly can build a list of directions for your path with something ending up like:

    14621364261362136

    You could basicly just return this as a string, and just use strlen to compare path lengths. (That's the benifit of using an array of pointers rather than six individual ones--although you could make a lookup function or table and end up with the same thing, the array just is easier.)

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The advantage of the breadth-first algorithm is that the first path you find to the other node is the shortest, so no comparing is needed.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    10
    Well, i found a simple solution, but it isn't 100% correct, works about half of the time, here it is:

    int dist = 0;

    while((x1 != x2) || (y1 != y2))
    {
    if(x1 > x2) x1 = x1 - 1;
    if(x1 < x2) x1 = x1 + 1;
    if(y1 > y2) y1 = y1 -1;
    if(y1 < y2) y1 = y2 + 1;
    dist++;
    }

    again I say, it works, but not great, i'll probably experiment around with breadth first searches to make it 100% correct
    thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 06:37 AM
  2. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 12:30 AM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Distance Formula in my program..... I need help fast!!!
    By Mackology101 in forum C Programming
    Replies: 3
    Last Post: 09-23-2004, 10:10 PM
  5. Fuzzy Logic
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-13-2002, 04:58 PM