6

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:

Hexagonal spiral

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}]
\pgfmathsetmacro\radius{4}
\pgfmathsetmacro\diameter{\radius*2}
\pgfmathsetmacro\shortdiameter{\radius*2-1}
\pgfmathsetmacro\largeradius{\radius+1}
\pgfmathsetmacro\shortradius{\radius-1}

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

\foreach \j in {0,...,\shortradius}{%
  \pgfmathsetmacro\end{\shortdiameter-\j}
  \foreach \i in {0,...,\end}{%
    \node[hexa] (h\i;\j) at ({(\i+\j/2-\shortradius*0.5)*sin(60)},{\j*0.75+\radius*0.75+0.75}) {}; %{\i;\j};
  }
}

% color change(s)
\node[hexa, fill=blue!20!white] (h\radius;\radius) at ({(\radius-\radius/2)*sin(60)},{\radius*0.75}) {};

% 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] (h3;0) at ({(3+0/2-\shortradius*0.5+0.5)*sin(60)},{0*0.75+\radius*0.75+0.75}) {4};
\node[numbr] (h4;0) at ({(4+0/2-\shortradius*0.5+0.5)*sin(60)},{0*0.75+\radius*0.75+0.75}) {5};
\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}
3
  • 2
    In case you haven't seen this before, this blog post is a must read if you are working with hexagonal grids.
    – Aditya
    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?
    – CroCo
    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.
    – Cryptc
    Oct 29, 2015 at 13:58

1 Answer 1

8

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}

enter image description here

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.