35

what is the easiest way to determine the intersection of two lines? I tried

\usetikzlibrary{intersections}
\begin{tikzpicture}[every node/.style={black,above right}]
\draw[name path=line 1] (0,0) -- (2,2);
\draw[name path=line 2] (2,0) -- (0,2);
\fill[red,name intersections={of=line 1 and line 2}]
    (intersection-1) circle (2pt) node {1}
    (intersection-2) circle (2pt) node {2}
\end{tikzpicture}

enter image description here

  • That doesn't look like it's the result of your code. And that code looks pretty easy to me, what would you like improved? – Loop Space Oct 12 '11 at 21:22
  • 1
    Although, this question is quite old, let me ask you for some clarification: What do you understand under “line”? Your question and all answers only use two straight lines. For these you can use (intersection of c1--c2 and c3--c4). – Qrrbrbirlbel Jun 8 '13 at 4:00
38

The reason the code does not work as provided is that there is only one intersection, and so (intersection-2) does not exist. One way to alleviate this kind of issue is to specify total=\t to contain the total number of intersections and the use a foreach to loop through each intersection:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}[every node/.style={black,above right}]
\draw[name path=line 1] (0,0) -- (2,2);
\draw[name path=line 2] (2,0) -- (0,2);
\fill[red,name intersections={of=line 1 and line 2,total=\t}]
    \foreach \s in {1,...,\t}{(intersection-\s) circle (2pt) node {\footnotesize\s}};
\end{tikzpicture}
\end{document}
33

Reading the PGF Manual helps ;). See page 54ff, I made this from it:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc,intersections,through,backgrounds}

\begin{document}

\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (3,3);
\draw [name path=A--B] (A) -- (B);
\coordinate (C) at (3,0);
\coordinate (D) at (0,1);
\draw [name path=C--D] (C) -- (D);
\path [name intersections={of=A--B and C--D,by=E}];
\node [fill=red,inner sep=1pt,label=-90:$E$] at (E) {};
\end{tikzpicture}

\end{document}

which results in:

enter image description here

12

An alternative, in the form of tkz-euclide.

\documentclass{article}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}
  \tkzDefPoint(0,0){A}  \tkzDefPoint(2,2){B}
  \tkzDefPoint(0,2){C}  \tkzDefPoint(2,0){D}
  \tkzDrawSegments(A,B C,D)
  \tkzInterLL(A,B)(C,D) \tkzGetPoint{E}
  \tkzDrawPoints(E) \tkzLabelPoints[below](E)
\end{tikzpicture}
\end{document}

The first three lines define the points and draw the line segments between them. \tkzInterLL compute the intersection of the lines A--B and C--D, while \tkzGetPoint{E} gives the point a name. Finally the point is drawn and labeled.

You can mix this with "normal" TikZ code if you want to, e.g. (borrowing from Tom Bombadil):

\documentclass{article}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (3,3);
\coordinate (C) at (3,0);
\coordinate (D) at (0,1);
\draw (A) -- (B);
\draw (C) -- (D);
  \tkzInterLL(A,B)(C,D) \tkzGetPoint{E}
\node [fill=red,inner sep=1pt,label=-90:$E$] at (E) {};
\end{tikzpicture}
\end{document}

This only uses tkz-euclide to find and name the intersection.

8

For the intersection of two segments, we don't need the intersections library. We can use directly in tikz points like (intersection of A--B and C--D).

\documentclass[tikz,border=5pt]{standalone}

\begin{document}
  \begin{tikzpicture}
    \draw (0,0) coordinate(A) -- (2,2) coordinate (B);
    \draw (2,0) coordinate(C) -- (0,2) coordinate (D);
    \node[red,scale=3] at (intersection of  A--B and C--D){.};
  \end{tikzpicture}
\end{document}

enter image description here

Note: I don't think that this syntax is in the manual (of v3.0) ?

Edit: It looks like that this syntax is deprecated now :(

  • 1
    this syntax is not described in tikz & pgf manual, v 3.0.1a. – Zarko Feb 13 '18 at 20:18
  • 1
    @Zarko But it still works on version 3.0.1.a Which is very practical because it allows to solve a system of 2 equations to 2 unknown and thus to find the intersection of 2 straight lines whose intersection is not visible. – AndréC Apr 30 '18 at 14:24
  • @AndréC, i didn'r say, that it not works :-), only that it is not described in recent official tikz version. – Zarko Apr 30 '18 at 14:45
  • @Zarko Certainly, I had asked the team to add the documentation here sourceforge.net/p/pgf/feature-requests/108 – AndréC Apr 30 '18 at 14:51
  • 1
    @AndréC, good news! now i hope that the new official tikz version will be realised soon :-)! – Zarko Apr 30 '18 at 15:00

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.