Graphics, Figures & TablestikZ | Shading on Cylinder

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
maxmax
Posts: 29
Joined: Sat Sep 03, 2011 11:19 am

tikZ | Shading on Cylinder

Post by maxmax »

Hi all,

i tried to get a picture like the one attached. Essentially, i need to shade on the surface of a cylinder. I figured out that it could be achieved by placing many small peaces next to each other, but unfortunately it takes alot of calculation time and i guess this is surely one of the worst methods.

Code: Select all

\documentclass[11pt]{article}
\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}
    \newcommand{\placearc}[3]{
      \path[fill=red!#3] (#1,0) arc (-90:90:1 and 2) -- (#1+#2,4) -- (#1+#2,4) arc (90:-90:1 and 2) -- cycle;
    }
    \foreach \posx in {0,0.05,...,2,4,4.05,...,6}{
      \pgfmathsetmacro{\arccolor}{sin(\posx*90)^2*100};
      \placearc{\posx}{0.05}{\arccolor};
    }
  \end{tikzpicture}
\end{document}

Greetings,
Max
Attachments
shading.pdf
(5.79 KiB) Downloaded 333 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

feuersaenger
Posts: 34
Joined: Sun Oct 16, 2011 5:56 pm

tikZ | Shading on Cylinder

Post by feuersaenger »

Hi Max,

I agree that "placing many small pieces next to each other" is the correct approach, but I guess that you should rather glue surface patches together instead of arcs. In other words: it might be feasible to use 2d pieces rather than 1d pieces.

On the other hand, shadings of this category are difficult to accomplish by means of on-board-methods of tikz.

The TikZ library PGFPlots offers interpolated shadings based on surface patch segments. To use it, you only need to find a suitable parameterization of a cylinder - like r(x,y) = (sin(x),cos(x),y).

I used that parameterized cylinder to get some starting point which might be of use for your application. Here is what I got so far:

Code: Select all

\documentclass[a4paper]{article}

\usepackage{pgfplots}

\begin{document}
\thispagestyle{empty}

\begin{tikzpicture}
	\begin{axis}[hide axis]
		\addplot3[surf,shader=interp,domain=0:2*pi,domain y=0:5]
			({sin(deg(x))},{cos(deg(x))},{y});
	\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
	\begin{axis}[hide axis,
		colormap={whitered}{color(0pt)=(white) color(1pt)=(red) color(2pt)=(white) color(3pt)=(red) color(4pt)=(white) }
	]
		\addplot3[surf,shader=interp,domain=0:2*pi,domain y=0:5]
			({sin(deg(x))},{cos(deg(x))},{y});
	\end{axis}
\end{tikzpicture}
\end{document}
I added two (almost identical) pictures - only the colormap differs. It relies on a PGFPlots axis environment which hides its axis. The key point here is a parameterized surface plot with interpolated shading.

It is done by means of the \addplot3 command which samples two coordinates x (defined by domain and samples) and y (defined by domain y and samples y). The following coordinate has three tuples, one for the X coord, one for Y, and one for Z. Each is encapsulated by curly braces in order to avoid scoping problems due to the opening/closing round braces. The shader=interp uses a linear triangle shading in this parameterization where the color is defined by some color mapping. The color map uses one scalar value (the Z coord by default) and maps that into the default color map. The second plot defines a custom color map which has some vague resemblence of your samples graphics.

Best regards

Christian
Attachments
P.png
P.png (65.62 KiB) Viewed 3806 times
Post Reply