# Is there an easy way to number a hexagonal spiral?

I created a hexagonal spiral, but ran into a problem while trying to number it. The spiral is created in 2 sections. I can manually number the whole thing, as I did below, but I haven't been able to find an easier method.

I read about a way to number a spiral, iterating through the rings, but it didn't seem to work very well with how TeX generates the shapes. Here is that method:

function cube_spiral(center, radius):
var results = [center]
for each 1 ≤ k ≤ radius:
results = results + cube_ring(center, k)
return results


My hexagonal grid is a variant of this grid.

What is should look like:

Code to generate the hexagonal spiral (with partial manual numbering):

\documentclass{standalone}
\usepackage{tikz}
\usepackage{ifthen} % if else statements
\usetikzlibrary{shapes} %allows hexagons

\begin{document}
\begin{tikzpicture} [hexa/.style= {shape=regular polygon, regular polygon sides=6, minimum size=1cm, draw, inner sep=0,     anchor=south, fill=darkgray!85!white, rotate=0},numbr/.style= {white},arro/.style= {draw,red,thick}]

% base hexagon (created in 2 sections and rotated 90 degrees)
\begin{scope}[rotate=90]
\foreach \i in {0,...,\end}{%
\node[hexa] (h\i;\j) at ({(\i-\j/2)*sin(60)},{\j*0.75}) {}; %{\i;\j};
}
}

\pgfmathsetmacro\end{\shortdiameter-\j}
\foreach \i in {0,...,\end}{%
}
}

% color change(s)

% manual numbering
\node (h4;4) at ({(4-4/2+0.5)*sin(60)},{4*0.75}) {0};

\node[numbr] (h4;3) at ({(4-3/2+0.5)*sin(60)},{3*0.75}) {1};
\node[numbr] (h3;3) at ({(3-3/2+0.5)*sin(60)},{3*0.75}) {2};
\node[numbr] (h3;4) at ({(3-4/2+0.5)*sin(60)},{4*0.75}) {3};
\node[numbr] (h5;4) at ({(5-4/2+0.5)*sin(60)},{4*0.75}) {6};

\node[numbr] (h5;3) at ({(5-3/2+0.5)*sin(60)},{3*0.75}) {7};
% ... etc ...

% special shape(s)
\pgfmathsetmacro\arrowlen{0.2}
\foreach \i in {4,...,7}{%
\draw[arro, ->, to path={-- (\tikztotarget)}]
({(\i-4/2+0.5)*sin(60)+\arrowlen/2},{4*0.75-\arrowlen}) to ({(\i-3/2+0.5)*sin(60)-\arrowlen/2},{3*0.75+\arrowlen});
}

\end{scope}
\end{tikzpicture}
\end{document}

• In case you haven't seen this before, this blog post is a must read if you are working with hexagonal grids. Oct 29, 2015 at 2:33
• please provide the final expected output you are looking for. What are the replacements of the shifted cells in your picture? Oct 29, 2015 at 4:17
• @CroCo The grid I provided is the final expected output. The red arrows show where the numbering uniformly continues on the next outer ring (not the shifting/replacing of cells). Manual numbering becomes cumbersome as the grid grows. Oct 29, 2015 at 13:58

Here is one possible solution.

\documentclass[border=7pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\tikzset{
hexagone/.style={
draw, thick,
fill=blue!7,
shape=regular polygon,
regular polygon sides=6,
outer sep=0, inner sep=0,
minimum size=1cm,
label={[red]center:#1}
}
}
\begin{document}
\begin{tikzpicture}
\node[hexagone=0] at (0,0){};
\foreach \r in {1,...,3}
\foreach \t in {0,...,5}
\foreach[evaluate={\l=int(\r*(\r-1)*3+\r*\t+\u)}] \u in {1,...,\r}
\scoped[rotate=-\t*60]
\node[hexagone=\l] at (0+.75*\u,{0.43301270189*(2*\r-\u)}){};
\end{tikzpicture}
\end{document}


Note: I haven't checked what is the maximal possible number of "rings", but for 21, \foreach \r in {1,...,21}, it works :)