3

I have a problem where I need to represent hexagons on a tile by their centre (which I refer to as a node) in my graph. Given a tile of hexagons, how can I find if two hexagons x and y are connected?

enter image description here
(source: domathtogether.com)

The following would work on hexagons with their position in two dimensional space, however I want to represent their position by an integer coordinate (0, 1), (0, 2), (0, 3), (1, 1), (1, 2), (1, 3) etc.

if (n1->getPoint().getEuclideanDistance(n2->getPoint()) < diameter)
{
    // The two are connected.
}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • 1
    Assuming all your hexagons have the same size, you only need to apply an offset on every other row for the horizontal position. The vertical step always stays the same. That way you can switch from integer coordinates to euclidean coordinates and you can use your original approach. – Zeta Mar 27 '14 at 11:05
  • 1
    What's this `combinedRadius`? I assumed that "connected" means the hexagons have a shared edge. – Dialecticus Mar 27 '14 at 11:13
  • It's just the value of ```d``` or ```2r```. All hexagons will have the same constant radius. –  Mar 27 '14 at 11:19

1 Answers1

2

Let us assume that the hexagons are numbered 012345 (top row 0), 0123456 (middle row 1), 012345 (bottom row 2): they touch when either they are

  • in the same row at indexes that differ by +/-1, or

  • in rows differing by +/-1 and indexes equal or differing by +1 or -1 depending on the row parity.