8

I have written the following code:

\begin{tikzpicture}
\draw[->] (-1,0) to node [auto]{$f$} (1,0);

\draw (-5,-1) node(0)[label=below:{$v_0$}, circle,fill=black,scale=0.3]{};
\draw (-2,-1) node(1)[label=below:{$v_1$}, circle,fill=black,scale=0.3]{};
\draw (-3.5,1) node(2)[label=above:{$v_2$}, circle,fill=black,scale=0.3]{};

\path (0) edge node {} (1);
\path (1) edge node {} (2);

\draw [gray!50, step=0.5cm] (1.9999,-2) grid (8,2);

\draw (2,-1) -- (8,-1);
\draw (3,-2) -- (3,2);

\draw (3,-1) node(f0)[label=below:{$f(v_0)$}, circle,fill=black,scale=0.3]{};
\draw (7.5,-1) node(f1)[label=below:{$f(v_1)$}, circle,fill=black,scale=0.3]{};
\draw (4.5,1.5) node(f2)[label=above:{$f(v_2)$}, circle,fill=black,scale=0.3]{};

\path[very thick] (f0) edge node {} (f1);
\draw[very thick] (f2) to [bend angle = 36, bend left] (f1);

\draw[very thick, dashed] (4.5,1.5) to (3,-1);
\foreach \x in {1,...,17}
\draw[thick, dashed] (4.5,1.5) to [bend angle = \x*2, bend left] (3+0.25*\x,-1);
\end{tikzpicture}

which when plotted comes out like so:

Image of plotted code

However my problem is that as $\x \to 17$, the curvature of the drawn dotted paths should approach that of the solid black line, however as you can see in the picture it quite clearly doesn't.

Does anyone know how I can fix this problem?

Thanks!

  • 1
    Welcome. If you select your code and press ctrl+k you can format it. – Sigur May 21 '15 at 17:55
  • 1
    An even better minimal example is this: \begin{tikzpicture} \foreach \x in {1,...,18} \draw[thick] (4.5,1.5) to [bend angle = 2*\x, bend left] (3+0.25*\x,-1); \draw[thick, red] (4.5,1.5) to [bend angle = 36, bend left](7.5, -1); \end{tikzpicture} --- the last line of the foreach seems exactly the same as the red line, but it is drawn very differently. – Rmano May 21 '15 at 19:27
5

Does this do the trick?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{intersections}
\begin{document}

\begin{tikzpicture}[aeang/.style={red,#1}]
\draw[->] (-1,0) to node [auto]{$f$} (1,0);

\draw (-5,-1)  node(0)[label=below:{$v_0$}, circle,fill=black,scale=0.3]{};
\draw (-2,-1)  node(1)[label=below:{$v_1$}, circle,fill=black,scale=0.3]{};
\draw (-3.5,1) node(2)[label=above:{$v_2$}, circle,fill=black,scale=0.3]{};

\path (0) edge node {} (1);
\path (1) edge node {} (2);

\draw [gray!50, step=0.5cm] (1.9999,-2) grid (8,2);

\draw (2,-1) -- (8,-1);
\draw (3,-2) -- (3,2);

\draw (3,-1)    node(f0)[label=below:{$f(v_0)$}, circle,fill=black,scale=0.3]{};
\draw (7.5,-1)  node(f1)[label=below:{$f(v_1)$}, circle,fill=black,scale=0.3]{};
\draw (4.5,1.5) node(f2)[label=above:{$f(v_2)$}, circle,fill=black,scale=0.3]{};

\path[very thick] (f0) edge node {} (f1);
\draw[very thick] (f2) to [bend angle = 36, bend left] (f1);

\draw[very thick, dashed] (f2) to (f0);
\foreach \x in {2.5,5,...,100}
{
  \pgfmathparse{36*\x/100}\edef\aeangle{\pgfmathresult}
  \pgfmathparse{1-\x/100} \edef\aedestination{\pgfmathresult}
  \draw[thin,red, dashed]
      let \p0=(f0),
          \p1=(f1) in
     (f2) to [bend angle = \aeangle, bend left] ($(f1)-({(\x1-\x0)*\aedestination},0)$);
}

\end{tikzpicture}

\end{document}

enter image description here

  • This works, but... why the OP one doesn't? If you do the loop to 18, the last draw should be exactly the same as the draw[very thick] ... outside it, but still the shape is different... puzzled. – Rmano May 21 '15 at 19:21
  • The \foreach loop in the OP's MWE doesn't really map the points in the correect way. Examine what the angles and destination point are for each iteration and you should see that the original path is never duplicated for any value of \x. – A.Ellett May 21 '15 at 19:43
  • Yes --- the problem is that bend angle=\x *2 does not do the multiplication --- but another thing. See my answer... – Rmano May 21 '15 at 19:52
  • @Rmano It's not a problem with expansion. TikZ is evaluating \x*2. But \x*2 is the wrong value: at \x=1 it's 1 but we're terminating the curve at (3.25,-1). For \x=18, the angle is 36, but we're terminating the curve at (7.5,-1). – A.Ellett May 21 '15 at 20:01
  • Hmmm... I'm a bit thick, I can't see it. With \x=18 the expansion should be exactly the same he has in the very thick line, see the green/blue/red line in my answer... – Rmano May 21 '15 at 20:06
7

Found it. Try this:

\begin{tikzpicture}
\foreach \x in {1,...,18}
\draw[thick] (4.5,1.5) to [bend angle = 2*\x, bend left] (3+0.5*\x,-1)
    node [below]{\tiny\x};

\draw[thick, red]  (4.5,1.5) to [bend angle = 36, bend left](3+0.5*18, -1) 
    node [below right]{\tiny E};
\end{tikzpicture}

you have:

enter image description here

But with changing the red line with:

\draw[thick, red]  (4.5,1.5) to [bend angle = 2*18, bend left](3+0.5*18, -1) 
     node [below right]{\tiny E};

you have:

enter image description here

So the problem is that the multiplication in the bend angle parameter is not executed --- the command is not math-aware. A bit of experiments led to this:

\begin{tikzpicture}
\draw[thick, blue]  (4.5,1.5) to [bend angle = 18, bend left](3+0.5*18, -1);
\draw[thick, red]  (4.5,1.5) to [bend angle = 18*2, bend left](3+0.5*18, -1);
\draw[thick, green]  (4.5,1.5) to [bend angle = 36, bend left](3+0.5*18, -1);
\end{tikzpicture}

which renders as (I do not know why, I supposed at least two of them would coincide):

enter image description here

The minimal solution for your code is to substitute the foreach with:

\foreach \x in {2,4,...,34}
\draw[thick, dashed] (4.5,1.5) to [bend angle = \x, bend left] (3+0.125*\x,-1);

which will give you this:

enter image description here

  • Great answer - thank you so much for the explaination!! – Stephen Nand-Lal May 21 '15 at 20:31
  • 3
    OK, I've had a chance to look at what you've done. This is very curious. Seems like some kind of a bug. Your example contrasting the three curves shows that TikZ is doing something with 18*2 (I tried 2*18) but TikZ is not treating it as 36. Very curious. – A.Ellett May 21 '15 at 21:06
  • 2
    @A.Ellett It's just not parsing after *. Try \documentclass{article} \usepackage{tikz}\begin{document}\tikzset{bend angle=2*18,bend left} \end{document} . Not every key is math-aware. Especially the old parts of TikZ. See tikzlibrarytopaths.code.tex line 30 and you'll see the old counter based computation. – percusse May 22 '15 at 0:54
  • 1
    @A.Ellett True but \tikz@to@out stays as 18*2 and then passed around like that and at some point it is parsed in a computation. \tikz@to@in however is once and for all defined and stays like that at the expansion. That's why the end points agree but the initial angles don't match. – percusse May 22 '15 at 1:20
  • 1
    This "bug" probably should be reported as "feature request". All numerical keys should be "math-aware", IMO. – Kpym Dec 1 '15 at 9:45
4

You can implement a slightly better control over your bend angle such that the homotopy is between zero and 36 degrees relative. Then you can use it for the in,out keys. The only change in the code is

\foreach \x[evaluate={\xc={(36/17)*(\x-1)};}] in {1,...,17}{
  \draw[thick, dashed] (4.5,1.5) to [out=\xc,in=180-\xc,relative] (3+0.25*\x,-1);
}

Note that this is the same as writing [bend angle=\xc,bend left]. Just put there so that it is visible what the bending is doing.

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.