5

Is it possible to define a new command in TikZ which will use the same arguments as the \path [line] (node1) -- (node2); command, but which draws instead a 3D tube?

5
  • Yes, it is possible. But what are you expecting, if the path crosses itself many times? Calculations how to hide parts of the tube? Dec 4, 2013 at 17:54
  • Do you mean To-Path? Dec 5, 2013 at 2:59
  • @PrzemysławScherwentke , I can live without crossing for the while, and then , I can try to work on your suggestions to have a more flexible command Dec 5, 2013 at 6:39
  • @JohnKormylo , see My update Dec 5, 2013 at 6:39
  • Have you looked at the code of packages for drawing knots and Celtic designs? If you do want to deal with crossings, that would be a good place to look.
    – cfr
    Dec 16, 2015 at 17:36

1 Answer 1

9

Here's a verdion that draws many lines. You specify the maximum width, the number of lines to draw, the inner and outer color and the path definition. Furthermore it is possible to supply further options.

The width of the lines increses in such a way that it gives the illusion of looking on a tube from the top. a very small number of lines (e.g. 3) will look quite bad, while a ver high number (e.g. 25) does not add to much. If you specify a path, intersections will look merged. If you want to achieve a look of paths running "over" and "under" each other, you'll have to break up the path.

Code

\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}

enter image description here


Edit 1: If you want a stronger 3D effect, you can draw small ellipses on the "tubes":

Code

\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}

Output

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.