[Tex/LaTex] TikZ: dice/domino faces: loops and looks

tikz-pgf

I recently came across a short discussion of the Schwenk dice and thought it would be fun to create the dice with TikZ. I found neat tizpictures for up to 9 dots and adapted the code to 16 dots. I did it without thinking (see below), but as I was typing (fast) it became clear that the code was ugly and the faces were not that pretty either.

Any suggestions on how to make more general code for faces with large numbers of dots and pleasant symmetries? That is, loops and looks.

The red faces are the ones copied from Andrew Swann's code linked above. The blue faces were copied from some domino designs I had at home. The green faces were made up on the spot.

\documentclass{standalone}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{shapes}
\tikzset{%
  dot hidden/.style={},
  line hidden/.style={},
  dot colour/.style={dot hidden/.append style={color=#1}},
  dot colour/.default=black,
  line colour/.style={line hidden/.append style={color=#1}},
  line colour/.default=black,
}%
\usepackage{xparse}
\NewDocumentCommand{\drawdie}{O{}m}{%
\begin{tikzpicture}[x=1cm,y=1cm,radius=0.06,#1]
\draw[rounded corners=1,line hidden] (0,0) rectangle (1,1);
\ifnum#2<10% "standard die"
  \ifodd#2
    \fill[dot hidden] (0.5,0.5) circle;% 1,3,5,7,9
  \fi
  \ifnum#2>1
    \fill[dot hidden] (0.15,0.15) circle;% 2
    \fill[dot hidden] (0.85,0.85) circle;% 3
    \ifnum#2>3
      \fill[dot hidden] (0.15,0.85) circle;% 4
      \fill[dot hidden] (0.85,0.15) circle;% 5
      \ifnum#2>5
        \fill[dot hidden] (0.85,0.5) circle;% 5
        \fill[dot hidden] (0.15,0.5) circle;% 6
        \ifnum#2>7
          \fill[dot hidden] (0.5,0.85) circle;% 7
          \fill[dot hidden] (0.5,0.15) circle;% 8
        \fi
      \fi
    \fi
  \fi
\fi
\ifnum#2>9% "extended die"
  \ifnum#2<13% 
    \fill[dot hidden] (0.15,0.15) circle;
    \fill[dot hidden] (0.15,0.85) circle;
    \fill[dot hidden] (0.85,0.15) circle;
    \fill[dot hidden] (0.85,0.85) circle;
    \fill[dot hidden] (0.15,0.38) circle;
    \fill[dot hidden] (0.15,0.61) circle;
    \fill[dot hidden] (0.85,0.38) circle;
    \fill[dot hidden] (0.85,0.61) circle;
    \ifodd#2
      \fill[dot hidden] (0.50,0.50) circle;
      \fill[dot hidden] (0.50,0.15) circle;
      \fill[dot hidden] (0.50,0.85) circle;
    \else
      \fill[dot hidden] (0.50,0.38) circle;
      \fill[dot hidden] (0.50,0.61) circle;
    \fi
    \ifnum#2>11
      \fill[dot hidden] (0.50,0.15) circle;
      \fill[dot hidden] (0.50,0.85) circle;
    \fi
  \else
    \fill[dot hidden] (0.15,0.15) circle;
    \fill[dot hidden] (0.15,0.85) circle;
    \fill[dot hidden] (0.85,0.15) circle;
    \fill[dot hidden] (0.85,0.85) circle;
    \fill[dot hidden] (0.15,0.38) circle;
    \fill[dot hidden] (0.15,0.61) circle;
    \fill[dot hidden] (0.85,0.38) circle;
    \fill[dot hidden] (0.85,0.61) circle;  
    \fill[dot hidden] (0.38,0.15) circle;
    \fill[dot hidden] (0.38,0.85) circle;
    \fill[dot hidden] (0.61,0.15) circle;
    \fill[dot hidden] (0.61,0.85) circle;
    \ifnum#2<14
      \fill[dot hidden] (0.50,0.50) circle; 
    \fi
    \ifnum#2>13
      \fill[dot hidden] (0.38,0.38) circle;
      \fill[dot hidden] (0.61,0.61) circle;
      \ifnum#2>14      
        \fill[dot hidden] (0.38,0.61) circle; 
        \ifnum#2>15
          \fill[dot hidden] (0.61,0.38) circle;
        \fi
      \fi
    \fi
  \fi
\fi
\end{tikzpicture}%
}%

\newcommand{\dieA}[1]{%
    \drawdie[color=black, line hidden/.append style={fill=red!70}]{#1}}

\newcommand{\dieB}[1]{%
    \drawdie[color=black, line hidden/.append style={fill=blue!50}]{#1}}

\newcommand{\dieC}[1]{%
    \drawdie[color=black, line hidden/.append style={fill=green!50}]{#1}}

\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,nodes={rectangle,align=center}]{%
\dieA{1}   & \dieA{2}   & \dieA{3}   & \dieA{4} \\
\dieA{5}   & \dieA{6}   & \dieA{7}   & \dieA{8} \\
\dieA{9}   & \dieB{10} & \dieB{11} & \dieB{12} \\
\dieC{13} & \dieC{14} & \dieC{15} & \dieC{16} \\
};
\end{tikzpicture}
\end{document}

enter image description here

Best Answer

Here is a version of your code that avoids nesting of tikzpictures, which also contains some minor changes. Whether my choices of placing the dots are "more symmetric" is probably a matter of taste. I also removed shapes and xcolor as you didn't use them.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix}
\tikzset{%
  dot hidden/.style={},
  line hidden/.style={},
  dot colour/.style={dot hidden/.append style={color=#1}},
  dot colour/.default=black,
  line colour/.style={line hidden/.append style={color=#1}},
  line colour/.default=black,
}%
\usepackage{xparse}
\tikzset{pics/.cd,
dice/.style args={#1/#2}{code={
\draw[rounded corners=1,line hidden,#1] (0,0) rectangle (1,1);
\ifnum#2<10% "standard die"
  \ifodd#2
    \fill[dot hidden] (0.5,0.5) circle(1.5pt);% 1,3,5,7,9
  \fi
  \ifnum#2>1
    \fill[dot hidden] (0.15,0.15) circle(1.5pt);% 2
    \fill[dot hidden] (0.85,0.85) circle(1.5pt);% 3
    \ifnum#2>3
      \fill[dot hidden] (0.15,0.85) circle(1.5pt);% 4
      \fill[dot hidden] (0.85,0.15) circle(1.5pt);% 5
      \ifnum#2>5
        \fill[dot hidden] (0.85,0.5) circle(1.5pt);% 5
        \fill[dot hidden] (0.15,0.5) circle(1.5pt);% 6
        \ifnum#2>7
          \fill[dot hidden] (0.5,0.85) circle(1.5pt);% 7
          \fill[dot hidden] (0.5,0.15) circle(1.5pt);% 8
        \fi
      \fi
    \fi
  \fi
\fi
\ifnum#2>9% "extended die"
  \ifnum#2<13% 
    \fill[dot hidden] (0.15,0.15) circle(1.5pt);
    \fill[dot hidden] (0.15,0.85) circle(1.5pt);
    \fill[dot hidden] (0.85,0.15) circle(1.5pt);
    \fill[dot hidden] (0.85,0.85) circle(1.5pt);
    \fill[dot hidden] (0.15,0.38) circle(1.5pt);
    \fill[dot hidden] (0.15,0.61) circle(1.5pt);
    \fill[dot hidden] (0.85,0.38) circle(1.5pt);
    \fill[dot hidden] (0.85,0.61) circle(1.5pt);
    \ifodd#2
      \fill[dot hidden] (0.50,0.50) circle(1.5pt);
      \fill[dot hidden] (0.50,0.15) circle(1.5pt);
      \fill[dot hidden] (0.50,0.85) circle(1.5pt);
    \else
      \fill[dot hidden] (0.50,0.15) circle(1.5pt); %<-changed
      \fill[dot hidden] (0.50,0.85) circle(1.5pt); %<-changed
    \fi
    \ifnum#2>11
      \fill[dot hidden] (0.50,0.38) circle(1.5pt); 
      \fill[dot hidden] (0.50,0.61) circle(1.5pt); 
    \fi
  \else % here #2 > 12
    \fill[dot hidden] (0.15,0.15) circle(1.5pt);
    \fill[dot hidden] (0.15,0.85) circle(1.5pt);
    \fill[dot hidden] (0.85,0.15) circle(1.5pt);
    \fill[dot hidden] (0.85,0.85) circle(1.5pt);
    \fill[dot hidden] (0.15,0.38) circle(1.5pt);
    \fill[dot hidden] (0.15,0.61) circle(1.5pt);
    \fill[dot hidden] (0.85,0.38) circle(1.5pt);
    \fill[dot hidden] (0.85,0.61) circle(1.5pt);  
    \ifnum#2=14
      \fill[dot hidden] (0.38,0.27) circle(1.5pt);
      \fill[dot hidden] (0.61,0.27) circle(1.5pt);
      \fill[dot hidden] (0.38,0.50) circle(1.5pt);
      \fill[dot hidden] (0.61,0.50) circle(1.5pt);
      \fill[dot hidden] (0.38,0.73) circle(1.5pt);
      \fill[dot hidden] (0.61,0.73) circle(1.5pt);
    \else
      \fill[dot hidden] (0.38,0.15) circle(1.5pt);
      \fill[dot hidden] (0.38,0.85) circle(1.5pt);
      \fill[dot hidden] (0.61,0.15) circle(1.5pt);
      \fill[dot hidden] (0.61,0.85) circle(1.5pt);
    \fi
    \ifnum#2<14
      \fill[dot hidden] (0.50,0.50) circle(1.5pt); 
    \fi
    \ifnum#2>14
      \fill[dot hidden] (0.38,0.61) circle(1.5pt);
      \fill[dot hidden] (0.61,0.61) circle(1.5pt); 
      \ifnum#2=15      
        \fill[dot hidden] (0.5,0.38) circle(1.5pt);        
      \fi   
      \ifnum#2>15
          \fill[dot hidden] (0.38,0.38) circle(1.5pt);
          \fill[dot hidden] (0.61,0.38) circle(1.5pt);
      \fi
    \fi
  \fi
\fi}}
}%

\begin{document}
\begin{tikzpicture}[A/.style={fill=red!70},B/.style={fill=blue!50},
C/.style={fill=green!50}]
\matrix[column sep=3pt,row sep=4pt]{%
\pic {dice=A/1};   & \pic {dice=A/2};   & \pic {dice=A/3};   & \pic {dice=A/4}; \\
\pic {dice=A/5};   & \pic {dice=A/6};   & \pic {dice=A/7};   & \pic {dice=A/8}; \\
\pic {dice=A/9};   & \pic {dice=B/10}; & \pic {dice=B/11}; & \pic {dice=B/12}; \\
\pic {dice=C/13}; & \pic {dice=C/14}; & \pic {dice=C/15}; & \pic {dice=C/16}; \\
}; 
\end{tikzpicture}
\end{document}

enter image description here