答案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}