我正在尝试绘制相邻的立方体形状。如果我不按顺序指定坐标,则后续形状的线条将覆盖前一个形状的线条,如下所示。我该如何处理此问题而不必担心绘制顺序?
\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}