在 Tikz 中向网格添加 3D 元素

在 Tikz 中向网格添加 3D 元素

基于我的问题我创建了下面给出的三角形网格。请注意,我对 Tikz 还很陌生,所以我不知道这是否是最好的方法。

我想在网格中添加 2 到 3 个 3D 元素,基本上将立方体(不同大小)放置在具有等距视图的网格顶部,以便两个都网格和立方体都是等距视图(因此网格显示类似于本示例的一个段. 立方体的顶点很重要不相交与已经创建的网格的顶点或边。

然后我想将立方体的 xy 坐标添加到网格中,创建一个单独的图多于带有立方体的那个,显示域的 2D“平面”视图。

从风格上来说,我喜欢左侧的圆锥体这个例子,但我不确定如何在 Tikz 中使用立方体重新创建类似的东西。

这可能吗?是否有地方提供 MWE?

\documentclass[tikz,border=5]{standalone}
\begin{document}

\begin{tikzpicture}
\foreach \i [evaluate={\ii=int(\i-1);}] in {0,...,2}{
  \foreach \j [evaluate={\jj=int(\j-1);}] in {0,...,2}{
    \coordinate [shift={(\j,\i)}] (n-\i-\j) at (0:1);
\ifnum\i>0
  \draw [help lines] (n-\i-\j) -- (n-\ii-\j);
\fi
\ifnum\j>0
  \draw [help lines] (n-\i-\j) -- (n-\i-\jj);
  \ifnum\i>0
    \pgfmathparse{int(rnd>.5)}
    \ifnum\pgfmathresult=0
      \draw [help lines] (n-\i-\j) -- (n-\ii-\jj);
    \else%
      \draw [help lines] (n-\ii-\j) -- (n-\i-\jj);
    \fi%
  \fi
\fi
}}
\end{tikzpicture}

使用 Tikz 创建的三角形网格

答案1

更新

(t-#)这是产生等距视图的另一种选择;可以轻松标记顶层的方块,因为可以使用、(l-#)(b-#)和访问四个角(r-#)(参见下面的代码):

在此处输入图片描述

代码:

\documentclass[tikz,border=5]{standalone}

\def\Side{0.5cm}
\newcounter{mycube}

\tikzset{  
  cube/.style={
    thin,
    fill opacity=.5,
    fill=cyan!40,
    line join=round
    }
}

\newcommand\Cube[1][1]{%
\begin{tikzpicture}[scale=#1]
  \filldraw[cube] 
    (0,0) -- 
    ++(30:\Side) coordinate (lr) -- 
    ++(0,\Side) -- 
    ++(210:\Side) -- 
    cycle --
    ++(150:\Side) -- 
    ++(0,\Side) -- 
    ++(-30:\Side) -- 
    cycle;
  \filldraw[cube] 
    (0,\Side) -- 
    ++(30:\Side) -- 
    ++(150:\Side) coordinate (up) -- 
    ++(210:\Side) -- 
    cycle;
  \draw[dashed,help lines]
    (up) -- (0,\Side);  
  \draw[dashed,help lines]
    (lr) -- ++(150:\Side) -- ++(210:\Side);  
\end{tikzpicture}%
}

\newcommand\BorderCube[1][1]{%
\stepcounter{mycube}%
\begin{tikzpicture}[scale=#1,remember picture]
  \filldraw[cube] 
    (0,0) coordinate (b-\themycube) -- 
    ++(30:\Side) coordinate (r-\themycube) -- 
    ++(150:\Side) coordinate (t-\themycube) -- 
    ++(210:\Side) coordinate (l-\themycube) -- 
    cycle;
\end{tikzpicture}%
}

\newsavebox\myboxa
\newsavebox\myboxb
\newsavebox\myboxc
\savebox\myboxa{\Cube[0.5]}
\savebox\myboxb{\Cube[0.75]}
\savebox\myboxc{\Cube[0.35]}

\newsavebox\myboxd
\newsavebox\myboxe
\newsavebox\myboxf
\savebox\myboxd{\BorderCube[0.5]}
\savebox\myboxe{\BorderCube[0.75]}
\savebox\myboxf{\BorderCube[0.35]}

\newcommand\MyGrid{
\pgfmathsetseed{9}
\foreach \i [evaluate={\ii=int(\i-1);}] in {0,...,2}{
  \foreach \j [evaluate={\jj=int(\j-1);}] in {0,...,2}{
    \coordinate [shift={(\j,\i)}] (n-\i-\j) at (0:1);
\ifnum\i>0
  \draw [help lines] (n-\i-\j) -- (n-\ii-\j);
\fi
\ifnum\j>0
  \draw [help lines] (n-\i-\j) -- (n-\i-\jj);
  \ifnum\i>0
    \pgfmathparse{int(rnd>.5)}
    \ifnum\pgfmathresult=0
      \draw [help lines] (n-\i-\j) -- (n-\ii-\jj);
    \else%
      \draw [help lines] (n-\ii-\j) -- (n-\i-\jj);
    \fi%
  \fi
\fi
}}
}

\begin{document}

\begin{tikzpicture}[
  remember picture,
  y={(-1cm,0.5cm)},
  x={(1cm,0.5cm)}, 
  z={(0cm,1cm)}
]
\MyGrid
\node[overlay] at (1.65,1.35) {\usebox\myboxa};
\node[overlay] at (1.7,0.5) {\usebox\myboxb};
\node[overlay] at (2.7,1.35) {\usebox\myboxc};
\begin{scope}[shift={(0,50pt)}]
\MyGrid
\node[overlay] at (1.65,1.35) {\usebox\myboxd};
\node[overlay] at (1.7,0.5) {\usebox\myboxe};
\node[overlay] at (2.7,1.35) {\usebox\myboxf};

\node[above=-2pt,font=\tiny] at (t-2) {$a$};
\node[left=-2pt,font=\tiny] at (l-2) {$b$};
\node[below=-2pt,font=\tiny] at (b-2) {$c$};
\node[right=-2pt,font=\tiny] at (r-2) {$d$};
\end{scope}
\end{tikzpicture}

\end{document}

第一个版本

您可以使用tikz-3dplot包裹:

在此处输入图片描述

代码:

\documentclass[tikz,border=5]{standalone}
\usepackage{tikz-3dplot}

\newcommand\Cube[1][0.1]{%
\tdplotsetmaincoords{60}{60}
\begin{tikzpicture}[
  scale=#1,
  tdplot_main_coords,  
  cube/.style={thin,fill opacity=.5,fill=cyan!40,line join=round}
]
  %draw the bottom of the cube
  \filldraw[cube] (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;
  %draw the back-right of the cube
  \filldraw[cube] (0,0,0) -- (0,2,0) -- (0,2,2) -- (0,0,2) -- cycle;
  %draw the back-left of the cube
  %\filldraw[cube] (0,0,0) -- (2,0,0) -- (2,0,2) -- (0,0,2) -- cycle;
  %draw the front-right of the cube
  \filldraw[cube] (2,0,0) -- (2,2,0) -- (2,2,2) -- (2,0,2) -- cycle;
  %draw the front-left of the cube
  \filldraw[cube] (0,2,0) -- (2,2,0) -- (2,2,2) -- (0,2,2) -- cycle;
  %draw the top of the cube
  \filldraw[cube] (0,0,2) -- (0,2,2) -- (2,2,2) -- (2,0,2) -- cycle;
\end{tikzpicture}%
}

\newcommand\BorderCube[1][0.1]{%
\tdplotsetmaincoords{60}{60}
\begin{tikzpicture}[
  scale=#1,
  tdplot_main_coords,  
  cube/.style={thin,fill opacity=.5,fill=cyan!40,line join=round}
]
  %draw the bottom of the cube
  \filldraw[cube] (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;
  %draw/hide the top of the cube
  \filldraw[opacity=0] (0,0,2) -- (0,2,2) -- (2,2,2) -- (2,0,2) -- cycle;
\end{tikzpicture}%
}

\newsavebox\myboxa
\newsavebox\myboxb
\newsavebox\myboxc
\savebox\myboxa{\Cube}
\savebox\myboxb{\Cube[0.15]}
\savebox\myboxc{\Cube[0.075]}

\newsavebox\myboxd
\newsavebox\myboxe
\newsavebox\myboxf
\savebox\myboxd{\BorderCube}
\savebox\myboxe{\BorderCube[0.15]}
\savebox\myboxf{\BorderCube[0.075]}

\newcommand\MyGrid{
\pgfmathsetseed{3}
\foreach \i [evaluate={\ii=int(\i-1);}] in {0,...,2}{
  \foreach \j [evaluate={\jj=int(\j-1);}] in {0,...,2}{
    \coordinate [shift={(\j,\i)}] (n-\i-\j) at (0:1);
\ifnum\i>0
  \draw [help lines] (n-\i-\j) -- (n-\ii-\j);
\fi
\ifnum\j>0
  \draw [help lines] (n-\i-\j) -- (n-\i-\jj);
  \ifnum\i>0
    \pgfmathparse{int(rnd>.5)}
    \ifnum\pgfmathresult=0
      \draw [help lines] (n-\i-\j) -- (n-\ii-\jj);
    \else%
      \draw [help lines] (n-\ii-\j) -- (n-\i-\jj);
    \fi%
  \fi
\fi
}}
}

\begin{document}

\tdplotsetmaincoords{60}{60}
\begin{tikzpicture}[
  tdplot_main_coords,
  cube/.style={very thick,black},
]
\MyGrid
\node[overlay] at (1.15,0.55) {\usebox\myboxa};
\node[overlay] at (2.35,0.28) {\usebox\myboxb};
\node[overlay] at (2.25,1.15) {\usebox\myboxc};
\begin{scope}[shift={(0,50pt)},transform shape]
\MyGrid
%\begin{scope}[shift={(0pt,0pt)}]
\node[overlay] at (1.15,0.55) {\usebox\myboxd};
\node[overlay] at (2.35,0.28) {\usebox\myboxe};
\node[overlay] at (2.25,1.15) {\usebox\myboxf};
%\end{scope}
\end{scope}
\end{tikzpicture}

\end{document}

相关内容