使用 tikz 繪製複雜的 3D 繪圖

使用 tikz 繪製複雜的 3D 繪圖

我已經透過 製作了下面的圖片TikZ在此輸入影像描述 為此,我修改了發現的數獨範例這裡如下:

\documentclass{standalone}

\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows}

\begin{document}

\fontsize{5}{6}

\begin{tikzpicture}[>=stealth,every node/.style={minimum size=1cm},on grid]

\begin{scope}[every node/.append style={yslant=-0.5},yslant=-0.5]
  \shade[right color=gray!10, left color=black!50] (0,0) rectangle +(3,3);
\end{scope}

\begin{scope}[every node/.append style={yslant=0.5},yslant=0.5]
  \shade[right color=gray!70,left color=gray!10] (3,-3) rectangle +(3,3);
\end{scope}

\begin{scope}[every node/.append style={yslant=0.5,xslant=-1},yslant=0.5,xslant=-1]
  \shade[bottom color=gray!10, top color=black!50] (6,3) rectangle +(-3,-3);
\end{scope}

\begin{scope}[every node/.append style={yslant=-0.5},yslant=-0.5]
  \foreach \i in {.5,1.5,...,2.5}
  \foreach \j in {.5,1.5,...,2.5}
  {
  \node at (\i,\j) {$+$};
  }

  \foreach \i in {0,3}
  \foreach \j in {.5,1.5,...,2.5}
  {
  \node at (\i,\j) {$+$};
  }

  \foreach \i in {.5,1.5,...,2.5}
  \foreach \j in {0,3}
  {
  \node at (\i,\j) {$+$};
  }

  \foreach \i in {0,1,...,3}
  \foreach \j in {.5,1.5,...,2.5}
  {
  \node at (\i,\j) {$+$};
  }

  \draw (0,0) grid (3,3);
\end{scope}

\begin{scope}[every node/.append style={yslant=0.5},yslant=0.5]  
  \foreach \i in {3.5,4.5,...,5.5}
  \foreach \j in {-2.5,-1.5,...,-.5}
  {
  \node at (\i,\j) {$+$};
  }

  \foreach \i in {3,6}
  \foreach \j in {-2.5,-1.5,...,-.5}
  {
  \node at (\i,\j) {$+$};
  }

  \foreach \i in {3.5,4.5,...,5.5}
  \foreach \j in {-3,0}
  {
  \node at (\i,\j) {$+$};
  }

  \draw (3,-3) grid (6,0);
\end{scope}

\begin{scope}[every node/.append style={yslant=0.5,xslant=-1},yslant=0.5,xslant=-1]  
  \foreach \i in {3.5,4.5,...,5.5}
  \foreach \j in {0.5,1.5,...,2.5}
  {
  \node at (\i,\j) {$+$};
  }

  \foreach \i in {3,6}
  \foreach \j in {0.5,1.5,...,2.5}
  {
  \node at (\i,\j) {$+$};
  }

  \foreach \i in {3.5,4.5,...,5.5}
  \foreach \j in {0,3}
  {
  \node at (\i,\j) {$+$};
  }

  \draw (3,0) grid (6,3);
\end{scope}

\begin{scope}[every node/.append style={yslant=-0.5},yslant=-0.5]
  \foreach \i in {.5,1.5,...,2.5}
  \foreach \j in {.5,1.5,...,2.5}
  {
  \draw[<-] (\i,\j) -- +(-.2,-.2);
  }

  \foreach \i in {0,3}
  \foreach \j in {.5,1.5,...,2.5}
  {
  \draw[red,<-] (\i,\j) -- +(-.2,-.2);
  }

  \foreach \i in {.5,1.5,...,2.5}
  \foreach \j in {0,3}
  {
  \draw[red,<-] (\i,\j) -- +(-.2,-.2);
  }

  \foreach \i in {0,1,...,3}
  \foreach \j in {.5,1.5,...,2.5}
  {
  \draw[red,->] (\i,\j) -- +(.2,0);
  }

  \foreach \i in {.5,1.5,...,2.5}
  \foreach \j in {0,1,...,3}
  {
  \draw[red,->] (\i,\j) -- +(0,.2);
  }  
\end{scope}

\begin{scope}[every node/.append style={yslant=0.5},yslant=0.5]  
  \foreach \i in {3.5,4.5,...,5.5}
  \foreach \j in {-2.5,-1.5,...,-.5}
  {
  \draw[->] (\i,\j) -- +(.2,-.2);
  }

  \foreach \i in {3,6}
  \foreach \j in {-2.5,-1.5,...,-.5}
  {
  \draw[red,->] (\i,\j) -- +(.2,-.2);
  }

  \foreach \i in {3.5,4.5,...,5.5}
  \foreach \j in {-3,0}
  {
  \draw[red,->] (\i,\j) -- +(.2,-.2);
  }

  \foreach \i in {3,4,...,6}
  \foreach \j in {-2.5,-1.5,...,-.5}
  {
  \draw[red,->] (\i,\j) -- +(.2,0);
  }

  \foreach \i in {3.5,4.5,...,5.5}
  \foreach \j in {-3,-2,...,0}
  {
  \draw[red,->] (\i,\j) -- +(0,.2);
  }  
\end{scope}

\begin{scope}[every node/.append style={yslant=0.5,xslant=-1},yslant=0.5,xslant=-1]  
  \foreach \i in {3.5,4.5,...,5.5}
  \foreach \j in {0.5,1.5,...,2.5}
  {
  \draw[->] (\i,\j) -- +(.2,.2);
  }

  \foreach \i in {3,6}
  \foreach \j in {0.5,1.5,...,2.5}
  {
  \draw[red,->] (\i,\j) -- +(.2,.2);
  }

  \foreach \i in {3.5,4.5,...,5.5}
  \foreach \j in {0,3}
  {
  \draw[red,->] (\i,\j) -- +(.2,.2);
  }

  \foreach \i in {3,4,...,6}
  \foreach \j in {0.5,1.5,...,2.5}
  {
  \draw[red,->] (\i,\j) -- +(.2,0);
  }

  \foreach \i in {3.5,4.5,...,5.5}
  \foreach \j in {0,1,...,3}
  {
  \draw[red,->] (\i,\j) -- +(0,-.2);
  }
\end{scope}
\end{tikzpicture}

\end{document}

我必須使用scope具有相同yslant和的多個環境slant才能使各層正確地相互重疊。

儘管如此,我對這張照片不太滿意,因為我無法輕易改變視野。有沒有更有效的方法在 TikZ 中(或更一般地說,在 LaTeX 中)繪製這樣的圖片?我想使用 TikZ (而不是用外部程式(例如 MATLAB)產生圖片),因為我應該在這樣的圖片中插入圖例、標籤和其他文字內容。此外,我懷疑pgfplots是答案,但我不確定。

編輯:我認為完全理解的人才能給出完整的答案這個答案作者:@Tom Bombadil。

答案1

到目前為止,我已經能夠使用 pgfplots 繪製這樣的東西:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
   \begin{axis}[
       xmin=0,
       xmax=1,
       ymin=0,
       ymax=1,
       zmin=0,
       zmax=1,
       axis equal,
       ticks=none,
       hide axis,
     ]
%lower face, drawn first as it will be hidden
     \addplot3[black,/pgfplots/quiver,
       quiver/u=0,
       quiver/v=0,
       quiver/w=0.1,
       -{stealth[]},
       samples=3,
       domain=0.16666:0.8333,
     ]{0};
     \addplot3[red,/pgfplots/quiver,
       quiver/u=0.1,
       quiver/v=0,
       quiver/w=0,
       -stealth,
       samples=4,
       domain y=0.16666:0.8333,
       samples y=3,
       domain=0:1,
     ]{0};
     \addplot3[red,/pgfplots/quiver,
       quiver/u=0,
       quiver/v=-0.1,
       quiver/w=0,
       stealth-,
       samples=3,
       domain=0.16666:0.8333,
       samples y=4,
       domain y=0:1,
     ]{0};
%hidden right face, drawn first as it will be hidden
     \addplot3[black,/pgfplots/quiver,
       quiver/u=0,
       quiver/v=0,
       quiver/w=0.1,
       -{stealth[]},
       samples=3,
       domain=0.16666:0.8333,
     ](x,1,y);
     \addplot3[red,/pgfplots/quiver,
       quiver/u=0.1,
       quiver/v=0,
       quiver/w=0,
       -stealth,
       samples=4,
       domain y=0.16666:0.8333,
       samples y=3,
       domain=0:1,
     ](x,1,y);
     \addplot3[red,/pgfplots/quiver,
       quiver/u=0,
       quiver/v=0,
       quiver/w=0.1,
       -stealth,
       samples=3,
       domain=0.16666:0.8333,
       samples y=4,
       domain y=0:1,
     ](x,1,y);
%hidden left face, drawn first as it will be hidden
     \addplot3[black,/pgfplots/quiver,
       quiver/u=0,
       quiver/v=0,
       quiver/w=0.1,
       -{stealth[]},
       samples=3,
       domain=0.16666:0.8333,
     ](0,x,y);
     \addplot3[red,/pgfplots/quiver,
       quiver/u=0,
       quiver/v=-0.1,
       quiver/w=0,
       stealth-,
       samples=4,
       domain y=0.16666:0.8333,
       samples y=3,
       domain=0:1,
     ](0,x,y);
     \addplot3[red,/pgfplots/quiver,
       quiver/u=0,
       quiver/v=0,
       quiver/w=0.1,
       -stealth,
       samples=3,
       domain=0.16666:0.8333,
       samples y=4,
       domain y=0:1,
     ](0,x,y);
      % faces
     \addplot3[surf,
       color=black!40,
       faceted color=black!80,
     domain=0:1,
   domain y=0:1,
   samples=4,
   samples y=4,
 ]
     (x,0,y);
     \addplot3[surf,
       color=black!40,
       faceted color=black!80,
     domain=0:1,
   domain y=0:1,
   samples=4,
   samples y=4,
 ]
     {1};
     \addplot3[surf,
       color=black!40,
       faceted color=black!80,
     domain=0:1,
   domain y=0:1,
   samples=4,
   samples y=4,
 ]
     (1,x,y);
     %upper face
     \addplot3[black,/pgfplots/quiver,
       quiver/u=0,
       quiver/v=0,
       quiver/w=0.1,
       -{stealth[]},
       samples=3,
       domain=0.16666:0.8333,
     ]{1};
    \addplot3[mark=x,
      only marks,
       samples=3,
       domain=0.16666:0.8333,
     ]{1};
     \addplot3[red,/pgfplots/quiver,
       quiver/u=0.1,
       quiver/v=0,
       quiver/w=0,
       -stealth,
       samples=4,
       domain y=0.16666:0.8333,
       samples y=3,
       domain=0:1,
     ]{1};
     \addplot3[red,/pgfplots/quiver,
       quiver/u=0,
       quiver/v=0.1,
       quiver/w=0,
       -stealth,
       samples=3,
       domain=0.16666:0.8333,
       samples y=4,
       domain y=0:1,
     ]{1};
     %left face
    \addplot3[black,/pgfplots/quiver,
       quiver/u=0,
       quiver/v=-0.1,
       quiver/w=0,
       {stealth[]}-,
       samples=3,
       domain=0.16666:0.8333,
     ](x,0,y);
    \addplot3[mark=x,
      only marks,
       samples=3,
       domain=0.16666:0.8333,
     ](x,0,y);
     \addplot3[red,/pgfplots/quiver,
       quiver/u=0.1,
       quiver/v=0,
       quiver/w=0,
       -stealth,
       samples=4,
       domain y=0.16666:0.8333,
       samples y=3,
       domain=0:1,
     ](x,0,y); 
     \addplot3[red,/pgfplots/quiver,
       quiver/u=0,
       quiver/v=0,
       quiver/w=0.1,
       -stealth,
       samples=3,
       domain=0.16666:0.8333,
       samples y=4,
       domain y=0:1,
     ](x,0,y);
   %right face
    \addplot3[black,/pgfplots/quiver,
       quiver/u=0.1,
       quiver/v=0,
       quiver/w=0,
       -{stealth[]},
       samples=3,
       domain=0.16666:0.8333,
     ](1,x,y);
    \addplot3[mark=x,
      only marks,
       samples=3,
       domain=0.16666:0.8333,
     ](1,x,y);
     \addplot3[red,/pgfplots/quiver,
       quiver/u=0,
       quiver/v=0.1,
       quiver/w=0,
       -stealth,
       samples=4,
       domain y=0.16666:0.8333,
       samples y=3,
       domain=0:1,
     ](1,x,y); 
     \addplot3[red,/pgfplots/quiver,
       quiver/u=0,
       quiver/v=0,
       quiver/w=0.1,
       -stealth,
       samples=3,
       domain=0.16666:0.8333,
       samples y=4,
       domain y=0:1,
     ](1,x,y);
 \end{axis}
\end{tikzpicture}
\end{document}

這就導致了下圖:

在此輸入影像描述

仍然存在一些差異(標記未對齊,沒有陰影),但這是可以管理的。更複雜的是優化程式碼,一團糟^^

而且我不知道如何管理臉的順序,以便它們以良好的順序隱藏。

相關內容