3

I need to draw two cylindrical cups, containing water, as in the following figure. enter image description here

Did perhaps more laborious method, although I have not been able to finish. Can someone help me to complete?

\documentclass{article}
\usepackage[a4paper,bottom=2cm]{geometry} 
\usepackage{tikz}   
\usepackage{color}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw[fill=blue!20]  (-1.5,0)--(-1.5,2)--(1.5,2)--(1.5,0);
\draw[fill=blue!20] (0,0) ellipse (1.5cm and 0.5cm);
\draw (-1.5,0)--(-1.5,4);
\draw (1.5,0)--(1.5,4);
\draw (0,4) ellipse (1.5cm and 0.5cm);
\draw[fill=blue!50] (0,2) ellipse (1.5cm and 0.5cm);
\end{tikzpicture}
\qquad
\begin{tikzpicture}
\draw[fill=blue!50] (1,1.6) ellipse (1.7cm and 0.6cm);
\begin{scope}[rotate=-30]
\draw[fill=blue!20] (0,0) ellipse (1.5cm and 0.5cm);
\draw (-1.5,0)--(-1.5,4);
\draw (1.5,0)--(1.5,4);
\draw (0,4) ellipse (1.5cm and 0.5cm);
\end{scope}
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

Salim Bou
  • 17,021
  • 2
  • 31
  • 76
benedito
  • 4,600
  • 2
    Did you try anything and encounter any issues? At a first look, this should not be that difficult, I would start with the left cup using e.g. \filldraw ellipse and \filldraw rectangle. For the second cup you can rotate parts of the first. – Ondrian Feb 21 '16 at 19:49
  • 2
    To rotate the first part, as @Ondrian suggested, try \begin{scope}[rotate=30] code of first cup \end{scope} – Carina Feb 21 '16 at 19:54
  • We've had something similar to this before, I think user loop space answered it – cmhughes Feb 21 '16 at 22:06
  • Did perhaps more laborious method, although I have not been able to finish. Can someone help me to complete? – benedito Feb 22 '16 at 12:18
  • this must be a duplicate somewhere – percusse Feb 22 '16 at 16:24
  • @percusse, there was one, kind of, but without code... – Alenanno Feb 22 '16 at 16:28
  • @Alenanno I'm pretty sure there is one. Same time around this one even http://tex.stackexchange.com/questions/103307/ but couldn't find it. – percusse Feb 22 '16 at 16:33
  • I meant [this one] http://tex.stackexchange.com/questions/31330/partially-filled-tilted-cylinder-in-tikz), but it gives no code. – Alenanno Feb 22 '16 at 16:36

1 Answers1

8

I don't really enjoy manual calculations, also because if you need more it gets tedious, so I made a newcommand:

\filltank[<optional rotation>]{<position>}{<fill>}
  • <optional rotation> is how much you want to rotate the container. This is optional in the sense that if you don't say anything the rotation is 0.

  • <position> is the position of the container (expressed through coordinates).

  • <fill> is how much you want to fill the tank, expressed in 100 units. So, 100 is full, 0 is empty, 50 is half, and so on.

Output

enter image description here

Code

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}

\usetikzlibrary{calc, intersections, decorations.markings}

\newcommand\filltank[3][0]{%
    \coordinate (O) at (#2);
    \begin{scope}[rotate around={#1:(O)}, shift={(#2)}]
    \pgfmathsetmacro\mylevel{((#1/2)+#3)/100}
    \coordinate (bottom) at (-1.5,0);
    \path[postaction={decoration={%
              markings, mark=at position \mylevel with \coordinate (lvl);
          },decorate}] (-1.5,0)--(-1.5,4);
    \path[name path=level] (lvl) --++ (-#1:4);
    \path[name path=right] (1.5,0)--(1.5,4);
    \path[name intersections={of=level and right,by=b}];
    \fill[opacity=.5, blue!30] (b) -- (lvl) -- (bottom) arc (180:360:1.5cm and .5cm) -- cycle;
    \path let
        \p1 = (lvl),
        \p2 = (b),
        \n1 = {veclen((\x2-\x1),(\y2-\y1))}
        in
        (lvl) -- (b);
    \begin{scope}[rotate={-#1}]
    \fill[blue!50] let
        \p1 = (lvl),
        \p2 = (b),
        \n1 = {veclen((\x2-\x1),(\y2-\y1))}
        in (lvl) arc (180:0:\n1/2 and .5cm) (b) arc (360:180:\n1/2 and .5cm);
    \end{scope}
    \draw[thick] (0,0) ellipse (1.5cm and 0.5cm);
    \draw[thick] (0,4) ellipse (1.5cm and 0.5cm);
    \draw[thick] (-1.5,0)--(-1.5,4);
    \draw[thick] (1.5,0)--(1.5,4);
    \end{scope}
}

\begin{document}
\begin{tikzpicture}
\filltank{0,0}{50}

\filltank[-30]{4,0}{50}
\end{tikzpicture}
\end{document}
Alenanno
  • 37,338