tikz 中的倾斜填充/阴影

tikz 中的倾斜填充/阴影

我有一个倾斜的圆柱体。我希望阴影也倾斜。目前,图形中间的颜色很浅,没有倾斜。

\documentclass{standalone}
\usepackage{tikz}
\newcommand{\sampleScrew}[5]{%
\pgfmathsetmacro{\cylinderradius}{#1}
\pgfmathsetmacro{\cylinderheight}{#2}
\pgfmathsetmacro{\aspectratio}{#3}
\pgfmathsetmacro{\opacitycolor}{#4}
\pgfmathsetmacro{\dx}{#5}
% Cylinder fill:
\fill[  left color=gray!70,
             right color=gray!70,
            middle color=gray!40,
            opacity=\opacitycolor] (\cylinderradius,0) -- (\cylinderradius+\dx,\cylinderheight) arc (360:180:\cylinderradius*1cm and \aspectratio*1cm) -- (-\cylinderradius,0) arc (180:0:\cylinderradius*1cm and \aspectratio*1cm);
% Bottom fill:
\fill[   top color=gray!95,
            middle color=gray!70,
            bottom color=gray!40,
            opacity=\opacitycolor] (0,0) circle (\cylinderradius*1cm and \aspectratio*1cm);
% Top fill:
\fill[   top color=gray!70,
            middle color=gray!40,
            bottom color=gray!10,,
            opacity=\opacitycolor] (0+\dx,\cylinderheight) circle (\cylinderradius*1cm and \aspectratio*1cm);   
% Cylinder lines:
\draw (-\cylinderradius+\dx,\cylinderheight) -- (-\cylinderradius,0) arc (180:360:\cylinderradius*1cm and \aspectratio*1cm)
        -- (\cylinderradius+\dx,\cylinderheight) ++ (-\cylinderradius,0) circle (\cylinderradius*1cm and \aspectratio*1cm);
% Dashed line in the back:
\draw[densely dashed] (-\cylinderradius,0) arc (180:0:\cylinderradius*1cm and \aspectratio*1cm);
}
\begin{document}
\begin{tikzpicture}
\sampleScrew{1.25}{3.5}{0.5}{1}{0.5}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

正如@percusse 指出的那样,缺少的代码是shading angle。在这个例子中,我将其设置为 79。

\documentclass{standalone}
\usepackage{tikz}
\newcommand{\sampleScrew}[5]{%
\pgfmathsetmacro{\cylinderradius}{#1}
\pgfmathsetmacro{\cylinderheight}{#2}
\pgfmathsetmacro{\aspectratio}{#3}
\pgfmathsetmacro{\opacitycolor}{#4}
\pgfmathsetmacro{\dx}{#5}
% Cylinder fill:
\fill[  left color=gray!70,
             right color=gray!70,
            middle color=gray!40,
            shading angle=79,
            opacity=\opacitycolor] (\cylinderradius,0) -- (\cylinderradius+\dx,\cylinderheight) arc (360:180:\cylinderradius*1cm and \aspectratio*1cm) -- (-\cylinderradius,0) arc (180:0:\cylinderradius*1cm and \aspectratio*1cm);
% Bottom fill:
\fill[   top color=gray!95,
            middle color=gray!70,
            bottom color=gray!40,
            opacity=\opacitycolor] (0,0) circle (\cylinderradius*1cm and \aspectratio*1cm);
% Top fill:
\fill[   top color=gray!70,
            middle color=gray!40,
            bottom color=gray!10,,
            opacity=\opacitycolor] (0+\dx,\cylinderheight) circle (\cylinderradius*1cm and \aspectratio*1cm);   
% Cylinder lines:
\draw (-\cylinderradius+\dx,\cylinderheight) -- (-\cylinderradius,0) arc (180:360:\cylinderradius*1cm and \aspectratio*1cm)
        -- (\cylinderradius+\dx,\cylinderheight) ++ (-\cylinderradius,0) circle (\cylinderradius*1cm and \aspectratio*1cm);
% Dashed line in the back:
\draw[densely dashed] (-\cylinderradius,0) arc (180:0:\cylinderradius*1cm and \aspectratio*1cm);
}
\begin{document}
\begin{tikzpicture}
\sampleScrew{1.25}{3.5}{0.5}{1}{0.5}
\end{tikzpicture}
\end{document}

相关内容