使用 TikZ 的几何形状和颜色

使用 TikZ 的几何形状和颜色

我尝试在 LaTeX 中重现下面的右棱镜:

在此处输入图片描述

我能够创建形状,但顶部似乎有问题?此外,我该如何:

  1. 添加颜色渐变
  2. 用另一种颜色给底部上色,用红色给边缘上色
  3. 添加相应的字母

分数维:

\begin{tikzpicture}
\draw[thick] (1,0.5) -- (3,0) -- (5,0.5);
\draw[thick,dashed] (1,0.5) -- (1.5,1.5) -- (4.5,1.5) -- (5,0.5);
\draw[thick] (1,3.5) -- (3,3) -- (5,3.5) -- (4.5,4.5) -- (1.5,4.5) -- (1,3.5);
\draw[thick] (1,0.5) -- (1,3.5);
\draw[thick] (3,0) -- (3,3);
\draw[thick] (5,0.5) -- (5,3.5);
\draw[thick,dashed] (1.5,4.5) -- (1.5,1.5);
\draw[thick,dashed] (4.5,1.5) -- (4.5,4.5);
\end{tikzpicture}

在此处输入图片描述

答案1

我将使用 3d 库来实现这一点。例如:

\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{3d,calc,perspective}
 
\begin{document}
\begin{tikzpicture}[isometric view,rotate around z=180,line join=round,line cap=round]
% dimensions
\def\r{1}   % radius (circumcircle)
\def\h{1.5} % height
% coordinates
\foreach\i in {1,...,5}
{
  \coordinate (A\i) at (72*\i-27:\r);
  \coordinate (B\i) at ($(A\i)+(0,0,\h)$);
}
% color gradient
\fill[left color=white,right color=brown] (B5) -- (A5) -- (A1) -- (A2) -- (B2) -- (B1) -- cycle;
% visible lines
\draw (B5) -- (A5) -- (A1) -- (A2) -- (B2) node[midway,right,red] {$h$};
\draw (A1) -- (B1);
% not visible lines
\draw[dashed] (A2) -- (A3) -- (A4) -- (A5);
\draw[dashed] (A3) -- (B3);
\draw[dashed] (A4) -- (B4);
% yellow pentagon
\draw[red,fill=yellow,fill opacity=0.5] (B1) -- (B2) -- (B3) -- (B4) -- (B5) -- cycle;
\node[red] at (0,0,\h)  {$\mathcal{B}$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

使用极坐标可能是一个好主意,例如像这样:

\documentclass[border=10pt]{standalone} 
\usepackage{tikz}

\begin{document}    

\begin{tikzpicture}
% fill/draw front part of cylinder
\fill[left color=orange!0, right color=orange!25] 
    (198:{1 and 0.5})
        -- ++(0,-1)
        -- ([shift={(down:1)}]270:{1 and 0.5})
        -- ([shift={(down:1)}]342:{1 and 0.5})
        -- ++(0,1)
        -- (270:{1 and 0.5})
        -- cycle;
\draw[thick, black] 
    (198:{1 and 0.5})
        -- ++(0,-1)
        -- ([shift={(down:1)}]270:{1 and 0.5})
        -- ([shift={(down:1)}]342:{1 and 0.5}) coordinate (a)
        -- ++(0,1) coordinate (b)
    (270:{1 and 0.5})
        -- ++(0,-1);

% draw invisible edges
\draw[thick, densely dashed]
    (54:{1 and 0.5})
        -- ++(0,-1)
        -- ([shift={(down:1)}]342:{1 and 0.5})
    (126:{1 and 0.5})
        -- ++(0,-1)
        -- ([shift={(down:1)}]198:{1 and 0.5})
    ([shift={(down:1)}]54:{1 and 0.5})
        -- ([shift={(down:1)}]126:{1 and 0.5});

% fill/draw top pentagon
\draw[thick, red, fill=yellow, fill opacity=0.5] 
    (270:{1 and 0.5}) coordinate (c)
        -- (342:{1 and 0.5})
        -- (54:{1 and 0.5})
        -- (126:{1 and 0.5})
        -- (198:{1 and 0.5})
        -- cycle;
\coordinate (d) at (90:{1 and 0.5});

% add annotations
\path (a) -- (b) node[midway, right, red] {$h$};
\path (c) -- (d) node[midway, red] {$\mathcal{B}$};

\end{tikzpicture}

\end{document}  

在此处输入图片描述

相关内容