6

I am trying to draw something like this cylinder: cylinder with water flowing out through a hole. Here is my attempt:

    \documentclass{article}
    \usepackage{graphicx}
    \usepackage{tikz}
    \usetikzlibrary{snakes,shapes,decorations.text}

    \begin{document}

    \begin{center}
    \begin{tikzpicture}
   %\draw (-4,-4) grid (4,4);

   \draw (0,0) node[cylinder,draw=black,thick,aspect=1.4,
    minimum height=3cm,minimum width=2.5cm,
    shape border rotate=90,
    cylinder uses custom fill,
    cylinder body fill=blue!30,
    cylinder end  fill=blue!10, opacity=.4,anchor=north]
   (A) {};

    \draw [dashed] (1.23,-2.87) arc[x radius=1.24, y radius=1/3, start angle=0, end angle=180];

    \draw [dashed] (0,-2.8) ellipse[x radius=0.3, y radius=1/12, start angle=0, end angle=180];
    \end{tikzpicture}
    \end{center}

    \end{document}

which producesthis. The arc drawn along the bottom doesn't exactly match the top of the cylinder, and neither does the dashed ellipse in the middle. How can I match this better? How would I be able to add the effect of fluid flowing out of the hole?

enter image description here

2
  • By specifying the height and width, aspect is used instead to modify the default short radius. So in this case the y radius should be 1.4(.125cm) where .125cm was the default used in all my test runs. – John Kormylo Jul 15 '15 at 16:45
  • 1
    Very refreshing to see someone who has actually tried to draw the thing! Well done, nice question, and we should refer other users to this :) – cmhughes Jul 15 '15 at 19:20
9

Instead of using the predefined cylinder shape, I 'd just use arcs and an army of Mark Wibrow's droplets for the water:

enter image description here

The code:

\documentclass[tikz,border=4pt]{standalone}

\pgfdeclareradialshading[droplet color]{droplet}{\pgfqpoint{-10bp}{-10bp}}{%
 color(0bp)=(droplet color!50!white);
 color(9bp)=(droplet color!75!white);
 color(18bp)=(droplet color!85!black);
 color(25bp)=(droplet color);
 color(50bp)=(droplet color!50!white)}

\colorlet{droplet color}{blue!50!cyan!80}
\tikzset{%
  raindrop/.pic={
    code={\tikzset{scale=1/10}
 \shade [shading=droplet]
 (0,0)  .. controls ++(0,-1) and ++(0,1) .. (1,-2)
 arc (360:180:1)
 .. controls ++(0,1) and ++(0,-1) .. (0,0) -- cycle;
  }}}

\begin{document}

\begin{tikzpicture}[line join=round]
\begin{scope}[yshift=-6pt]
\foreach \Coord in {{(2,0)},{(2.3,-0.25)},{(2.2,-0.3)},{(2.2,-0.4)}%,
  ,{(1.8,0)},{(1.7,-0.25)},{(2,-0.3)},{(1.8,-0.4)}%
  ,{(1.7,-0.2)},{(1.9,-0.5)},{(2,-0.4)},{(1.9,-0.3)}%
  ,{(2.3,-0.2)},{(2.1,-0.5)},{(2,-0.3)},{(2.1,-0.3)}%
  ,{(2.3,-0.1)},{(2.1,-0.2)},{(2,-0.15)},{(2.1,-0.05)}%
  ,{(1.7,-0.1)},{(1.9,-0.25)},{(2,-0.1)},{(1.9,-0.15)}%
  } 
  \path \Coord pic {raindrop};
\end{scope}
\begin{scope}[yshift=4pt]
\foreach \Coord in {{(2,0)},{(2.3,-0.25)},{(2.2,-0.3)},{(2.2,-0.4)}%,
  ,{(1.8,0)},{(1.7,-0.25)},{(2,-0.3)},{(1.8,-0.4)}%
  ,{(1.7,-0.2)},{(1.9,-0.5)},{(2,-0.4)},{(1.9,-0.3)}%
  ,{(2.3,-0.2)},{(2.1,-0.5)},{(2,-0.3)},{(2.1,-0.3)}%
  ,{(2.3,-0.1)},{(2.1,-0.2)},{(2,-0.15)},{(2.1,-0.05)}%
  ,{(1.7,-0.1)},{(1.9,-0.25)},{(2,-0.1)},{(1.9,-0.15)}%
  ,{(2.2,0)},{(1.7,-0.1)},{(2,0)},{(1.9,-0.1)}%
  } 
  \path \Coord pic {raindrop};
\end{scope}
\filldraw[draw=black,ultra thick,fill=droplet color!20]
 (0,3) arc[x radius=2, y radius=0.4, start angle=180, end angle=0]
 (4,3) arc[x radius=2, y radius=0.4, start angle=0, end angle=-180];
\filldraw[ultra thick,fill=droplet color!70,opacity=1,fill opacity=0.7]
  (0,3) -- 
  (0,0) 
  arc[x radius=2, y radius=0.4, start angle=-180, end angle=0] --
   (4,3)
  arc[x radius=2, y radius=0.4, start angle=0, end angle=-180];
\draw[dashed] 
  (0,0) arc[x radius=2, y radius=0.4, start angle=180, end angle=0]
  (1.5,0) arc[x radius=0.5, y radius=0.12, start angle=180, end angle=0]
  (1.5,0) arc[x radius=0.5, y radius=0.12, start angle=-180, end angle=0];
\end{tikzpicture}

\end{document}
2
  • Sorry that my own drawing was very unclear, but I am trying to draw a cylinder. I like the way your tikZ image looks and I will play with this code. Thank you. – julesjanker Jul 15 '15 at 15:42
  • 1
    @julesjanker You're welcome. No problem. I added a version with a cylinder. – Gonzalo Medina Jul 15 '15 at 15:46
2

When you specify the width and height, the default value for the (in this case) y radius appears to be a constant 0.125cm. This is then scaled by the aspect parameter.

cylinder aspect

    \documentclass[border=.1in]{standalone}
    \usepackage{graphicx}
    \usepackage{tikz}
    \usetikzlibrary{snakes,shapes,decorations.text}

    \begin{document}

    \begin{tikzpicture}
   %\draw (-4,-4) grid (4,4);

   \draw (0,0) node[cylinder,draw=black,thick,aspect=1.4,
    minimum height=3cm,minimum width=2.5cm,
    shape border rotate=90,
    cylinder uses custom fill,
    cylinder body fill=blue!30,
    cylinder end  fill=blue!10, opacity=.4,anchor=north]
   (A) {};

    \draw [dashed] (1.25,-2.825) arc[x radius=1.25, y radius=.175, start angle=0, end angle=180];

    \draw [dashed] (0,-2.825) ellipse[x radius=0.3, y radius=.042, start angle=0, end angle=180];

    %demo
    \draw (4,0) node[cylinder,draw=black,thick,aspect=1,
    minimum height=3cm,minimum width=2.5cm,
    shape border rotate=90,
    cylinder uses custom fill,
    cylinder body fill=blue!30,
    cylinder end  fill=blue!10, opacity=.4,anchor=north]
   {};
  \draw (4,0) node[cylinder,draw=black,thick,
    minimum height=1cm,minimum width=1cm,
    shape border rotate=90,
    cylinder uses custom fill,
    cylinder body fill=blue!30,
    cylinder end  fill=blue!10, opacity=.4,anchor=north]
   {};
  \draw (4,0) node[draw=red,
    minimum height=3cm,minimum width=2.5cm,
    shape border rotate=90,anchor=north]
   {aspect demo};
  \draw[green] (4,0) -- (4,-.25) (2.75,-.125) -- (5.25,-.125);

    \end{tikzpicture}
    \end{document}

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.