# How to calculate intersections of a line defined by a segment and an ellipse in tikz

EDIT Here's what I intended to ask, but didn't clearly formulate initially. The Tikz package intersections allows one to calculate and manipulate the intersection point of two non-parallel lines defined by (non intersecting) line segments. The link in my original question shows how to do this, it is how the point (F) is defined. In the example below, the point (0) is defined as the intersection of the lines extending the line segments vertline and horline. What I want to do is calculate and manipulate intersection points of an ellipse with the line extending a given line segment, without having to rely on extending the line segment ''by hand'' as it were, to ensure it actually intersects the ellipse. When I copy the syntax used in the code from the link, and naively adapt it,

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\usetikzlibrary{intersections}
\path[name path=ellipse] (0,0) ellipse (4 and 3);
\path[name path=horline] (1,0) -- (2,0);
\path[name path=vertline] (0,1) -- (0,2);
\path[name intersections={of = horline and vertline, by={origin}}, draw]
(origin) node [above right] {$0$} circle (1.5pt);
\path[name intersections={of = ellipse and vertline}]
\coordinate (I) at (intersection-1);
\coordinate (J) at (intersection-2);
\draw (I) circle (1.5pt);
\draw (J) circle (1.5pt);
\end{tikzpicture}
\end{document}


it doesn't give me the desired result : the intersections (I) and (J) are of ellipse and vertline are totally misplaced. Moreover, the code is faulty, but I don't understand where the error lies. Please tell me if you'd code things differently, I don't want to develop bad habits.

Further Edit It appears (I) and(J) aren't misplaced, they simply aren't calculated at all, and what appeared on the pdf were points (I) and(J) defined in a previous drawing.

Oringinal Question - May be dicarded In this Example, the author gets tikz to calculate the coordinates of the two intersections (X and Y) of an ellipse with the line defined by a certain segment, and also the coordinates of the intersection (F) of two lines defined by two line segments.

On my machine, I have no problem with the second problem, finding the intersection point of two lines defined by (non intersecting line segments), but i get non-sense when I try to perform the intersection of an ellipse with the line defined by a line segment that doesn't intersect the ellipse.

• Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Commented Nov 27, 2013 at 5:30
• @KevinC The link provides this. Commented Nov 27, 2013 at 5:37
• So you're saying that you can't get the points X and Y to show up? The linked example compiles okay with me (MikTeX 2.9), and the output is as expected. Commented Nov 27, 2013 at 5:44
• @KevinC I'll get back at you tomorow. Commented Nov 27, 2013 at 5:48
• @KevinC I've edited the question, I think it is clearer now what I'm asking. Commented Nov 27, 2013 at 15:31

You can calculate the intersection of two non-parallel lines using the coordinate specification (intersection of A--B and C--D), where A, B, C and D have to be named nodes. That's the approach used in the code you linked to. This, however, does not work with arbitrary named paths, which is what you're trying to do.

To calculate the intersections of a line with an arbitrary path, you'll have to make sure that the paths actually intersect. If you don't want to manually extend the line segment, you can use the approach from Intersection with rays in TikZ and create "infinitely long" (1 metre, say) line segments inside a interruptboundingbox environment:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\usetikzlibrary{intersections, calc}

\path[name path=ellipse] (0,0) ellipse (4 and 3);
\begin{pgfinterruptboundingbox}
\path[name path global=horline] ($(1,0)!-100cm!(2,0)$) -- ($(1,0)!100cm!(2,0)$);
\path[name path global=vertline] ($(0,1)!-100cm!(0,2)$) -- ($(0,1)!100cm!(0,2)$);
\end{pgfinterruptboundingbox}
\path[name intersections={of = horline and vertline, by={origin}}, draw]
(origin) node [above right] {$0$} circle (1.5pt);
\path[name intersections={of = ellipse and vertline}];
\coordinate (I) at (intersection-1);
\coordinate (J) at (intersection-2);
\draw (I) circle (1.5pt);
\draw (J) circle (1.5pt);
\end{tikzpicture}
\end{document}

• I get an intersection, but it's not where it should be. Commented Nov 27, 2013 at 16:17
• Check your .log file, there's probably the same error message in there and the node is just put at the origin. In the code you linked to, the point F is found using intersection of A--D and B--C, which is not the same as the intersection of two named paths.
– Jake
Commented Nov 27, 2013 at 16:18
• In the .log file the error message is with respect to intersection-1, it says ! Package pgf Error: No shape named intersection-1 is known. Commented Nov 27, 2013 at 16:25
• @OlivierBégassat: Yes, that's the same message that I got. On my system, that error halted the compilation (which it should, since everything after that is unreliable). I've edited my answer to show how your code can be fixed.
– Jake
Commented Nov 27, 2013 at 16:31
• Your 'Intersection with rays' answer is very very neat. I think I'll use that. I still wish there was an intrinsic way to work with infinite lines or half lines. While we're talking, what manual do you recommend for learning Tikz? Also, do I need to learn pgftricks aswell? Commented Nov 27, 2013 at 16:43