如何正确填充 3d 图形

如何正确填充 3d 图形

我正在尝试填充一个 3D 图形,它是一个半圆柱体。但是,我不知道如何正确填充图形,因为在 2D 平面上路径会自动相交。

当我使用选项填充时,结果显示第一个图形(左)

\draw[red, fill=red!25, xshift=-2.5cm] (0,0) -- (0,0,1) arc (90:270:1) -- (0,-2,0) arc (270:90:1);

我想要的结果在右边,但是效率非常低\foreach

左:填充选项,右:期望结果

答案1

欢迎使用 TeX-SE!fill仅填充 2D 轮廓,因此您需要分两步进行填充。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{60}{50}
\begin{tikzpicture}[tdplot_main_coords,declare function={h=4;r=3;}]
 \draw[fill=blue!40,fill opacity=0.5] plot[variable=\x,domain=\tdplotmainphi:180,smooth] ({r*cos(\x)},{r*sin(\x)},0)
 -- plot[variable=\x,domain=180:\tdplotmainphi,smooth] ({r*cos(\x)},{r*sin(\x)},h);
 \draw[fill=blue!40,fill opacity=0.5] plot[variable=\x,domain=0:\tdplotmainphi,smooth] ({r*cos(\x)},{r*sin(\x)},0)
 -- plot[variable=\x,domain=\tdplotmainphi:0,smooth] ({r*cos(\x)},{r*sin(\x)},h)
 -- cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

或者是pic具有当今强制动画的可旋转版本。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot,tikzlings}
\tikzset{pics/half cylinder/.style={code={\tikzset{3d stuff/.cd,#1}
\ifnum\tdplotmainphi>180
 \path[pic actions,3d stuff/background] 
 plot[variable=\x,domain=\tdplotmainphi-180:0,smooth] ({\pgfkeysvalueof{/tikz/3d stuff/radius}*cos(\x)},{\pgfkeysvalueof{/tikz/3d stuff/radius}*sin(\x)},0)
 -- plot[variable=\x,domain=0:\tdplotmainphi-180,smooth] ({\pgfkeysvalueof{/tikz/3d stuff/radius}*cos(\x)},{\pgfkeysvalueof{/tikz/3d stuff/radius}*sin(\x)},\pgfkeysvalueof{/tikz/3d stuff/height});
 \path[pic actions,3d stuff/foreground] plot[variable=\x,domain=180:\tdplotmainphi-180,smooth] ({\pgfkeysvalueof{/tikz/3d stuff/radius}*cos(\x)},{\pgfkeysvalueof{/tikz/3d stuff/radius}*sin(\x)},0)
 -- plot[variable=\x,domain=\tdplotmainphi-180:180,smooth] ({\pgfkeysvalueof{/tikz/3d stuff/radius}*cos(\x)},{\pgfkeysvalueof{/tikz/3d stuff/radius}*sin(\x)},\pgfkeysvalueof{/tikz/3d stuff/height})
 -- cycle;
\else
 \path[pic actions,3d stuff/background] 
 plot[variable=\x,domain=\tdplotmainphi:180,smooth] ({\pgfkeysvalueof{/tikz/3d stuff/radius}*cos(\x)},{\pgfkeysvalueof{/tikz/3d stuff/radius}*sin(\x)},0)
 -- plot[variable=\x,domain=180:\tdplotmainphi,smooth] ({\pgfkeysvalueof{/tikz/3d stuff/radius}*cos(\x)},{\pgfkeysvalueof{/tikz/3d stuff/radius}*sin(\x)},\pgfkeysvalueof{/tikz/3d stuff/height});
 \path[pic actions,3d stuff/foreground] plot[variable=\x,domain=0:\tdplotmainphi,smooth] ({\pgfkeysvalueof{/tikz/3d stuff/radius}*cos(\x)},{\pgfkeysvalueof{/tikz/3d stuff/radius}*sin(\x)},0)
 -- plot[variable=\x,domain=\tdplotmainphi:0,smooth] ({\pgfkeysvalueof{/tikz/3d stuff/radius}*cos(\x)},{\pgfkeysvalueof{/tikz/3d stuff/radius}*sin(\x)},\pgfkeysvalueof{/tikz/3d stuff/height})
 -- cycle;
\fi 
}},3d stuff/.cd,radius/.initial=1,height/.initial=1,foreground/.style={},
background/.style={}}
\begin{document}
\foreach \X in {0,10,...,360}
{\tdplotsetmaincoords{60}{\X}
\begin{tikzpicture}[tdplot_main_coords]
 \path[tdplot_screen_coords] (-4,-2) rectangle (4,5);
 \pic[draw,fill=blue!40,opacity=0.5]{half
 cylinder={radius=3,height=4,foreground/.style={opacity=0}}};
 \koala[tdplot_screen_coords];
 \pic[draw,fill=blue!40,opacity=0.5]{half
 cylinder={radius=3,height=4,background/.style={opacity=0}}};
\end{tikzpicture}}
\end{document}

在此处输入图片描述

使用 可以获得更自动化的解决方案pgfplots,但您需要了解形状的参数化。(我知道,在圆柱体的情况下,这是一个薄弱的论点。;-)

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[hide axis,axis equal,view/h=125, view/v=-25]

    \addplot3[fill opacity=0.5,
    surf,shader=interp,
    colormap/hot,point meta=x,
     mesh/ordering=y varies,
    samples = 50,samples y=10,
    domain = 0:180,
    y domain = 0:3,
    ] ({cos(x)}, {sin(x)}, {y});

  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容