Create a triangular array of red and black circles in TikZ

Not sure it is very elegant ...

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \foreach \x [evaluate=\x as \xmod using {int(mod(\x,5))}] in {0,...,19}
  {
    \foreach \y [evaluate=\y as \ymod using {int(mod(\y,5))}, evaluate=\ymod as \k using { \ymod > \xmod ? "black" : "red" }] in {0,...,\x}
    \draw [fill=\k] (\x - \y / 2, \y * 0.866) circle (0.2);
  }
\end{tikzpicture}
\begin{tikzpicture}
  \foreach \i  [evaluate=\i as \imod using {int(mod(\i,5))}] in {1,...,20}
  {
    \foreach \j [evaluate=\j as \jadj using {int(mod((int((floor(\j/6))+\j)),6))} ] in {1,...,\i}
    {
      \def\clr{red}
      \ifnum\i>5\ifnum\imod>0\ifnum\j>1\ifnum\j<\i\ifnum\jadj>\imod\def\clr{black}\fi\fi\fi\fi\fi
      \filldraw [\clr] ( \j - \i/2 , -\i*.866  ) circle (.2);
    }
  }
\end{tikzpicture}
\end{document}

Will yield two copies of

inelegance


Stack it!

\documentclass{article}
\usepackage{stackengine,xcolor}
\stackMath
\renewcommand\useanchorwidth{T}
\newcommand\redc{\textcolor{red}{\bullet}}
\newcommand\blac{\textcolor{black}{\bullet}}
\newcommand\redt{\Shortstack{\redc{} \redc\redc{} \redc\redc\redc{} %
  \redc\redc\redc\redc{} \redc\redc\redc\redc\redc}}
\newcommand\blat{\Shortstack{\blac\blac\blac\blac{} \blac\blac\blac{} %
  \blac\blac{} \blac{} \vphantom{\blac}}}
\setstackgap{S}{-.078ex}
\begin{document}
\LARGE\stackunder[6pt]{%
\stackon{\redt\blat\redt\blat\redt\blat\redt}{%
  \stackon{\redt\blat\redt\blat\redt}{%
    \stackon{\redt\blat\redt}{%
      \redt}}}%
}{\boldmath\normalsize\Longstack{\textsf{\bfseries{}Triangular Numbers} 
  \color{cyan}T(20)=\color{red}T(5)T(4)\color{cyan}+\color{black}T(4)T(3)}}
\end{document}

enter image description here


With a pic:

\documentclass{amsart}
\usepackage{tikz}
\usepackage{ifthen}
\tikzset{%
    pics/blacktri/.style= {code={%
            \foreach \y in {0,...,3}{%
                \foreach \x in {0,...,\y}{%
                    \node[draw=black, fill=black] at (\x - \y/2, \y * 0.866) {};
                }
            }
        }}
    }

\begin{document}
    \begin{figure}
        \begin{tikzpicture}[xscale=0.3,yscale=0.3, 
        every node/.style={circle, minimum size=4pt, inner sep=0pt, outer sep=0pt}]
        \foreach \x in {0,...,19}{%
            \foreach \y in {0,...,\x}{%
                \node[draw=red, fill=red] (P-\x-\y) at (\x - \y/2, \y * 0.866) {};
            }
        }
        \foreach \x in {15,10,5}{%
            \foreach \y in {11,6,1}{%
                \ifthenelse{\y<\x}{%
                    \pic[xscale=0.3,yscale=0.3] at (P-\x-\y) {blacktri};
                }{}
            }
        }
        \end{tikzpicture}
        \caption{The 20th triangular number}
    \end{figure}

\end{document}

enter image description here

Tags:

Tikz Pgf