4

I have a cylinder which is slanted. I want the shade to be slanted as well. At the moment the colours are light in the middle of the figure and not slanted.

\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}

enter image description here

  • 2
    there is a key called shading angle – percusse Mar 16 '16 at 11:42
4

As pointed out by @percusse, the code missing is shading angle. I set it to 79 in this example.

\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}

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.