我正在尝试得到这样的数字
我到目前为止的代码是
\begin{tikzpicture}[thick, scale = 2, line join = round, line cap = round]
%\draw[step = 1cm, gray, very thin] (0,-4) grid (8,8);
\draw (4.3,0) arc (0:90:1cm);
\draw (4.7,0) arc (180:90:1cm);
\draw (2,1) -- (3.3,1);
\draw (5.7,1) -- (7,1);
% thin pipe
\draw (4.3,3) -- (4.3,-2);
\draw (4.7,3) -- (4.7,-2);
% glass
\draw (2,1.5) -- (2,-3);
\draw (7,1.5) -- (7,-3);
\draw (2,-3) -- (7,-3);
\foreach \x in {-2.9,-2.7,...,0.6}
{\draw[gray!30, dash pattern = on 4pt off 4pt] ([xshift = 4pt] + 2cm, \x cm) -- ++([xshift = -4pt]5.0cm, 0);}
\foreach \x in {-3.0,-2.8,...,0.5}
{\draw[gray!30, dash pattern = on 4pt off 4pt] ([xshift = 4pt] + 1.9cm, \x cm) -- ++([xshift = -4pt]5.0cm, 0);}
% depressed mercury in the thin pipe
\draw (4.7,-0.2) arc (0:180:0.2);
\node at (6,-2) {mercury};
\end{tikzpicture}
如何填充圆角周围。
答案1
正如评论中所建议的,关键在于\clip
图像。为此,您应该在一个命令中创建要剪辑的区域,而不是多个命令。使用cycle
,您可以返回到命令启动的位置(此处:(2,1)
)。
\draw[clip] (2,1) -- (3.3,1) arc (90:0:1cm) -- (4.3,-0.2) arc (180:0:0.2) --
(4.7,0) arc (180:90:1cm) -- (7,1) -- (7,-3) -- (2,-3) -- cycle;
这将绘制路径,并剪切跟随该区域的所有内容。因此,只需将两个\foreach
循环放在这之后即可。
如下例所示,您应该将这些命令放在 内\begin{scope} ... \end{scope}
,这样您以后就可以创建未剪切的路径。否则,细管将始终位于填充物下方。
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[thick]
% Glass
\draw (2,1.5) -- (2,-3) -- (7,-3) -- (7,1.5);
% Fill
\begin{scope}
\clip (2,1) -- (3.3,1) arc (90:0:1cm) -- (4.3,-0.2) arc (180:0:0.2) -- (4.7,0) arc (180:90:1cm) -- (7,1) -- (7,-3) -- (2,-3) -- cycle;
\foreach \x in {-2.9,-2.7,...,2}
{\draw[gray!30, dash pattern = on 4pt off 4pt] ([xshift = 4pt] + 2cm, \x cm) -- ++([xshift = -4pt]5.0cm, 0);}
\foreach \x in {-3.0,-2.8,...,2}
{\draw[gray!30, dash pattern = on 4pt off 4pt] ([xshift = 4pt] + 1.9cm, \x cm) -- ++([xshift = -4pt]5.0cm, 0);}
\draw (2,1) -- (3.3,1) arc (90:0:1cm) -- (4.3,-0.2) arc (180:0:0.2) -- (4.7,0) arc (180:90:1cm) -- (7,1) -- (7,-3) -- (2,-3) -- cycle;
\end{scope}
% Thin pipe
\draw (4.3,3) -- (4.3,-2);
\draw (4.7,3) -- (4.7,-2);
\end{tikzpicture}
\end{document}
答案2
正如我在之前的评论中所建议的那样,您可以将此代码与“圆角”选项一起使用(您可以调整半径的值)作为圆弧的替代(您只需将填充分成三个部分):
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[thick]
% Fill
\begin{scope}
\clip {[rounded corners=6](2,-3) -- (2,1) -- (4.3,1) -- (4.3,-0.2)} -- (4.3,-3) -- cycle;
\foreach \x in {-2.9,-2.7,...,2}
{\draw[gray!30, dash pattern = on 4pt off 4pt] ([xshift = 4pt] + 2cm, \x cm) -- ++([xshift = -4pt]5.0cm, 0);}
\foreach \x in {-3.0,-2.8,...,2}
{\draw[gray!30, dash pattern = on 4pt off 4pt] ([xshift = 4pt] + 1.9cm, \x cm) -- ++([xshift = -4pt]5.0cm, 0);}
\draw {[rounded corners=6](2,-2) -- (2,1) -- (4.3,1) -- (4.3,-0.2)} -- (4.3,-2);
\end{scope}
\begin{scope}
\clip {[rounded corners=6](4.3,-3) -- (4.3,-0.2) -- (4.7,-0.2)} -- (4.7,-3) -- cycle;
\foreach \x in {-2.9,-2.7,...,2}
{\draw[gray!30, dash pattern = on 4pt off 4pt] ([xshift = 4pt] + 2cm, \x cm) -- ++([xshift = -4pt]5.0cm, 0);}
\foreach \x in {-3.0,-2.8,...,2}
{\draw[gray!30, dash pattern = on 4pt off 4pt] ([xshift = 4pt] + 1.9cm, \x cm) -- ++([xshift = -4pt]5.0cm, 0);}
\draw {[rounded corners=6, pattern=north east lines](4.3,-2) -- (4.3,-0.2) -- (4.7,-0.2)} -- (4.7,-2);
\end{scope}
\begin{scope}
\clip {[rounded corners=6](4.7,-0.2) -- (4.7,1) -- (7,1)} -- (7,-3) -- (4.7,-3) -- cycle;
\foreach \x in {-2.9,-2.7,...,2}
{\draw[gray!30, dash pattern = on 4pt off 4pt] ([xshift = 4pt] + 2cm, \x cm) -- ++([xshift = -4pt]5.0cm, 0);}
\foreach \x in {-3.0,-2.8,...,2}
{\draw[gray!30, dash pattern = on 4pt off 4pt] ([xshift = 4pt] + 1.9cm, \x cm) -- ++([xshift = -4pt]5.0cm, 0);}
\draw {[rounded corners=6](4.7,-2) -- (4.7,-0.2) -- (4.7,1) -- (7,1)} -- (7,-2);
\end{scope}
% Glass
\draw (2,1.5) -- (2,-3) -- (7,-3) -- (7,1.5);
% Thin pipe
\draw (4.3,3) -- (4.3,-2);
\draw (4.7,3) -- (4.7,-2);
\end{tikzpicture}
\end{document}
它会给你这样的图片: