11

I want to use named path to calculate the intersection of 2 line. but below example code doesn't work!

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\tikzset{
    dot/.style={circle,inner sep=1pt,fill,label={\tiny #1},name=#1},
}

\node[dot=A] (A) at (0,0) {};
\node[dot=B] (B) at (1,0) {};
\node[dot=C] (C) at (0,1) {};
\node[dot=D] (D) at (1,0.5) {};

\path[name path=P1] (A) -- (B);
\path[name path=P2] (C) -- (D);

\coordinate (CS) at (intersection of P1 and P2); 
%\coordinate (CS) at (intersection of A--B and C--D); 

\draw (A) -- (B);
\draw (C) -- (D);
\node[dot=E] (E) at (CS) {};
\draw[dotted] (B)--(E)--(D);
\end{tikzpicture}
\end{document}

If I use the comment out line as below:

\coordinate (CS) at (intersection of A--B and C--D);

It works fine. Is it possible to use a named path as argument of intersection macro!

Expected output as below: enter image description here

8

With a real intersection you can write (I changed the coordinates of D)

\begin{tikzpicture}
\tikzset{dot/.style={circle,inner sep=1pt,fill,label={\tiny #1},name=#1}}
\node[dot=A] (A) at (0,0) {};
\node[dot=B] (B) at (1,0) {};
\node[dot=C] (C) at (0,1) {};
\node[dot=D] (D) at (1,-0.5) {};

\draw [name path=P1] (A) -- (B);
\draw [name path=P2] (C) -- (D);

\path [name intersections={of=P1 and P2,by=E}];
\node[dot=E]  at (E) {};
\draw[dotted] (B)--(E)--(D);
\end{tikzpicture}

else

\begin{tikzpicture}
\tikzset{
    dot/.style={circle,inner sep=1pt,fill,label={\tiny #1},name=#1}}
\node[dot=A] (A) at (0,0) {};
\node[dot=B] (B) at (1,0) {};
\node[dot=C] (C) at (0,1) {};
\node[dot=D] (D) at (1,0.5) {};

\draw [name path=A--B] (A) -- (B);
\draw [name path=C--D] (C) -- (D);

\coordinate (E) at (intersection of A--B and C--D);
\node[dot=E]  at (E) {};
\draw[dotted] (B)--(E)--(D);
\end{tikzpicture}

enter image description here

  • Most of the case, user didn't know where is the intersection. so we wish the second solution better. That means in my case, we can not use path to simplify our code! :-) – lucky1928 Mar 15 '16 at 20:00
  • @lucky1928 I agree with you but sometime we must adapt ... – Alain Matthes Mar 15 '16 at 21:57
  • Could you edit to make clear that only the first code is supported by current TikZ and only the second by version 2? This is causing some confusion: tex.stackexchange.com/questions/384673/…. (I'm assuming this explanation is correct, based on the comments. Please accept my apologies if I'm mistaken.) – cfr Aug 3 '17 at 21:36
6

There must be a real intersection between path P1 and path P2. You could use something like

\path[name path=P1,overlay] (A) -- (B)--([turn]0:5cm);
\path[name path=P2,overlay] (C) -- (D)--([turn]0:5cm);

to enlarge the paths without enlarging the bounding box of the picture.

Then you can use

\path [name intersections={of=P1 and P2,by={CS}}];

to define a coordinate CS at the intersection of the enlarged paths P1 and P2.

enter image description here

Code:

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\tikzset{
    dot/.style={circle,inner sep=1pt,fill,label={\tiny #1},name=#1},
}

\node[dot=A] (A) at (0,0) {};
\node[dot=B] (B) at (1,0) {};
\node[dot=C] (C) at (0,1) {};
\node[dot=D] (D) at (1,0.5) {};

\path[name path=P1,overlay] (A) -- (B)--([turn]0:5cm);
\path[name path=P2,overlay] (C) -- (D)--([turn]0:5cm);

\path [name intersections={of=P1 and P2,by={CS}}];

\draw (A) -- (B);
\draw (C) -- (D);
\node[dot=E] (E) at (CS) {};
\draw[dotted] (B)--(E)--(D);
\end{tikzpicture}
\end{document}
  • I had never seen turn before. Nice trick! Thank you! – Ignasi Mar 15 '16 at 17:55
  • Yes very fine remark ! in 13.4.2 Rotational Relative Coordinates – Alain Matthes Mar 15 '16 at 18:11
  • @AlainMatthes Cool, but if I remove the turn, the result is totally wrong. why? I didn't find such section in my pgfmanual, is it a latest version change? – lucky1928 Mar 15 '16 at 19:54
4

Notation intersection of A--B and C--D is old and doesn't appear in TikZ 3.0 documentation.

New intersections library is documented in section "13.3.2 Intersections of Arbitrary Paths" where is explained how to use named paths.

In your particular case the problem is that paths P1 and P2 doesn't intersect because they are too short. You can make them longer with calc library help and syntax explained in "13.15.3 The Syntax of Partway Modifiers" which defines points on line between two coordinates but not necessarily in between.

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections,calc}

\begin{document}
\begin{tikzpicture}
\tikzset{
    dot/.style={circle,inner sep=1pt,fill,label={\tiny #1},name=#1},
}

\node[dot=A] (A) at (0,0) {};
\node[dot=B] (B) at (1,0) {};
\node[dot=C] (C) at (0,1) {};
\node[dot=D] (D) at (1,0.5) {};

\path[name path=P1] (A) -- ($(A)!3!(B)$);
\path[name path=P2] (C) -- ($(C)!3!(D)$);

\path [name intersections={of=P1 and P2, by={E}}];
\draw (A) -- (B);
\draw (C) -- (D);
\node[dot=E] at (E) {};
\draw[dotted] (B)--(E)--(D);
\end{tikzpicture}
\end{document}

enter image description here

  • In the pgfmanual pgf 3.01 page 61 you have \path [name intersections={of=A--B and C--C’,by=F}]; but with \draw [name path=A--B] (A) -- (B); before. – Alain Matthes Mar 15 '16 at 18:08
  • @AlainMatthes Example on page 61 doesn't use syntax intersection of A--B and C--D (no equal sign, no name intersections) which was valid in earlier TikZ versions (see comments in tex.stackexchange.com/q/290502/1952) – Ignasi Mar 15 '16 at 19:37

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.