4

I want to draw an arrow curve line over another curve line. what is the easiest an accurate way?

\documentclass{article}
    \usepackage{tikz}
    \begin{document}
    \begin{tikzpicture}[ultra thick]
    \coordinate (G) at (2.3,6.1);
    \coordinate (B) at (2.1,1.7);
    \node [fill=green,circle] at (G) {}; 
    \node [fill=blue, circle] at (B) {};
    \draw [violet] (B) to[out=120,in=150] (G);
    \end{tikzpicture}
    \end{document}

the result of abovecode is:

enter image description here Please, any one can help me to draw the following as example enter image description here

5

Taking inspiration from what I learnt from this answer, you could use a decoration to

  1. either draw on top of drawn path
  2. or directly draw all together.

In order to implement the first strategy I defined the draw on top style, but this is less handy to be used, since you need to duplicate code. The second strategy is achieved using one command only (here I called bicolor the new defined style).

Both style have a first argument which should be between 0 and 1 and which indicates the portion of path which is covered by the arrow, the second argument is the arrow colour and the third argument (only of the bicolor style) is the colour of the path under the arrow.

Note: I used a xshift=0.02\totallength to adjust the position of the arrow, if someone knows a more elegant way to achieve the same adjustment, I would be curious to know it.

enter image description here

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{decorations.markings}

\newlength\totallength

\tikzset{
    draw on top/.style 2 args={
        decoration={
            markings,
            mark=at position #1 with {
                \node[draw=none,inner sep=0pt,fill=none,text width=0pt,minimum size=0pt] {\global\setlength\totallength{\pgfdecoratedpathlength}};
                \arrow[#2, xshift=0.02\totallength]{stealth}
            },
        },
        draw=#2,
        dash pattern=on #1\totallength off \totallength-#1\totallength,
        preaction={decorate},
    },
    bicolor/.style n args={3}{
        decoration={
            markings,
            mark=at position #1 with {
                \node[draw=none,inner sep=0pt,fill=none,text width=0pt,minimum size=0pt] {\global\setlength\totallength{\pgfdecoratedpathlength}};
            },
        },
        draw=#3,
        preaction={decorate},
        postaction={
            draw=#2,
            dash pattern=on #1\totallength off \totallength-#1\totallength, 
        },
        postaction={
            decorate, decoration={markings,mark=at position #1 with {\arrow[#2, xshift=0.02\totallength]{stealth}}}
        }
    }
}


\begin{document}
    \begin{tikzpicture}[ultra thick]
        \coordinate (G) at (2.3,6.1);
        \coordinate (B) at (2.1,1.7);
        \node [fill=green,circle] at (G) {}; 
        \node [fill=blue, circle] at (B) {};
        %Strategy 1
        \draw [violet] (B) to[out=120,in=150] (G);
        \draw [draw on top={0.4}{orange}] (B) to[out=120,in=150] (G);
        %Strategy 2 (one command only)
        \draw [bicolor={0.2}{orange}{violet}] (G) to[out=320,in=30] (B);
    \end{tikzpicture}
\end{document}
  • That's a very nice solution! +1. But why do you shift the arrow by an amount that is proportional to the total path length? This will shift the arrow more for longer paths. Wouldn't it be better to shift it by some fixed amount such as half the length of the arrow? (And did you try to use \pgfextra instead of placing an empty node?) – user121799 Apr 6 '18 at 15:28
  • To shift the arrow by an amount proportional to the path length is not a good idea, I admit it. Moreover, xshift=0.02\totallength is a shift along the tangent line to the path in that point and this is potentially catastrophic since it could make the arrow go out of the path in case of abrupt bends. I quickly tried to achieve what you suggest, but I do not figure out how to get the length of the arrow. I also failed to understand how to successfully use \pgfextra in the decoration to avoid an empty node, maybe you can enlighten me more explicitly... ;) – Axel Krypton Apr 8 '18 at 17:50
  • Oh, sorry, I did not fully appreciate what you're doing. I couldn't make use of \pgfextra either, at least not in a way that makes the code shorter. So I think your trick is actually great! – user121799 Apr 8 '18 at 18:40

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.