5
\$\begingroup\$

I've been searching for hex grid algorithms for a while now, but I'm not having much luck.

I'm working on a game that will be using hex grids for the board. There seems to be sufficient resources to manage the A* path finding routines, but trying to find something as simple as determining a set of hexes within a triangle seems to be a real challenge.

An example of what I'm looking for is that I want to have a starting coordinate. I want to get a hex that is 3 hexes to the north/east away from the source hex. I want to get a hex that is 3 hexes to the south/east away from the source hex. I then want to produce a list of all hexes bounded by these 3 hexes.

It should be simple enough, but the fact that rows are staggered is completely confounding me.

Thanks

\$\endgroup\$
2
  • 4
    \$\begingroup\$ I have posted a library of Hex Grid Utilities (licensed under the MIT license) here: hexgridutilities.codeplex.com that may be of use. \$\endgroup\$ Apr 15, 2013 at 0:57
  • \$\begingroup\$ Ask away; I will see what I can do. I prefer to retain my email for the moment. \$\endgroup\$ May 15, 2013 at 22:46

1 Answer 1

9
\$\begingroup\$

Use a Canonical (Or oblique) coordinate system (with axes at 120 degrees) to identify the hexes that are in the triangle. Then convert the coordinates of those hexes (with a homogenous transformation) to Rectangular coordinates for display. The utility toolkit I linked to above could be of help in writing this code.

\$\endgroup\$
10
  • \$\begingroup\$ I've spent a short while going through the code in your utility. Once I figure out the meaning of your canonical coordinates, I might be able to make use of the algorithms it uses. \$\endgroup\$ Apr 15, 2013 at 11:55
  • 1
    \$\begingroup\$ I meant to add to the comment - even though much of the code/math is over my head, the code is well written. Thanks! \$\endgroup\$ Apr 15, 2013 at 11:56
  • \$\begingroup\$ Okay, I've solved the question. I couldn't get much out of the source code in Pieter's toolbox, but I was able to use the concept of his answer to come up with a solution. I was going to post the source code, but the comment-size limit is preventing me. If there is a good way to provide source code here, let me know and I'll include it. \$\endgroup\$ Apr 15, 2013 at 16:34
  • \$\begingroup\$ @PaulMouchet: thank you for the complement. If you have specific questions about the code, that could be answered as part of expanding/improving the documentation, I would look at answering them. \$\endgroup\$ Apr 16, 2013 at 2:41
  • \$\begingroup\$ @PieterGeerkens: I think the majority of my difficulty in reading your code isn't a reflection of your documentation (even if I find it a little light), my issue is my understanding of the math related language being used. I'm not familiar with terms like "contravariant, canonical" and I was mis-remembering terms like "vector". My own math weaknesses were making it difficult for me to follow your code logic. That being said, as I spend more time with your code, it's meaning is becoming more apparent to me. \$\endgroup\$ Apr 16, 2013 at 14:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .