我正在尝试使用 tikz 绘制一个装有水的圆柱形水箱。以下是我目前所得到的:
\documentclass[a4paper, 12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{polar}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{calc}
\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}
\node[draw, cylinder, rotate=90, shape aspect=4, minimum height=4cm, minimum
width=8cm, fill = blue, fill opacity = 0.2, blue] {};
\node[draw, cylinder, rotate=90, shape aspect=4, minimum height=6cm, minimum
width=8cm, thick] (A) {};
\draw[dashed, thick]
let \p1 = ($ (A.after bottom) - (A.before bottom) $),
\n1 = {0.5*veclen(\x1,\y1)-\pgflinewidth},
\p2 = ($ (A.bottom) - (A.after bottom)!.5!(A.before bottom) $),
\n2 = {veclen(\x2,\y2)-\pgflinewidth}
in
([xshift=-\pgflinewidth] A.before bottom) arc [start angle=0, end
angle=180,
x radius=\n1, y radius=\n2];
\end{tikzpicture}
\caption{The tank.}
\label{fig:tank}
\end{figure}
\end{document}
结果如下:
我想要将蓝色部分向下移动,以便它与黑色部分底部处于同一水平。
对于这个解决方案或使我的代码更高效的任何建议都将非常有用!
答案1
我会使用tikz-3dplot
它,特别是如果你正在考虑添加更多类似 3d 的元素。以下有 4 个参数,一个设置为 的视角100
,一个半径\R
,水的高度\HW
和圆柱体的高度\HC
。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{calc}
\begin{document}
\tdplotsetmaincoords{100}{0}
\begin{tikzpicture}[tdplot_main_coords]
\pgfmathsetmacro{\R}{4}
\pgfmathsetmacro{\HW}{4}
\pgfmathsetmacro{\HC}{6}
% water
\fill[blue!40] plot[variable=\x,domain=0:180,smooth] ({\R*cos(\x)},{\R*sin(\x)},0)
--
plot[variable=\x,domain=180:360,smooth] ({\R*cos(\x)},{\R*sin(\x)},\HW)
-- cycle;
\draw[blue] plot[variable=\x,domain=0:360,smooth,samples=51]
({\R*cos(\x)},{\R*sin(\x)},\HW);
% "invisible" lined
\draw[dashed] plot[variable=\x,domain=180:360,smooth]
({\R*cos(\x)},{\R*sin(\x)},0);
% visible cylinder lines
\draw plot[variable=\x,domain=0:180,smooth]
({\R*cos(\x)},{\R*sin(\x)},0)
--
plot[variable=\x,domain=180:360,smooth]
({\R*cos(\x)},{\R*sin(\x)},\HC) -- cycle;
\draw plot[variable=\x,domain=0:180,smooth]
({\R*cos(\x)},{\R*sin(\x)},\HC);
\end{tikzpicture}
\end{document}
view angle
此动画说明了的含义。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{calc}
\begin{document}
\foreach \X in {0,10,...,350}
{\tdplotsetmaincoords{120+30*sin(\X)}{0}
\begin{tikzpicture}
\pgfmathsetmacro{\R}{4}
\pgfmathsetmacro{\HW}{4}
\pgfmathsetmacro{\HC}{6}
\path[use as bounding box] (-1.1*\R,-0.75*\HC) rectangle (1.1*\R,1.25*\HC);
\begin{scope}[tdplot_main_coords]
% water
\fill[blue!40] plot[variable=\x,domain=0:180,smooth] ({\R*cos(\x)},{\R*sin(\x)},0)
--
plot[variable=\x,domain=180:360,smooth] ({\R*cos(\x)},{\R*sin(\x)},\HW)
-- cycle;
\draw[blue] plot[variable=\x,domain=0:360,smooth,samples=51]
({\R*cos(\x)},{\R*sin(\x)},\HW);
% "invisible" lined
\draw[dashed] plot[variable=\x,domain=180:360,smooth]
({\R*cos(\x)},{\R*sin(\x)},0);
% visible cylinder lines
\draw plot[variable=\x,domain=0:180,smooth]
({\R*cos(\x)},{\R*sin(\x)},0)
--
plot[variable=\x,domain=180:360,smooth]
({\R*cos(\x)},{\R*sin(\x)},\HC) -- cycle;
\draw plot[variable=\x,domain=0:180,smooth]
({\R*cos(\x)},{\R*sin(\x)},\HC);
\end{scope}
\end{tikzpicture}}
\end{document}
答案2
您需要在圆柱体的底部定义锚点......
\documentclass[a4paper, 12pt]{article}
%\usepackage{pgfplots}
%\pgfplotsset{compat=1.15}
%\usepgfplotslibrary{polar}
\usepackage{tikz}
\usetikzlibrary{calc,
positioning,
shapes.geometric}
\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}[
node distance = 0pt,
valj/.style args = {#1/#2}{draw, cylinder, shape aspect=4, shape border rotate=90,
fill=#2, fill opacity=0.2,
minimum height=#1, minimum width=8cm,
outer sep=0pt, anchor=bottom, % <--- added
node contents={}
}
]
\node (A) [valj=60mm/white];
\node (B) [valj=40mm/blue,
above=of A.bottom]; % <--- positioning of cylinder
\draw[dashed]
let \p1 = ($ (A.after bottom) - (A.before bottom) $),
\n1 = {0.5*veclen(\x1,\y1)-\pgflinewidth},
\p2 = ($ (A.bottom) - (A.after bottom)!.5!(A.before bottom) $),
\n2 = {veclen(\x2,\y2)}
in (A.before bottom) arc [start angle=0, end angle=180,
x radius=\n1, y radius=\n2];
\end{tikzpicture}
\caption{The tank.}
\label{fig:tank}
\end{figure}
\end{document}
答案3
我已经找到了解决方案。在包含第一个圆柱体(绘制水的圆柱体)参数的方括号中,我们只需添加xshift = -0.75cm
。由于外圆柱体的高度为 6 厘米,内圆柱体的高度为 4.5 厘米,因此高度差为 1.5 厘米。由于两个圆柱体都位于同一位置的中心,蓝色上方和下方间隙的高度分别为 0.75 厘米,因此我们只需将其向下移动该距离即可。起初我很好奇为什么yshift
没有用,但后来我意识到圆柱体一开始就旋转了 90 度,所以这可能是原因。