
Estou tentando desenhar formas de cubos adjacentes. Se eu não especificar as coordenadas em ordem, as linhas da forma subsequente se sobreporão às anteriores, conforme mostrado abaixo. Como posso lidar com esse problema sem me preocupar com a ordem do sorteio?
\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{50}{60}
\begin{tikzpicture}
[tdplot_main_coords,
cube/.style={very thick,black},
grid/.style={very thin,gray},
axis/.style={->,blue,thick}]
%draw a grid in the x-y plane
\foreach \x in {-1,0,...,4.5}
\foreach \y in {-1,0,...,4.5}
{
\draw[grid] (\x,-1) -- (\x,4.5);
\draw[grid] (-1,\y) -- (4.5,\y);
}
%draw the axes
\draw[axis] (0,0,0) -- (8,0,0) node[anchor=west]{$x$};
\draw[axis] (0,0,0) -- (0,8,0) node[anchor=west]{$y$};
\draw[axis] (0,0,0) -- (0,0,8) node[anchor=west]{$z$};
%draw the edges of the cube
\foreach \x/\y/\z in {0/0/0,0/1/0,0/2/0}
{
\draw [fill=gray!30] (\x,\y,\z)--++(0,0,1)--++(0,1,0)--++(0,0,-1)--cycle; %x
\draw [fill=gray!30] (\x,\y,\z)--++(1,0,0)--++(0,1,0)--++(-1,0,0)--cycle; %z
\draw [fill=gray!30] (\x,\y,\z)--++(1,0,0)--++(0,0,1)--++(-1,0,0)--cycle; %y
\draw [fill=gray!30] (\x,\y+1,\z)--++(1,0,0)--++(0,0,1)--++(-1,0,0)--cycle; %y
\draw [fill=gray!30] (\x+1,\y,\z)--++(0,0,1)--++(0,1,0)--++(0,0,-1)--cycle; %x
\draw [fill=gray!30] (\x,\y,\z+1)--++(1,0,0)--++(0,1,0)--++(-1,0,0)--cycle; %z
}
\end{tikzpicture}
\end{document}