# tikz spiral on hexagonal board

Could any of you help me with a code to render the attached figure? It is an hexagonal board (cells visible) showing a clockwise spiral starting one cell NW to the center and ending 3 cells NW to the center. Thank you very much, ---J.M.S.

• Welcome to TeX.SX! Can you enclose the TeX code that you have tried so far? – gernot Sep 18 '16 at 11:15

It is possible to redefine the coordinate system in TikZ. The example uses a 3D coordinate system. The middle hexagon is the origin (0, 0, 0). The x axis goes to the right below, the y axis to the right above and the z axis to the top.

Full example:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\def\Unit{10mm}
\def\Num{3}
\pgfmathsetlengthmacro\xdiff{cos(30) * \Unit}
\pgfmathsetlengthmacro\ydiff{sin(30) * \Unit}
\tikzset{
x={(\xdiff, -\ydiff)},
y={(\xdiff, \ydiff)},
z={(0, \Unit)},
}
\draw
\foreach \x in {-\Num, ..., \Num} {
\foreach \z in {
\the\numexpr -\Num \ifnum\x>0 +\x \fi \relax,
...,
\the\numexpr \Num \ifnum\x<0 +\x \fi \relax
} {
(\x, 0, \z)
\foreach \i in {1, ..., 5} {
}
-- cycle
}
}
;
\draw[very thick, blue]
(-1, 0) -- (0, 0, 1) -- (0, 1) -- (1, 0) --
(0, 0, -1) -- (-2, 0, -1) -- (-2, 0) -- (-2, 2) --
(0, 2) -- (2, 0) -- (0, 0, -2) -- (-3, 0, -2) --
(-3, 0)
;
\end{tikzpicture}
\end{document}


Well, I'm a fan of automatized code, but in this case it may not be worth it. I think you could just use a plot since it's easier to enter coordinates/node names.

The node names for each node can be "seen" by adding label=center:{h\count\n} to the node options. h stands for hexagon, while the other two variables will add numbers to make each node name unique.

Also, you can render the plot smooth or sharp depending on your needs.

Smooth plot

Sharp plot

## Code

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}

\usetikzlibrary{shapes}

\tikzset{hexa/.style={shape=regular polygon,
regular polygon sides=6,
minimum size=1cm,
draw,
inner sep=0mm,
outer sep=0mm,
anchor=south,
fill=white},
hl/.style={line width=2pt,line cap=round}
}

\begin{document}
\begin{tikzpicture}[x=1cm, y=5mm]

\foreach \m [count=\count] in {1,2,3,4,3,4,3,4,3,4,3,2,1}{
\foreach \n in {1,...,\m}{
\node at ({(\n-\m/2)*sin(30)*3},{\count*sin(60)})
[hexa] (h\count\n) {};
}
}

% Add [smooth] to plot to make it smooth, such as 'plot[smooth]'

\draw[hl] plot coordinates {(h82) (h92) (h83) (h63) (h52) (h71) (h91) (h112) (h93) (h53) (h32) (h61) (h101)};

\end{tikzpicture}
\end{document}