# Tikz Nodes Order and Shapes with a General Number of Nodes

This is part 2 of Tikz Nodes Positions and Shapes with a General Number of Nodes, a question I was asked to split. Please refer to the code below.

In the table below I'd like to:

Control the direction in which the numbers are running

At this time, the numbers go something like this:

81 82 83 84 85

86 87 88 89 90

But I'd be interested in experimenting with:

81 82 83 84 85

90 89 88 87 86

An already existing example or a sketch would be more than enough.

Ultimately it would be fun to design winding paths, like in kids' board games. A board game is actually what I'm working towards, at a snail's pace.

\RequirePackage[svgnames,x11names]{xcolor}
\documentclass[tikz,convert=false,margin=0pt]{standalone}%
\usepackage{rotating}% sideways environment

\begin{document}%
\begin{tikzpicture}[%
every node/.style = {
align = center
, scale = 2
, anchor = base
, font = \fontfamily{pzc}\selectfont% common font
, text = black
}
]%
%
% Set Grid Dimensions
\newcommand{\xa}{1}
\newcommand{\xb}{5}
\newcommand{\ya}{1}
\newcommand{\yb}{5}
\pgfmathsetmacro{\yc}{\yb-1}% \yb minus one
%
% Change styles of numbers according to set membership
\foreach \x in {\xa,...,\xb}
\foreach \y in {\ya,...,\yb}
{\pgfmathtruncatemacro{\label}{\x - \xb * (\y - \yb) }
\node (\x\y) at (1.5*\x, -1.5*\y) {\label};}
\foreach \x in {\xa,...,\xb}
\foreach \y [count = \yi] in {\ya,...,\yc}
\draw (\x\y)(\x\yi) (\y\x)(\yi\x) ;
\end{tikzpicture}%
\end{document}%


-
I think (as shown in an answer to one of your other questions) it is easier to derive the x and y coordinates from the number rather than the other way round. So, for example, \foreach \n [evaluate={\x=-mod(\n-1,10); \y=-floor((\n-1)/10);}] in {1,...,100}\node at (\x,\y) {\n}; –  Mark Wibrow Feb 3 '14 at 7:31

First, I made your code a little more straightforward, so that the order in which TikZ actually makes the numbers is 1,2,3,... Having that made it easier for me to think about, and so easier to modify (and hopefully easier to adjust in the future). Here is the changed section (this leaves the result unchanged):

\foreach \y in {\ya,...,\yb}
\foreach \x in {\xa,...,\xb}
{\pgfmathtruncatemacro{\label}{\x + (\y-1) * \xb }
\node (\x\y) at (1.5*\x, 1.5*\y) {\label};}


Now the boustrophedon (ooh that's a fun word) numbering. It took a little toying, but the main idea I used is that (-1)^x is a nice step function that changes every integer from -1 to 1 and back. This is perfect for the changing nature of each line, where (going from the left) numbers are increasing in one line and decreasing in the next. So here is the code:

\foreach \y in {\ya,...,\yb}
\foreach \x in {\xa,...,\xb}
{\pgfmathtruncatemacro{\label}
{(-1)^(\y-1)*\x + (\y-1)*\xb + (1-(-1)^(\y-1))*(\xb+1)/2}
\node (\x\y) at (1.5*\x, 1.5*\y) {\label};}


That generates the image

Now the first \x changes sign from line to line, and the last term (1-(-1)^(\y-1))*(\xb+1)/2 switches each line from 0 to 1*\xb+1 = 6 and back. I'd want the indices \xa and \ya to be 0, so that the code might be a little shorter, but in the end it doesn't matter, there's always a function that will do the necessary trick.

-
This is perfect. I'm very grateful for the explanations. There are several answers in tex.stackexchange that use the \foreach construct like the one I had, but they appear more like a clever hand trick than something I could put my mind around. It's very helpful that you showed how to rewrite my code more clearly and how to use the step function to do something else. Thanks Jānis! Oh and +1 for Boustrophedon. I'm on the wiki page now :-) –  PatrickT Feb 3 '14 at 10:29

Just to illustrate my comment above, here is a simple version showing how it can be done. It can be easily combined with the answer given here to provide fancier output.

\documentclass[tikz,border=0.125cm]{standalone}

\begin{document}

\begin{tikzpicture}[x=1cm, y=1cm]
\foreach \n [evaluate={%
\y=-floor((\n-1)/10);
\k=mod(-\y,2);
\x=9*\k-(\k*2-1)*mod(\n-1,10);}] in {1,...,100}
\node [rectangle, minimum size=1cm, draw]
at (\x,\y) {\n};
\end{tikzpicture}

\end{document}


-
Thanks Mark, this is very neat. You have a gift for dividing the number of lines of code severalfold! Thanks for the comment on "it is easier to derive the x and y coordinates from the number rather than the other way round": while it seems obvious now, I did not have a clear understanding at the time. Very helpful. Your answer is excellent: I picked Jānis because a choice had to be made and because I picked your other, brilliant answer. Thanks Mark! –  PatrickT Feb 3 '14 at 10:34

In my question, almost as an afterthought, I had written "Ultimately it would be fun to design winding paths". I'd like to point out here that in an earlier thread I had not seen until after I asked my question, Paul Gaborit shows how to trace a spiralling path:

Sieve of Eratosthenes in tikz

This uses polar coordinates. The difference in syntax is quite small:

\coordinate(A) at (0,0); % cartesian coordinates
\coordinate(A) at (360:0); % polar coordinates, angle=360, radius=0


I have posted an adaptation of the code here, in case someone is interested:

Tikz Nodes Positions and Shapes with a General Number of Nodes

-