\begin{tikzpicture}[rotate=-90, scale=0.2] \def\n{50} \pgfmathparse{\n+1} \xdef\nn{\pgfmathresult} \foreach \i in {1,...,\n} { \foreach \j in {1,...,\n} { \pgfmathparse{\i+2} \ifthenelse{\j=\pgfmathresult}{ \draw[fill=red] (\i-1,\j-1)--(\i,\j-1)--(\i,\j)--(\i-1,\j)--(\i-1,\j-1); }{ \ifthenelse{\j=\i}{ \draw[fill=blue] (\i-1,\j-1)--(\i,\j-1)--(\i,\j)--(\i-1,\j)--(\i-1,\j-1); }{ \draw (\i-1,\j-1)--(\i,\j-1)--(\i,\j)--(\i-1,\j)--(\i-1,\j-1); } } } } \end{tikzpicture}