How to draw stacked cubes of different sizes and colors?

If you create a new command to draw a cube, you can use them as building blocks. Here is an example:

\newcommand{\drawbox}[4]{
    \pgfmathsetmacro \angle {30}
    \pgfmathsetmacro \xd {{2/3*cos(\angle)}}
    \pgfmathsetmacro \yd {{2/3*sin(\angle)}}
    \pgfmathsetmacro \x {{#1-1+(#2-1)*(\xd)}}
    \pgfmathsetmacro \y {{#3-1+(#2-1)*(\yd)}}

    \draw[fill=#4] (\x,\y) -- (\x+1,\y) -- (\x+1,\y+1) -- (\x,\y+1) -- cycle;
    \draw[fill=#4] (\x,\y+1) -- (\x+\xd,\y+1+\yd) -- (\x+1+\xd,\y+1+\yd) -- (\x+1,\y+1) -- cycle;
    \draw[fill=#4] (\x+1,\y+1) -- (\x+1+\xd,\y+1+\yd) -- (\x+1+\xd,\y+\yd) -- (\x+1,\y) -- cycle;
}

This command takes 4 arguments: an x-coordinate, a y-coordinate, a z-coordinate and a fill-colour. ((1,1,1) being the leftmost and downmost block).

Now you can start building your desired structures by stacking cubes:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

\newcommand{\drawbox}[4]{
    \pgfmathsetmacro \angle {30}
    \pgfmathsetmacro \xd {{2/3*cos(\angle)}}
    \pgfmathsetmacro \yd {{2/3*sin(\angle)}}
    \pgfmathsetmacro \x {{#1-1+(#2-1)*(\xd)}}
    \pgfmathsetmacro \y {{#3-1+(#2-1)*(\yd)}}

    \draw[fill=#4] (\x,\y) -- (\x+1,\y) -- (\x+1,\y+1) -- (\x,\y+1) -- cycle;
    \draw[fill=#4] (\x,\y+1) -- (\x+\xd,\y+1+\yd) -- (\x+1+\xd,\y+1+\yd) -- (\x+1,\y+1) -- cycle;
    \draw[fill=#4] (\x+1,\y+1) -- (\x+1+\xd,\y+1+\yd) -- (\x+1+\xd,\y+\yd) -- (\x+1,\y) -- cycle;
}

\begin{document}
\begin{tikzpicture}
    \drawbox{1}{2}{1}{white}
    \drawbox{2}{2}{1}{white}
    \drawbox{1}{1}{1}{white}
    \drawbox{2}{1}{1}{white}
    \drawbox{2}{2}{2}{gray!25}
\end{tikzpicture}

\begin{tikzpicture}
    \drawbox{5}{5}{1}{blue!25}\drawbox{5}{5}{2}{white}\drawbox{5}{5}{3}{blue!25}\drawbox{5}{5}{4}{white}\drawbox{1}{5}{5}{blue!25}\drawbox{2}{5}{5}{white}\drawbox{3}{5}{5}{blue!25}\drawbox{4}{5}{5}{white}\drawbox{5}{5}{5}{blue!25}
    \drawbox{5}{4}{1}{white}\drawbox{5}{4}{2}{white}\drawbox{5}{4}{3}{white}\drawbox{5}{4}{4}{white}\drawbox{1}{4}{5}{white}\drawbox{2}{4}{5}{white}\drawbox{3}{4}{5}{white}\drawbox{4}{4}{5}{white}\drawbox{5}{4}{5}{white}
    \drawbox{5}{3}{1}{blue!25}\drawbox{5}{3}{2}{white}\drawbox{5}{3}{3}{blue!25}\drawbox{5}{3}{4}{white}\drawbox{1}{3}{5}{blue!25}\drawbox{2}{3}{5}{white}\drawbox{3}{3}{5}{blue!25}\drawbox{4}{3}{5}{white}\drawbox{5}{3}{5}{blue!25}
    \drawbox{5}{2}{1}{white}\drawbox{5}{2}{2}{white}\drawbox{5}{2}{3}{white}\drawbox{5}{2}{4}{white}\drawbox{1}{2}{5}{white}\drawbox{2}{2}{5}{white}\drawbox{3}{2}{5}{white}\drawbox{4}{2}{5}{white}\drawbox{5}{2}{5}{white}
    \drawbox{1}{1}{1}{blue!25}\drawbox{2}{1}{1}{white}\drawbox{3}{1}{1}{blue!25}\drawbox{4}{1}{1}{white}\drawbox{1}{1}{2}{white}\drawbox{2}{1}{2}{white}\drawbox{3}{1}{2}{white}\drawbox{4}{1}{2}{white}\drawbox{1}{1}{3}{blue!25}\drawbox{2}{1}{3}{white}\drawbox{3}{1}{3}{blue!25}\drawbox{4}{1}{3}{white}\drawbox{1}{1}{4}{white}\drawbox{2}{1}{4}{white}\drawbox{3}{1}{4}{white}\drawbox{4}{1}{4}{white}
    \drawbox{5}{1}{1}{blue!25}\drawbox{5}{1}{2}{white}\drawbox{5}{1}{3}{blue!25}\drawbox{5}{1}{4}{white}\drawbox{1}{1}{5}{blue!25}\drawbox{2}{1}{5}{white}\drawbox{3}{1}{5}{blue!25}\drawbox{4}{1}{5}{white}\drawbox{5}{1}{5}{blue!25}
\end{tikzpicture}

\begin{tikzpicture}
    \drawbox{1}{2}{1}{white}
    \drawbox{2}{2}{1}{white}
    \drawbox{1}{2}{2}{white}
    \drawbox{2}{2}{2}{gray!25}
    \drawbox{1}{2}{3}{white}
    \drawbox{1}{1}{1}{white}
    \drawbox{2}{1}{1}{white}
    \drawbox{1}{1}{2}{white}
    \drawbox{2}{1}{2}{gray!25}
    \drawbox{1}{1}{3}{white}
\end{tikzpicture}
\end{document}

Which produces output: enter image description here


This is very raw code but it colors

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[on grid]
  \draw[yslant=-0.5,]  (0,0) rectangle +(3,3);
  \draw[yslant=-0.5] (0,0) grid (3,3);
  \foreach \x in {0,2}{
  \foreach \y in {0,2}{
  \fill[yslant=-0.5, blue] (\x,\y) rectangle +(1,1);
  }}
  \draw[yslant=0.5] (3,-3) rectangle +(3,3);
  \draw[yslant=0.5] (3,-3) grid (6,0);
  \foreach \x in {3,5}{
  \foreach \y in {-3,-1}{
  \fill[yslant=0.5, blue] (\x,\y) rectangle +(1,1);
  }}
  \draw[yslant=0.5,xslant=-1,] (6,3) rectangle +(-3,-3);
  \draw[yslant=0.5,xslant=-1] (3,0) grid (6,3);
  \foreach \x in {5,3}{
  \foreach \y in {2,0}{
  \fill[yslant=0.5,,xslant=-1, blue] (\x,\y) rectangle +(1,1);
  }}
\end{tikzpicture}
\end{document}

enter image description here

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[on grid]
  \draw[yslant=-0.5,]  (0,0) rectangle +(3,2);
  \draw[yslant=-0.5] (2,0) grid (3,2);
  \draw[yslant=-0.5] (0,0) grid (2,3);
  \foreach \x in {0,2}{
  \foreach \y in {0}{
  \fill[yslant=-0.5, blue] (\x,\y) rectangle +(1,1);
  }}
  \draw[yslant=0.5] (3,-3) rectangle +(3,2);
  \draw[yslant=0.5] (3,-3) grid (6,-1);
  \draw[yslant=0.5] (2,0) grid (5,1);
  \foreach \x in {3,5}{
  \foreach \y in {-3}{
  \fill[yslant=0.5, blue] (\x,\y) rectangle +(1,1);
  }}
  \draw[yslant=0.5,xslant=-1,] (6,3) rectangle +(-3,-2);
  \draw[yslant=0.5,xslant=-1] (3,1) grid (6,3);
  \draw[yslant=0.5,xslant=-1] (2,-1) grid (5,0);
  \foreach \x in {5,3}{
  \foreach \y in {2}{
  \fill[yslant=0.5,,xslant=-1, blue] (\x,\y) rectangle +(1,1);
  }}
\end{tikzpicture}
\end{document}

enter image description here


Building upon Maarten's answer, and using the \foreach, \pgfmathparse{} and \ifnum\pgfmathresult constructs, the answer could be much shorter.

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\newcommand{\drawbox}[4]{
\draw[fill=#4] ({#1-1+(#2-1)*2/3*cos(30)},{(#2-1)*2/3*sin(30)+#3-1}) -- ({#1-1+(#2-1)*2/3*cos(30)+1},{(#2-1)*2/3*sin(30)+#3-1}) -- ({#1-1+(#2-1)*2/3*cos(30)+1},{(#2-1)*2/3*sin(30)+#3-1+1}) -- ({#1-1+(#2-1)*2/3*cos(30)},{(#2-1)*2/3*sin(30)+#3-1+1}) -- cycle;
\draw[fill=#4] ({#1-1+(#2-1)*2/3*cos(30)},{(#2-1)*2/3*sin(30)+#3-1+1}) -- ({#1-1+#2*2/3*cos(30)},{(#2-1)*2/3*sin(30)+#3-1+1+2/3*sin(30)}) -- ({#1-1+(#2-1)*2/3*cos(30)+1+2/3*cos(30)},{(#2-1)*2/3*sin(30)+#3-1+1+2/3*sin(30)}) -- ({#1-1+(#2-1)*2/3*cos(30)+1},{(#2-1)*2/3*sin(30)+#3-1+1}) -- cycle;
\draw[fill=#4] ({#1-1+(#2-1)*2/3*cos(30)+1},{(#2-1)*2/3*sin(30)+#3-1+1}) -- ({#1-1+(#2-1)*2/3*cos(30)+1+2/3*cos(30)},{(#2-1)*2/3*sin(30)+#3-1+1+2/3*sin(30)}) -- ({#1-1+(#2-1)*2/3*cos(30)+1+2/3*cos(30)},{(#2-1)*2/3*sin(30)+#3-1+2/3*sin(30)}) -- ({#1-1+(#2-1)*2/3*cos(30)+1},{(#2-1)*2/3*sin(30)+#3-1}) -- cycle;}

\begin{document}
\begin{tikzpicture}
  \foreach [count=\i]\x in {1,...,5}
  \foreach [count=\j]\y in {5,...,1}
  \foreach [count=\k]\z in {1,...,5}{  
  \pgfmathparse{isodd(\i*\j*\k)}  
  \ifnum\pgfmathresult>0   
    \drawbox{\x}{\y}{\z}{blue!25};
  \else   
    \drawbox{\x}{\y}{\z}{white};
  \fi}
\end{tikzpicture}
\end{document}

With the required result:

enter image description here

Tags:

Tikz Pgf