我构造了一个圆柱分解器,灵感来自 绘制两个圆柱体并使用裁剪
不幸的是,我发现圆柱形状的行为相当令人困惑。从“原则上”来看,它们的总高度应该为 10 厘米,但实际上它们的高度都不一样。前两个甚至因为标签而外观不同。这里讨论了这个问题 TikZ 中圆柱形状的奇怪行为 但我很难找到令人满意的答案,因为它并没有真正解释为什么文本会影响绘图的盖子部分或如何修复这种行为。
将 \theasp 替换为非常小的值 0.000001 会形成等高矩形。为什么圆柱体形状会破坏此属性?
\documentclass[tikz,margin=2mm]{standalone}
\usetikzlibrary{shapes.geometric,calc,positioning}
\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}
\pgfmathsetmacro{\cylw}{1.15}
\pgfmathsetmacro{\stepx}{1.5}
\pgfmathsetmacro{\theasp}{.78}
\tikzset{mycyl/.style={cylinder,shape border rotate=90,minimum width=\cylw cm,cylinder uses custom fill, shape aspect=\theasp}}
\node (db) [mycyl,
minimum height=10 cm,draw,
cylinder body fill = magenta!10, cylinder end fill = magenta!40,
anchor = south] at (0,0) {1};
\node (db-text) [mycyl,
minimum height=10 cm,draw,
cylinder body fill = magenta!10, cylinder end fill = magenta!40,
anchor = south] at (\stepx,0) {text};
\node (db-slice1) [mycyl, draw,
minimum height=3 cm,
cylinder body fill = green!10, cylinder end fill = green!40,
anchor = south] at (2*\stepx,0) {.3};
\node (db-slice2) [mycyl, draw,
minimum height=7 cm,
cylinder body fill = red!10, cylinder end fill = red!40,
above=0pt of db-slice1.before top, anchor= after bottom] {.7};
\node (db-slice3) [mycyl, draw,
minimum height=3 cm,
cylinder body fill = green!10, cylinder end fill = green!40,
anchor = south] at (3*\stepx,0) {.3};
\node (db-slice4) [mycyl, draw,
minimum height=3 cm,
cylinder body fill = green!10, cylinder end fill = green!40,
above=0pt of db-slice3.before top, anchor=after bottom] {.3};
\node (db-slice5) [mycyl, draw,
minimum height=3 cm,
cylinder body fill = green!10, cylinder end fill = green!40,
above=0pt of db-slice4.before top, anchor=after bottom] {.3};
\node (db-slice6) [mycyl, draw,
minimum height=1 cm,
cylinder body fill = red!10, cylinder end fill = red!40,
above=0pt of db-slice5.before top, anchor=after bottom] {.1};
\end{tikzpicture}
\end{figure}
\end{document}
答案1
我会直接用 tikz 绘制圆柱体,为此创建一个宏。像这样:
\documentclass[margin=2mm]{standalone}
\usepackage{tikz}
\definecolor{mypink} {HTML}{F899D1}
\definecolor{mygreen}{HTML}{E6FFE6}
\definecolor{salmon} {HTML}{FF9999}
\newcommand{\cylinder}[5] % position (lower base center), radius, height, color, text
{%
\begin{scope}[shift={#1}]
\draw[fill=#4!50!white] (-#2,#3) -- (-#2,0) arc (180:360:#2 cm and 0.5*#2 cm) -- (#2,#3);
\draw[fill=#4] (0,#3) ellipse (#2 cm and 0.5*#2 cm);
\node at (0,0.5*#3-0.5*#2) {#5};
\end{scope}
}
\begin{document}
\begin{tikzpicture}
\cylinder{(0 ,0)}{0.5}{10}{mypink} {1}
\cylinder{(1.5,0)}{0.5}{10}{mypink} {text}
\cylinder{(3 ,0)}{0.5} {3}{mygreen}{.3}
\cylinder{(3 ,3)}{0.5} {7}{salmon} {.7}
\cylinder{(4.5,0)}{0.5} {3}{mygreen}{.3}
\cylinder{(4.5,3)}{0.5} {3}{mygreen}{.3}
\cylinder{(4.5,6)}{0.5} {3}{mygreen}{.3}
\cylinder{(4.5,9)}{0.5} {1}{salmon} {.1}
\end{tikzpicture}
\end{document}