# straight lines + point of intersection in TikZ

I have four points

\coordinate (N1) at (-4*pi/3,    0);
\coordinate (N2) at (-2*pi/3, {( 2/3)*pi*sqrt(3)});
\coordinate (N3) at ( 2*pi/3, {( 2/3)*pi*sqrt(3)});
\coordinate (N4) at ( 4*pi/3,    0);


and want to achieve the following things:

1. draw straight lines (! no line segments) through N1 and N3 (and N2 and N4 respectively)
2. Calculate the point of intersection of those two lines and give it a name (e.g. IS1) so I can use it later on.

This should not be too hard to achieve but I just started learning TikZ and was a bit put off by the ~1100 page reference and was not able to find it there.

• You have some example of how to use intersections within the tutorials at the beginning of the pgf manual. You should quickly find them by <kbd>Ctrl</kbd>+<kbd>F</kbd>ing it (hint: look at §4.1.4 The Intersection of the Circles). – ebosi Nov 10 '16 at 15:08
• See section 13.3.2 Intersections of Arbitrary Paths in the manual, which is about \usetikzlibrary{intersections}. – Torbjørn T. Nov 10 '16 at 15:10
• @ebo: Drawing the lines is the major problem. And thanks for your gracious help... – Simon Fromme Nov 10 '16 at 15:10
• @SimonFromme Yup, how to draw "infinite" lines is an issue I've faced... but workarounded. I'm interrested in the answer, though. The issue is that if you draw infinite lines, your picture won't have boundaries (or supposing "inf = a very big integer", your picture will never fit your page, except if you \clip it). – ebosi Nov 10 '16 at 15:15
• @ebo: Yes the picture is \cliped so I was hoping there was a possibility with an easy syntax like \draw -- (N1) -- (N3) --; which doesn't exist unfortunately. – Simon Fromme Nov 10 '16 at 15:20

By lines and not line segments, I guess you want to extend the lines beyond the coordinates. The following extends the lines 0.5cm beyond the specified coordinates.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections,calc}
\begin{document}
\begin{tikzpicture}
\coordinate (N1) at (-4*pi/3,    0);
\coordinate (N2) at (-2*pi/3, {( 2/3)*pi*sqrt(3)});
\coordinate (N3) at ( 2*pi/3, {( 2/3)*pi*sqrt(3)});
\coordinate (N4) at ( 4*pi/3,    0);

\draw [name path=A] ($(N1)!-0.5cm!(N3)$) -- ($(N3)!-0.5cm!(N1)$);
\draw [name path=B] ($(N2)!-0.5cm!(N4)$) -- ($(N4)!-0.5cm!(N2)$);
\path [name intersections={of=A and B,name=i}];

\foreach \n in {1,...,4}

\end{tikzpicture}
\end{document}

• Thank you! Ideally I would not specify the length 0.5cm manually but have the lines extend all the way to the boundries of the \cliped area. I could certainly just specify a somewhat big number there but that's rather ugly I think. – Simon Fromme Nov 10 '16 at 15:23
• Also, could you explain why you have to reference (i-1) and not (i)? – Simon Fromme Nov 10 '16 at 15:34
• @SimonFromme You didn't say anything about having a \clipped area in the question ... Might be better ways than specifying a big number, can't think of anything immediately though. About i-1: Depending on the paths in question, there can be multiple intersections, so they are always numbered. See the examples in the manual. – Torbjørn T. Nov 10 '16 at 15:38
• @SimonFromme Torbjørn T.'s advice was a good one: have a look at §13.3.2 Intersections of Arbitrary Paths of the pgf manual. Every thing is really well documented there. Basically, you need to specify which intersection you're refering to. It's obvious in the case of two non-parralel line in a 2D referential since there is only one, but TikZ syntax is intended for more general cases - you'll see examples in the manual. – ebosi Nov 10 '16 at 15:38

As alternative/supplement to Torbjørn T. answer, but with slightly shorter code and simpler way of prolonging of lines:

\documentclass[border=0mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections,calc}
\begin{document}
\begin{tikzpicture}[
shrtn/.style = {shorten <=-5mm, shorten >=-5mm}
]
\coordinate (N1) at (-4*pi/3,    0);
\coordinate (N2) at (-2*pi/3, {( 2/3)*pi*sqrt(3)});
\coordinate (N3) at ( 2*pi/3, {( 2/3)*pi*sqrt(3)});
\coordinate (N4) at ( 4*pi/3,    0);
%
\draw [red,name path=A,shrtn] (N1) -- (N3);
\draw [red,name path=B,shrtn] (N2) -- (N4);
%
\fill [name intersections={of=A and B, by={a}}, blue]
(a) circle (3pt);
%
\foreach \n in {1,...,4}
\node[fit=(N1) (N3) (N4), scale=1.1] {};% <-- determine bounding box
\end{tikzpicture}
\end{document}


Edit: Bounding box consider with shrtn prolonged lines til to given coordinates. That at complete line is visible (when standalone has option border=0pt) you have two possibilities:

• calculate coordinaes of lines end (as do Torbjørn T. in his answer)
• determne bounding box with scaled node, which fit given coordinates (see edit MWE above)
• I considered this method as well. One potential downside is that the "shortened" lines are not considered in determining the bounding box -- set the border of standalone to 0 to see this. – Torbjørn T. Nov 10 '16 at 15:45
• @TorbjørnT., about bonding box you have right. I consider your comment in edit of my answer. – Zarko Nov 10 '16 at 16:21

Here's an alternative version with Metapost and luamplib. Compile with lualatex.

\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
numeric u;
u = 3.14159265359 cm; % unit is arbitrary

% implicitly define points z1, z2, z3, and z4
-x1 = x4 = 4/3u;
-x2 = x3 = 2/3u;
y1 = y4 = 0;
y2 = y3 = 2/3u * sqrt(3);

% z5 is the intersection of z1--z3 and z2--z4
z5 = whatever[z1,z3] = whatever[z2,z4];

% idiom to draw line through two points
r = 1+7mm/length(z1-z3);  % with 7mm overhang...
draw r[z1,z3] -- r[z3,z1] withcolor .57 red;
draw r[z2,z4] -- r[z4,z2] withcolor .57 red;

% add some dots and labels
dotlabel.ulft("$N_1$", z1);
dotlabel.llft("$N_2$", z2);
dotlabel.lrt ("$N_3$", z3);
dotlabel.urt ("$N_4$", z4);
dotlabel.top ("$I$",   z5);

endfig;
\end{mplibcode}
\end{document}