
Ich versuche, nebeneinanderliegende Würfelformen zu zeichnen. Wenn ich die Koordinaten nicht in der richtigen Reihenfolge angebe, überlagern die Linien der nachfolgenden Form die vorherigen, wie unten gezeigt. Wie kann ich dieses Problem lösen, ohne mir Gedanken über die Zeichenreihenfolge machen zu müssen?
\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}