5

I ran into the following problem: Constructing a path using "plot", I fail to find intersections that fall exactly onto the coordinates used to construct that path. In the example below, the 2nd intersection is only found when I move the rectangle so that it intersects the plot path before or after the x=4.

This looks a bit to me like the "plot" path would actually not be a single path but segments that are joined, and that the intersection is not found at the points where the segments are linked.

Is this intended, or might it be a bug?

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes,intersections,scopes,calc,positioning}

\begin{document}
\begin{tikzpicture}[scale=1, transform shape]
    \tikzstyle{dot}=[fill=blue,circle,inner sep=.3ex]
    \draw[name path=b,yellow] (2cm,-.5cm) rectangle (4.0cm,2); %3.9cm works 
        \draw [name path=p, blue, thick]
            plot [smooth] coordinates { (0,0) (1,.1) (2,1) (3,1) (4,0.1) (5,0) };
        \path [name intersections={of=b and p}]
            (intersection-1) node[dot] {}
            (intersection-2) node[dot] {};           
\end{tikzpicture} 
\end{document}
  • It doesn't work even with relative coordinates \draw[name path=b,yellow] (2cm,-.5cm) rectangle ++(2cm,2.5cm); – CarLaTeX Jun 3 '18 at 6:58
  • @CarLaTex: I think this is unrelated, 2+2 still is 4. If you change the rectangle or the point in the plot path so that they become different (e.g. 3.9 and 4 instead of 4 and 4) then things work. – user52366 Jun 3 '18 at 7:23
  • It was only a comment to enforce your statement :) – CarLaTeX Jun 3 '18 at 7:27
3

interestingly! just omit units in coordinates, i.e.: instead of (4.0cm,2) use (4.0,2) or shorter (4,2) and your code will work as expected:

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,intersections,scopes,calc,positioning}

\begin{document}
\begin{tikzpicture}[scale=1, transform shape]
    \tikzstyle{dot}=[fill=blue,circle,inner sep=.3ex]
    \draw[name path=b,yellow] (2cm,-.5cm) rectangle (4,2);
        \draw [name path=p, blue, thick]
            plot [smooth] coordinates { (0,0) (1,.1) (2,1) (3,1) (4,0.1) (5,0) };
        \path [name intersections={of=b and p}]
            (intersection-1) node[dot] {}
            (intersection-2) node[dot] {};
\end{tikzpicture}
\end{document}

enter image description here

  • Interesting. I wonder whether removing the unit changes round-off errors and one of the "4" becomes slightly larger or smaller than the other one, and the intersection then is found. – user52366 Jun 3 '18 at 7:24

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.