2

This question is a follow up of TikZ: how to find the intersection of two extended lines?

I have a moderately complicated TikZ graphic that shows the setup of an optical system. I need to calculate the intersection of two extended lines defined as the lines which pass respectively through (A)--(B) and (C)--(D).

I have a few \def at the beginning which I can modify to alter some parameters of the optics. These \def obviously also alter the relative positioning of the four points. The solution proposed in the question I linked is not perfect for me, because I can't a priorii know the length I should use in the calc command $(A)!length!(B)$, neither I can do \path [name path=AB] (A) -- ($(B)!-100cm!(A)$);, which creates an enormous line, even if not drawn.

Mi ideal solution would be something like the one proposed by Count-Zero using tkz-euclide package, which it's quite hard for me to use because I can't translate my whole drawing in tkz-euclide style due to the lack of english documentation of the package...

Rackbox
  • 927
  • 3
    Try to add the overlay option to your "enormous" \path... – Paul Gaborit May 13 '16 at 08:09
  • 2
    Enclose the extended path in a pgfinterruptboundingbox environment. This way the bounding box is not adjusted to fit the invisible path. The same can be achieved by the overlay option as noted by Paul Gaborit. – Henri Menke May 13 '16 at 08:09
  • 3
    If you read this answer: http://tex.stackexchange.com/a/31402/15036 you will see a method that only uses tkzeuclide to find the intersection... So you would not have to rewrite your whole diagram. – Thruston May 13 '16 at 08:23
  • 1
    For optical setups you could have a look at my pst-optexp package – Christoph Jun 01 '16 at 19:12

1 Answers1

5

TikZ has an by now undocumented intersection coordinate system that can implicitly used as

(intersection of <c11>--<c12> and <c21>--<c22>)

which finds the intersections of a line from (<c11>) to (<c12>) and a line from (<c21>) to (<c22>) even if this intersection doesn't lie on the line segments between these points.

So, in your case, you just need to do

(intersection of A--B and C--D)

and it will return the required point. (It will throw an error if these lines are parallel or almost parallel because the intersection is too far away.)

The intersection coordinate system just takes the four given points and solves a linear system of equations for two lines (and not lines segment).

From quick view of the defintion of \tkzInterLL the tkz-euclide only does math, too.


To be fair, the intersections library only does math, too, but of course works for arbitrary paths but only returns points that actually lie on the paths.

I'll add another solution using the intersections library where the paths are constructed by extending the lines about 37cm (maximum distance that fits on an A4 paper) in both directions and then simply finds the intersection as always.

The option overlay means that the path doesn't contribute to the bounding box. path only makes sure that no inherited option will make the path visible.

Code (intersection cs)

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[nodes={circle, minimum size=+4pt, inner sep=+0pt, draw}]
\pgfmathsetseed{309382}
\path (rand-5, 3*rand) node (A) [label=A] {}
      (rand-3, 3*rand) node (B) [label=B] {}
      (rand+2, 3*rand) node (C) [label=C] {}
      (rand+4, 3*rand) node (D) [label=D] {};
\draw (A)--(B) (C)--(D);
\path[red] node (E) at (intersection of A--B and C--D) [label=E] {};
\end{tikzpicture}
\end{document}

Code (intersections library)

\documentclass[tikz]{standalone}
\usetikzlibrary{intersections,calc}
\tikzset{
  intersection of/.code args={#1--#2 and #3--#4}{
    \path[overlay,path only,name path=@a]($(#1)!-37cm!(#2)$)--($(#2)!-37cm!(#1)$);
    \path[overlay,path only,name path=@b]($(#3)!-37cm!(#4)$)--($(#4)!-37cm!(#3)$);
    \tikzset{name intersections={of=@a and @b}}}}
\begin{document}
\begin{tikzpicture}[nodes={circle, minimum size=+4pt, inner sep=+0pt, draw}]
\pgfmathsetseed{309382}
\path (rand-5, 3*rand) node (A) [label=A] {}
      (rand-3, 3*rand) node (B) [label=B] {}
      (rand+2, 3*rand) node (C) [label=C] {}
      (rand+4, 3*rand) node (D) [label=D] {};
\draw (A)--(B) (C)--(D);
\tikzset{intersection of=A--B and C--D}
\path[red] node (E) at (intersection-1) [label=E] {};
\end{tikzpicture}
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821