也许是更费力的方法,尽管我还没能完成。有人能帮我完成吗?
\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}
答案1
我不太喜欢手动计算,而且如果需要更多计算就会变得很乏味,所以我创建了一个新命令:
\filltank[<optional rotation>]{<position>}{<fill>}
<optional rotation>
是您希望容器旋转多少度。这是可选的,如果您不指定任何内容,则旋转度为0
。<position>
是容器的位置(通过坐标表示)。<fill>
是您想要填充油箱的量,以100
单位表示。因此,100 表示满,0 表示空,50 表示一半,依此类推。
输出
代码
\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}