该代码给出了圆柱体,但我希望圆柱表面的一半部分是一种颜色,另一半部分是另一种颜色(一半相对于顶部而不是中间,意味着当我们将顶部圆形横截面切成两半时)。
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (0,0) ellipse (1.25 and 0.5);
\draw (-1.25,0) -- (-1.25,-3.5);
\draw (-1.25,-3.5) arc (180:360:1.25 and 0.5);
\draw [dashed] (-1.25,-3.5) arc (180:360:1.25 and -0.5);
\draw (1.25,-3.5) -- (1.25,0);
\fill [gray,opacity=0.5] (-1.25,0) -- (-1.25,-3.5) arc (180:360:1.25 and 0.5) -- (1.25,0) arc (0:180:1.25 and -0.5);
\end{tikzpicture}
\end{document}
我们如何才能实现它?
答案1
你是指这样的吗?
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (0,0) ellipse (1.25 and 0.5);
\draw (-1.25,0) -- (-1.25,-3.5);
\draw (-1.25,-3.5) arc (180:360:1.25 and 0.5);
\draw [dashed] (-1.25,-3.5) arc (180:360:1.25 and -0.5);
\draw (1.25,-3.5) -- (1.25,0);
%
\fill [left color = blue, right color = red, opacity=0.5] (-1.25,0) -- (-1.25,-3.5) arc (180:360:1.25 and 0.5) -- (1.25,0) arc (0:180:1.25 and -0.5);
\end{tikzpicture}
\end{document}
如果你仍然想要一个急剧的过渡,你可以fill
用以下方法替换路径
\fill [opacity=0.5, color = blue] (-1.25,0) -- (-1.25,-3.5) arc
(180:270:1.25 and 0.5) -- (0,-0.5) node[midway, left, color =
black]{blue} arc(270:180:1.25 and 0.5) ;
\fill [opacity=0.5, color = red] (1.25,0) -- (1.25,-3.5) arc
(0:-90:1.25 and 0.5) -- (0,-0.5) node[midway, right, color =
black]{red} arc(270:360:1.25 and 0.5);