我正在尝试使用 TikZ 的等距透视图为圆柱体的表面着色。TikZ 能做到这一点吗?我所做的是用两个矩形近似表面,看起来不错。但是错误是可见的。当然,我可以随时添加更多矩形。
但是,有没有捷径可以做到这一点,或者是否不值得花费精力去“正确地”做到这一点?
如果能有一种不那么繁琐的方式来填充表面,那么对于未来的项目来说将会很有帮助。
代码
\begin{tikzpicture}[y={(-1cm,0.5cm)},x={(1cm,0.5cm)}, z={(0cm,1cm)}]
%Behind Surface
\draw[dashed] (2, 0, 0) -- (2, -1.5, 0);
%Surface
\fill[color = black!45!white, opacity = .4] (-2, -1.43, .451) -- (-2, -1.228, 0.86) -- (2, -1.228, 0.86) -- (2, -1.43, .451);
\fill[color = black!45!white, opacity = .4] (-2, -1.43, .451) -- (-2, -1.5, 0) -- (2, -1.5, 0) -- (2, -1.43, .451);
%Wedge
\draw (-2,0,0) -- (-2,-1.5,0);
\draw (-2, -1.5, 0) -- (2, -1.5, 0);
\draw[canvas is yz plane at x = 2] (0:-1.5) arc (0:-35:-1.5);
\draw (-2, 0, 0) -- (-2, -1.228, 0.86);
\draw[canvas is yz plane at x = -2] (0:-1.5) arc (0:-35:-1.5);
\draw (-2, -1.228, 0.86) -- (2, -1.228, 0.86);
\draw (2,0,0) -- (2,-1.228,0.86);
\draw (2,0,0) -- (-2,0,0);
\end{tikzpicture}
相关包是tikz
,连同3d
库。
结果
答案1
您可以像在 2d 中一样填充。我保留了您的坐标(尽管它们不是完全等距的,但perspective
库中有一个预定义的isometric view
),但更改了 y 和 z 的角色以简化代码。这样做的原因是圆弧自动位于 xy 平面中。此外,使用圆柱坐标也很有用。
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{3d}
\begin{document}
\begin{tikzpicture}[y={(1cm,-0.5cm)},z={(1cm,0.5cm)}, x={(0cm,1cm)},
declare function={R=1.5;alpha=35;H=2;},
radius=R,line join=round,line cap=round]
% behind surface
\draw[dashed] (0, 0,H) -- (0,R,H);
% filled surface
\draw[fill=black!45!white,fill opacity=0.4]
(0,R,-H) arc[start angle=90,end angle=90-alpha]
-- (xyz cylindrical cs:radius=R,angle=90-alpha,z=H)
arc[start angle=90-alpha,end angle=90] -- cycle;
\draw (0,R,-H) -- (0,0,-H) -- (xyz cylindrical cs:radius=R,angle=90-alpha,z=-H);
\draw (0,0,-H) -- (0,0,H) -- (xyz cylindrical cs:radius=R,angle=90-alpha,z=H);
\end{tikzpicture}
\end{document}
如您所见,绘制和填充合并在一个命令中。我还引入了用于重复参数(例如开角alpha
、半径R
和半高)的“函数” H
。
圆柱坐标在库中定义,并在 pgfmanual 3.1.10 版第 40.1 节中记录。您还可以在 x 方向上为圆柱定义自己的圆柱坐标,并在库3d
中绘制楔形。isometric view
perspective
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{3d,perspective}
\makeatletter
\tikzdeclarecoordinatesystem{yzx cylindrical}
{%
\pgfset{/tikz/cs/.cd,angle=0,radius=0,x=0,#1}%
\pgfmathsetmacro{\tikz@cs@y}{\tikz@cs@xradius*cos(\tikz@cs@angle)}%
\pgfmathsetmacro{\tikz@cs@z}{\tikz@cs@xradius*sin(\tikz@cs@angle)}%
\pgfpointxyz{\tikz@cs@x}{\tikz@cs@y}{\tikz@cs@z}%
}%
\makeatother
\begin{document}
\begin{tikzpicture}[isometric view,
declare function={R=1.5;alpha=35;H=2;},
radius=R,line join=round,line cap=round]
% behind surface
\draw[dashed] (yzx cylindrical cs:radius=0,x=H) coordinate (T) -- (yzx cylindrical cs:radius=R,angle=180,x=H) ;
% filled surface
\draw[fill=black!45!white,fill opacity=0.4,variable=\t,smooth]
plot[domain=180:180-alpha] (yzx cylindrical cs:radius=R,angle=\t,x=-H)
-- plot[domain=180-alpha:180] (yzx cylindrical cs:radius=R,angle=\t,x=H) -- cycle;
% remaining lines
\draw (yzx cylindrical cs:radius=R,angle=180,x=-H) -- (yzx cylindrical cs:radius=0,x=-H) coordinate (B) -- (yzx cylindrical cs:radius=R,angle=180-alpha,x=-H)
(B) -- (T) -- (yzx cylindrical cs:radius=R,angle=180-alpha,x=H);
\end{tikzpicture}
\end{document}