Determining Neighbors in a Geometric Hexagon Pattern 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


Think in terms of rings around the central hexagon:

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