Determining Neighbors in a Geometric Hexagon Pattern

2.1k Views Asked by At

Given a hexagonal grid enumerated from a center point (see example), how can one mathematically determine if two hexagons are adjacent to another?

Edit: Asked another way, Given two non negative integers are the corresponding hexagons adjacent?

Example Hexagonal Grid

enter image description here

3

There are 3 best solutions below

0
On

A fact that might be of some use, is that your numbering follows a "spiral" path such that the numbers of hexagons per round is a multiple of $6$, e.g. first round $1-6$, second round $7-18$, $19-36$, etc.

Two hexagons can be adjacent by being neighbours along the spiralling path indicated above, in which case their IDs are also neighbouring, which will be kept in mind.

In this spiralling counting pattern, hexagons such as $7$, $19$, $37$,... are where the path jumps out to a larger circular path. We call them "doors" point.

Such a hexagon labelled $1+ \sum_1^n 6k = 1 + 3n(n+1)$ for some $n$, has neighbours on the outer spiral round $1 + 3(n+1)(n+2)$, $3(n+1)(n+2)$ and $3(n+1)(n+2)-1$. One more neighbour is of the form $3n(n+1)$, and two have lower IDs.

Any hexagon could be seen as a "door" point by hexagonal symmetry, simply by starting counting in a different hexagon among the first six, and following the same "hexagonal spiral" pattern. Will not this lead somewhere (useful)?, I will work it out.

An algorithm could looks as follows:

  1. Given two integers $m$ and $l$ , $m<l$ and $l-l>1$, check if the $m$ is of the form $ 1 + 3n(n+1)$

  2. If not, find an integer $k \in [1,5]$, such that $m-k = 1 + 3n(n+1)$ for some $n$, and go to 3 upon performing the substituions $m \to m-k$ and $l \to l-k$

  3. If yes, check if $l = 1 + 3(n+1)(n+2)$, $l = 3(n+1)(n+2)$ or $l = 3(n+1)(n+2)-1$

0
On

Several suggestions (not an answer)

  • Start with a two dimensional indexing scheme (essentially cartesian). Then use one of the well known algorithms for enumerating pairs of integers to create a single index for each cell. If the range of values has reasonable bounds, packing two integers into one is even easier. To find neighbors, unpack the single index into its corresponding pair of integers, find the neighbors (easy) and convert them to their single indices.

  • You might get neater formulas with the numbering scheme you have if you always start each new ring north of the origin, rather than north of where you ended up in the previous ring.

0
On

Think in terms of rings around the central hexagon: Hexagons

In the image above, darker tone indicates corner hexagon, and lighter tone an edge hexagon.

You can label all hexagons using a single nonnegative integer $i$, $0 \le i \in \mathbb{Z}$; or, equivalently, using the ring number $r$, $0 \le r \in \mathbb{Z}$, and position within the ring $p$, $0 \le p \in \mathbb{Z}$: $$i = \begin{cases} 0, & r = 0 \\ 3 r (r - 1) + 1 + p, & r \ge 1 \end{cases} \tag{1}\label{1}$$ and inversely $$r = \begin{cases} 0, & i = 0 \\ \left \lfloor \frac{3 + \sqrt{12 i - 3}}{6} \right \rfloor, & i \ge 1 \end{cases} \tag{2}\label{2}$$ where $\lfloor \, \rfloor$ denote rounding towards zero, and $$p = i - 3 r ( r - 1 ) - 1 \tag{3}\label{3}$$

Ring $r = 0$ is special, because it has only one hexagon (the center hexagon in white). It's neighbours are the six hexagons in ring $r = 1$; I suggest you number the neighbors in the same order you number the hexagons in ring $r = 1$.

Ring $r = 1$ is kind of special, because it consists of only corner hexagons, but it turns out the same rules below handle this ring as well as all outer rings just fine.

All rings except ring $r = 0$ have $r - 1$ edge hexagons between consecutive corner hexagons.

Using OP's numbering, hexagons $p = r - 1$, $p = 2 r - 1$, $p = 3 r - 1$, $p = 4 r - 1$, $p = 5 r - 1$, and $p = 6 r - 1$ are the corner hexagons, in order, in ring $r$.

When given $i$ for a specific hexagon, I do believe you have three separate cases you need to handle:

  • If $i = 0$, then $r = 0$, $p = 0$, and the neighbors are $1$, $2$, $3$, $4$, $5$, and $6$

    Otherwise, calculate $r$ according to $\eqref{2}$, and then $p$ according to $\eqref{3}$.
     

  • If $p \mod r = r - 1$, this is a corner hexagon, with one neighbor in ring $r-1$, two in the current ring ($i-1$ and $i+1$), and three in ring $r+1$.

    Otherwise,
     

  • This is an edge hexagon, which has two neighbors in ring $r-1$, two in the current ring ($i-1$ and $i+1$), and two in ring $r+1$.

It should not be difficult to formulate the rules for the $(r, p)$ for the neighboring inner and outer ring hexagons; I'm just too lazy to work it out for myself right now.

In particular, remember that each outer ring has one more edge hexagon between corner hexagons: that allows you to compensate for the $p$ indexing along the ring in different rings. One way to do this is to number the edges along the ring from $e = 0$ to $e = 5$, so that $e = \lfloor p / r \rfloor$.