hex grid

This is a discussion on hex grid within the C Programming forums, part of the General Programming Boards category; Hi there, I have a hex grid that grows in a spiral and am trying to figure out how to ...

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

    hex grid

    Hi there, I have a hex grid that grows in a spiral and am trying to figure out how to assign (x,y) values to a hexagon.


    / \__/ \__/ \__/ \__/ \__/ \
    \__/ \__/ \__/41\__/ \__/ \__/
    / \__/.. \__/40\__/42\__/ \__/ \
    \__/62\__/39\__/22\__/43\__/ \__/
    / \__/38\__/21\__/23\__/44\__/ \
    \__/61\__/20\__/ 9 \__/24\__/45\__/
    / \__/37\__/ 8 \__/10\__/25\__/ \
    \__/60\__/19\__/ 2 \__/11\__/46\__/
    / \__/36\__/ 7 \__/ 3 \__/26\__/ \
    \__/59\__/18\__/ 1 \__/12\__/47\__/
    / \__/35\__/ 6 \__/ 4 \__/27\__/ \
    \__/58\__/17\__/ 5 \__/13\__/48\__/
    / \__/34\__/16\__/14\__/28\__/ \
    \__/57\__/33\__/15\__/29\__/49\__/
    / \__/56\__/32\__/30\__/50\__/ \
    \__/ \__/55\__/31\__/51\__/ \__/
    / \__/ \__/54\__/52\__/ \__/ \
    \__/ \__/ \__/53\__/ \__/ \__/
    / \__/ \__/ \__/ \__/ \__/ \
    \__/ \__/ \__/ \__/ \__/ \__/

    hex 1 - (0,0)
    2 - (0,1)
    3 - (1,1)
    4 - (1,-1) etc...
    Any ideas on how to actually make the conversion?
    Thanks

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    heh intresting looking problem, but your visual obviously didn't come out like you wanted. I went in to edit your post, see what I could do there. I tried code tages, quotes, lists, ehh everything...wouldn't work. But I'm curious so...I did this:

    (this is his diagram...there's another row, with nothing in it on top like the one on teh bottom, I just couldn't fit it.)
    Attached Images Attached Images  
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can easily represent a hex grid as a 2D array. This is very easy to do:
    Code:
    {0}  {1}  {2}  {3}
       {0}  {1}  {2}  {3}
    {0}  {1}  {2}  {3}
    See how that works out? Each 'hex' connects to the cell directly left and right of it, and at each corner. You treat each row in the hex grid as you would normally, but every other row is offset slightly when displayed. For movement or how it connects, you simply write fomulae to see what each "direction" does:

    Assuming that X goes across, and Y goes verticly, you do the following:

    East = x+1
    West = x-1
    NW = y-1
    NE = y-1,x+1
    SW = y+1
    SE = y+1,x+1

    Enjoy.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 12:30 AM
  2. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  3. More Windows Trouble
    By samGwilliam in forum Windows Programming
    Replies: 9
    Last Post: 04-12-2005, 10:02 AM
  4. Find a word in a 2d grid
    By The_Kingpin in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 04:38 PM
  5. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21