我正在尝试画一个漂浮在水中的桶。我尝试使用pgfonlayer
这样一种方式:当水线与桶相交时,水线将被移除。
另外,我浏览了手册中有关图案的一小部分,但找不到适合水的图案。
这是我想重新创建的图像:
这是我的代码和图像:
\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}
答案1
我重新绘制了圆柱体并用 填充它,white
这样在background
图层上绘制的任何内容都不可见。此外,通过操纵dash pattern
并使用\foreach
一些 循环xshift
,可以实现所需的效果。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{patterns}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,foreground}
\begin{document}
\begin{tikzpicture}
\begin{pgfonlayer}{foreground}
% draw a barrel
\draw[fill=white] (-1.5cm, 0) -- (-1.5cm, -3cm) coordinate (C) arc[x radius = 1.5cm, y radius = 0.5cm,
start angle = 180, end angle = 360] coordinate (D) -- (1.5cm, 0) --(-1.5cm, 0) -- cycle;
% \draw[red] (A) -- (D);
% \draw[red] (B) -- (C);
\draw[fill=white] (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 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[dash pattern=on 20pt off 10pt] (-2.5cm, -1.5cm) -- ++(6cm, 0);
\foreach \x in {-1.7,-1.9,...,-4.1}{
\draw[gray!30,dash pattern=on 4pt off 4pt] ([xshift=4pt]-2.5cm, \x cm) -- ++([xshift=-4pt]6cm, 0);
}
\foreach \x in {-1.6,-1.8,...,-4}{
\draw[gray!30,dash pattern=on 4pt off 4pt] (-2.5cm, \x cm) -- ++(6cm, 0);
}
\end{pgfonlayer}
\end{tikzpicture}
\end{document}