是否可以在蒂克兹它将使用与命令相同的参数\path [line] (node1) -- (node2);
,但是绘制的是 3D 管?
答案1
这是一个绘制多条线的版本。您可以指定最大宽度、要绘制的线数、内外部颜色以及路径定义。此外,还可以提供更多选项。
线条的宽度以某种方式增加,给人一种从顶部看管子的错觉。线条数量非常少(例如 3 条)看起来会很糟糕,而数量非常多(例如 25 条)则不会增加太多。如果您指定了一条路径,交叉点看起来会合并。如果您想实现路径“在上方”和“下方”相互延伸的外观,您必须拆分路径。
代码
\documentclass[tikz, border=2mm]{standalone}
\begin{document}
\newcommand{\Tube}[6][]%
% [further options], width, iterations, inner color, outer color, path definition
{ \colorlet{InColor}{#4}
\colorlet{OutColor}{#5}
\foreach \I in {1,...,#3}
{ \pgfmathsetlengthmacro{\h}{(\I-1)/#3*#2}
\pgfmathsetlengthmacro{\r}{sqrt(pow(#2,2)-pow(\h,2))}
\pgfmathsetmacro{\c}{(\I-0.5)/#3*100}
\draw[InColor!\c!OutColor, line width=\r, #1] #6;
}
}
\begin{tikzpicture}
\Tube{2mm}{10}{gray!25}{black!75}
{(0,0) to[out=70,in=200] (5,0)}
\Tube[dashed]{2mm}{10}{yellow!25}{orange!75}
{(0,1) to[out=70,in=200] (5,1)}
\Tube{2mm}{3}{gray!25}{black!75}
{(0,2) to[out=70,in=200] (5,2)}
\Tube{2mm}{25}{gray!25}{black!75}
{(0,3) to[out=70,in=200] (5,3)}
\Tube{2mm}{25}{white}{black}
{(0,4) to[out=0,in=0] (2.5,6) to[out=180,in=180] (5,4)}
\Tube{2mm}{25}{orange!25}{red}
{(0,5.5) to[out=0,in=0] (2.5,7.5)}
\Tube{2mm}{25}{orange!25}{red}
{(2.5,7.5) to[out=180,in=180] (5,5.5)}
\end{tikzpicture}
\end{document}
编辑1:如果想要更强的 3D 效果,可以在“管子”上画上小椭圆:
代码
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{decorations.markings}
\usepackage{xifthen}
\begin{document}
\newcommand{\Tube}[9][]%
% [further options], width, iterations, inner color, outer color, path definition, decorate yn, decorate length, decorate offset
{ \colorlet{InColor}{#4}
\colorlet{OutColor}{#5}
\foreach \I in {1,...,#3}
{ \pgfmathsetlengthmacro{\h}{(\I-1)/(#3-0.7)*#2}
\pgfmathsetlengthmacro{\r}{sqrt(pow(#2,2)-pow(\h,2))}
\pgfmathsetmacro{\c}{(\I-0.5)/#3*100}
\draw[InColor!\c!OutColor, line width=\r, #1] #6;
}
\ifthenelse{\equal{#7}{y}}
{ \path[decoration={markings, mark=between positions #9 and 1 step #8 with {\draw[OutColor] (0,#2/2-\pgflinewidth/2) arc (90:-90:#2/4 and #2/2-\pgflinewidth/2);}}, decorate] #6;
}
{}
}
\begin{tikzpicture}
\Tube{3mm}{10}{gray!25}{black!75}
{(0,0) to[out=70,in=200] (5,0)}
{y}{5mm}{2mm}
\Tube[dashed]{2mm}{10}{yellow!25}{orange!75}
{(0,1) to[out=70,in=200] (5,1)}
{n}{}{}
\Tube{5mm}{3}{gray!25}{black!75}
{(0,2) to[out=70,in=200] (5,2)}
{y}{0.05}{0.025}
\Tube[densely dashed]{1mm}{25}{gray!25}{black!75}
{(0,3) to[out=70,in=200] (5,3)}
{n}{}{}
\Tube{2mm}{25}{white}{black}
{(0,4) to[out=0,in=0] (2.5,6) to[out=180,in=180] (5,4)}
{y}{5mm}{1.5mm}
\Tube{2mm}{25}{orange!25}{red}
{(0,5.5) to[out=0,in=0] (2.5,7.5)}
{y}{0.03}{0.02}
\Tube{2mm}{25}{orange!25}{red}
{(2.5,7.5) to[out=180,in=180] (5,5.5)}
{y}{0.03}{0.01}
\end{tikzpicture}
\end{document}