梅威瑟:

梅威瑟:

我想用 tikz 画一个这样的金字塔。不幸的是,我不知道如何开始,因为我从来没有在 tikz 中使用过 3d 的东西。

在此处输入图片描述

如果有人能帮助我,我会非常高兴。

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\end{tikzpicture}

\end{document}

答案1

我确信最终会出现一些很好的答案,但无论如何,这是一个相当棘手的方法:

正如@John Kormylo 所评论的,要实现所需图片的细节,需要对金字塔的每个面进行不同的填充和正确的阴影处理,这可能会生成非常冗长的代码。另一方面,阴影可以更轻松地实现,但它们也会使绘图变得不那么“自动化”。

要创建金字塔,必须提供基本信息:\pyramidheight\pyramidwidth\pyramiddepth。结构使用名为ABCD的坐标计算H。前四个定义底部,最后一个定义顶部。要划分层级,必须收集另外两个信息: 的数量\levels\levelsep。之后,我们可以创建辅助坐标来绘制金字塔截头体,如下所示:

\foreach \level[remember=\level as \lastlevel (initially 1)] in {2,...,\levels}{%
    \foreach \i in {A,...,D}{%
          \coordinate (\i\lastlevel t) at ($(\i)!\lastlevel/\levels-\levelsep/2!(H)$);
          \coordinate (\i\level b) at ($(\i)!\lastlevel/\levels+\levelsep/2!(H)$);
}}

这样,每个截头体的坐标为A<level>bA<level>t的坐标也为BCD共同构成了截头体的b底平面和上平面。现在我们为结构节点创建别名,这样它们就符合相同的逻辑:也将成为等等。但会有几个别名:、和。tAA1bHA\levels tB\levels tC\levels tD\levels t

最后,我们可以使用两个 s 顺序地绘制视锥体\foreach,因为我们不是在绘制 3D 实体,而是“连接”它的面,所以line join=round必须传递以切断尖锐的连接。

x可以通过改变向量、y和来改变金字塔的透视图z。在此示例中,我将 改为主要是x为了展示其效果。此外,由于截头体应具有不同的颜色,因此绘图在绘制面时会自动查找样式,因此必须定义样式(其中是从 1 到 的数字),否则会出现“ ”错误。(1cm,0)(3.5mm,-1mm)level <i>\def\levels{<num>}<num>level <i><i><num>I don't know this key

在此处输入图片描述

梅威瑟:

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{calc}
\def\pyramidwidth{0.75cm}
\def\pyramiddepth{0.5cm}
\def\pyramidheight{.35cm}
\def\levels{3}
\def\levelsep{0.05}
\begin{document}
  \sffamily
  \begin{tikzpicture}[line width=1pt, every node/.style={scale=3},
                      level 1/.style={fill=orange, draw=orange!80},
                      level 2/.style={fill=cyan!80, draw=cyan!60},
                      level 3/.style={fill=yellow!70!black, draw=yellow!70!black!80},
                      x={(3.5mm,-1mm)}]
    \coordinate[alias=A1b] (A) at (0,0,0);
    \coordinate[alias=C1b] (C) at (\pyramidwidth,0,\pyramiddepth);
    \coordinate[alias=B1b] (B) at (0,0,\pyramiddepth);
    \coordinate[alias=D1b] (D) at (\pyramidwidth,0,0);
    \coordinate (H) at (\pyramidwidth/2,\pyramidheight,\pyramiddepth/2);
    \foreach \n in {A,B,C,D}{%
      \foreach \level[remember=\level as \lastlevel (initially 1)] in {2,...,\levels}{%
        \coordinate (\n\lastlevel t) at ($(\n)!\lastlevel/\levels-\levelsep/2!(H)$);
        \coordinate (\n\level b) at ($(\n)!\lastlevel/\levels+\levelsep/2!(H)$);}}
    \foreach \n in {A,B,C,D}{\coordinate (\n\levels t) at (H);}
    \foreach \level in {1,...,\levels}{%
        \foreach \i/\j in {D/A, A/B, B/C, C/D}{%
            \draw[level \level, line join=round] (\i\level b) -- (\j\level b) -- (\j\level t) -- (\i\level t) -- cycle;};
            \draw[level \level, line join=round] (A\level t) -- (B\level t) -- (C\level t) -- (D\level t) -- cycle;};
     \begin{scope}[every node/.style={rotate=-16,xslant=.1,above, inner sep=3pt, font=\bfseries}]
       \node[scale=4] at ($(B1b)!.5!(C1b)$) {Daten};
       \node[scale=1.8] at ($(B2b)!.5!(C2b)$) {Informationen};
       \node[scale=1.6] at ($(B3b)!.5!(C3b)$) {Etwas};
     \end{scope}
     %
     \begin{scope}[x=1cm]
       \draw ($(D1b)!.5!(D1t)$) -- +(1,0) node[right] {Text};
       \draw ($(D2b)!.5!(D2t)$) -- +(1,0) node[right] {Text};
       \draw ($(D3b)!.5!(D3t)$) -- +(1,0) node[right] {Text};
     \end{scope}
  \end{tikzpicture}
\end{document}

编辑:

只是为了好玩,这里有一个带有“照明”和阴影的,用经验方法制作的;-)

在此处输入图片描述

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{calc,shadings,shadows}
\def\pyramidwidth{0.75cm}
\def\pyramiddepth{0.5cm}
\def\pyramidheight{.35cm}
\def\levels{3}
\def\levelsep{0.08}
\begin{document}
  \sffamily
  \begin{tikzpicture}[line width=1pt, every node/.style={scale=3},
                      level 1/.style={fill=orange, draw=orange!80},
                      level 2/.style={fill=cyan!80, draw=cyan!60},
                      level 3/.style={fill=yellow!70!black, draw=yellow!70!black!80},
                      x={(3.5mm,-1mm)}]
    \coordinate[alias=A1b] (A) at (0,0,0);
    \coordinate[alias=C1b] (C) at (\pyramidwidth,0,\pyramiddepth);
    \coordinate[alias=B1b] (B) at (0,0,\pyramiddepth);
    \coordinate[alias=D1b] (D) at (\pyramidwidth,0,0);
    \coordinate (H) at (\pyramidwidth/2,\pyramidheight,\pyramiddepth/2);
    \foreach \n in {A,B,C,D}{%
      \foreach \level[remember=\level as \lastlevel (initially 1)] in {2,...,\levels}{%
        \coordinate (\n\lastlevel t) at ($(\n)!\lastlevel/\levels-\levelsep/2!(H)$);
        \coordinate (\n\level b) at ($(\n)!\lastlevel/\levels+\levelsep/2!(H)$);}}
    \foreach \n in {A,B,C,D}{\coordinate (\n\levels t) at (H);}
    %% First level
    \draw[line join=round, draw=orange!80, very thick] (D1b) -- (A1b) -- (A1t) -- (D1t) -- cycle;
    \draw[line join=round, draw=orange!80, very thick] (A1b) -- (B1b) -- (B1t) -- (A1t) -- cycle;
    \shade[line join=round, upper right=orange!10, lower left=orange, upper left=orange, lower right=orange, draw=orange!80, very thick] (B1b) -- (C1b) -- (C1t) -- (B1t) -- cycle;
    \shade[line join=round, upper left=orange!10, lower right=orange!50, upper right=orange, lower left=orange, draw=orange!80, very thick] (C1b) -- (D1b) -- (D1t) -- (C1t) -- cycle;
    \shade[line join=round, lower right=orange!10, upper right=orange, lower left=orange, upper left=orange, draw=orange!80, very thick] (A1t) -- (B1t) -- (C1t) -- (D1t) -- cycle;
%     %% Second level
    \path[drop shadow={shadow scale=0.9, shadow xshift=.5ex, shadow yshift=-4ex, opacity=.5, fill=black!50}] (B2b) -- (C2b) -- (D2b) -- (A2b);
    \draw[line join=round, draw=cyan!80, very thick] (D2b) -- (A2b) -- (A2t) -- (D2t) -- cycle;
    \draw[line join=round, draw=cyan!80, very thick] (A2b) -- (B2b) -- (B2t) -- (A2t) -- cycle;
    \shade[line join=round, upper right=cyan!10, lower left=cyan, upper left=cyan, lower right=cyan, draw=cyan!80, very thick] (B2b) -- (C2b) -- (C2t) -- (B2t) -- cycle;
    \shade[line join=round, upper left=cyan!10, lower right=cyan!50, upper right=cyan, lower left=cyan, draw=cyan!80, very thick] (C2b) -- (D2b) -- (D2t) -- (C2t) -- cycle;
    \shade[line join=round, lower right=cyan!10, upper right=cyan, lower left=cyan, upper left=cyan, draw=cyan!80, very thick] (A2t) -- (B2t) -- (C2t) -- (D2t) -- cycle;
     \begin{scope}[every node/.style={rotate=-16,xslant=.1,above, inner sep=3pt, font=\bfseries}]
       \node[scale=4] at ($(B1b)!.5!(C1b)$) {Daten};
       \node[scale=1.8] at ($(B2b)!.5!(C2b)$) {Informationen};
     \end{scope}
     %
     \begin{scope}[x=1cm]
       \draw ($(D2b)!.5!(D2t)$) -- +(1,0) node[right] {Text};
       \draw ($(D1b)!.5!(D1t)$) -- +(1,0) node[right] {Text};
     \end{scope}
  \end{tikzpicture}
\end{document}

相关内容