
我正在嘗試繪製相鄰的立方體形狀。如果我不按順序指定座標,則後續形狀的線條將覆蓋前面的線條,如下所示。如何處理這個問題而不用擔心繪圖順序?
\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}