9

I can (I learned here!) to draw, but I can't change the colors or make several distinct cells.

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

\begin{tikzpicture}[on grid]
  \shade[yslant=-0.5,right color=gray!10, left color=black!50]
    (0,0) rectangle +(3,3);
  \draw[yslant=-0.5] (0,0) grid (3,3);
  \shade[yslant=0.5,right color=gray!70,left color=gray!10]
    (3,-3) rectangle +(3,3);
  \draw[yslant=0.5] (3,-3) grid (6,0);
  \shade[yslant=0.5,xslant=-1,bottom color=gray!10,
    top color=black!80] (6,3) rectangle +(-3,-3);
  \draw[yslant=0.5,xslant=-1] (3,0) grid (6,3);
\end{tikzpicture}
\end{document}

enter image description here

  • Welcome to TeX.SX! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). – Jesse Jan 25 '15 at 13:41
  • Jesse, I'm sorry, I don't understand your tip – benedito Jan 25 '15 at 14:22
  • That is information for putting your code in a code sample mode with gray background, just like I showed above. :-). Your previous OP was of text mode. – Jesse Jan 25 '15 at 14:38
6

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

7

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

4

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

2

I struggled to get three different colours for each "stack" so decided to post the solution, working off of AboAmmar and Maarten. Note, this produces a grid 24x24x3

\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}
\foreach \a in {1,...,24}{
\drawbox{24}{3}{\a}{blue!40}
\drawbox{\a}{3}{24}{blue!40}
}

\foreach \d in {1,...,24}{
\drawbox{24}{2}{\d}{green!40}
\drawbox{\d}{2}{24}{green!40}
}

\foreach \f in {1,...,24}{
\foreach \g in {1,...,24}{
\drawbox{\g}{1}{\f}{red!40}
}
}

\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.