I am attempting to draw a barrel floating in water. I am trying to use pgfonlayer
so that when the water line intersects the barrel, the line will be removed.
Also, I looked through small section in the manual on patterns, but I couldn't find a suitable pattern for water.
Here is the image I am trying to re-create:
Here is the code I have and the image:
\documentclass[tikz]{standalone}
\usetikzlibrary{patterns}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,foreground}
\begin{document}
\begin{tikzpicture}
\begin{pgfonlayer}{foreground}
% draw a barrel
\draw (0, 0) ellipse[x radius = 1.5cm, y radius = 0.5cm]
coordinate (B) at (-1.5cm, 0) coordinate (A) at (1.5cm, 0);
\draw[dashed] (-1.5cm, -1cm) arc[x radius = 1.5cm, y radius = 0.5cm,
start angle = 180, end angle = 360];
\draw[dashed] (-1.5cm, -1.1cm) arc[x radius = 1.5cm, y radius = 0.5cm,
start angle = 180, end angle = 360];
\draw[dashed] (-1.5cm, -2cm) arc[x radius = 1.5cm, y radius = 0.5cm,
start angle = 180, end angle = 360];
\draw[dashed] (-1.5cm, -2.1cm) arc[x radius = 1.5cm, y radius = 0.5cm,
start angle = 180, end angle = 360];
\draw (-1.5cm, -3cm) coordinate (C) arc[x radius = 1.5cm, y radius = 0.5cm,
start angle = 180, end angle = 360] coordinate (D);
\draw (A) -- (D);
\draw (B) -- (C);
% draw the direction of the W = mg
\fill[black] (0, -1.85cm) coordinate (W) circle[radius = 0.025cm];
\draw[-stealth] (W) -- ++(0, -0.35cm) node[right, font = \scriptsize] {$mg$};
% draw the barrel dimensions
\begin{scope}[>=stealth]
\draw[|<->|] (-1.5cm, 1cm) -- ++(3cm, 0) node[pos = 0.5,
fill = white, font = \scriptsize, inner sep = 0.05cm] {$d$};
\draw[|<->|] (-2cm, 0cm) -- ++(0, -3.5cm) node[pos = 0.5,
fill = white, font = \scriptsize, inner sep = 0.05cm] {$\ell$};
\end{scope}
\end{pgfonlayer}
% draw the water
\begin{pgfonlayer}{background}
\draw (-2.5cm, -1.5cm) -- ++(7cm, 0);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
Best Answer
I have re-drawn the cylinder and filled it with
white
so that whatever is drawn onbackground
layer, is not visible. Further, by manipulating thedash pattern
and using\foreach
loop with somexshift
, the desired effect can be achieved.