-
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
-
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.)
-
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.