(Note: this article was originally written in and transcribed to WordPress, so forgive the equation alignment. Get the original). If you have a line and an ellipse, how can you tell where they intersect? This is a relatively simple problem that has worked-out examples all over the web if you Google for “line ellipse intersection”. However, what I’ve come to find is that nobody will actually *give* you the solution for an *arbitrary* line and an *arbitrary* ellipse. I’m here to do just that.

## Derivation

An ellipse is defined by a long axis and a short axis, called the semi-major and semi-minor axes, respectively. Usually people use the variable to represent the length of the semi-major axis, and to represent the length of the semi-minor axis. In this article I’ll use to represent only the **horizontal** axis and to represent only the **vertical** axis. That said, the formal equation for an ellipse is this: 1 And the equation for a line is this: To avoid confusion about what means, I’ll use the term to represent the y-intercept instead. 2 To find your potentially two intersecting points, you need to solve for and then use the values you found for (there will be two) to find corresponding values for . That is, you need to simultaneously solve equations 1 and 2. But first, let’s discuss our line.

## Equation of a Line for Two Points and

You’re given two points , and you need to find values for slope and y-intercept like in Eqn. 2. Well slope, , is simply the change in over the change in . 3 The actual order of and in Eqn. 2 doesn’t matter — you can have or vice versa and you’ll get the same slope. Now to find the y-intercept, which we’re referring to as , we just take one of our points (arbitrarily choose ) and plug it into our equation to solve for : subtracting from both sides leaves us with our y-intercept : 4 Now that we know our values for , , , and , we are ready to solve for the intersection points between the line and the ellipse. First, substitute the line equation (Eqn. 2) into the ellipse equation (Eqn. 1) so that we can solve for : Expanding the square: We want to have a common denominator for both fractions on the left-hand side, so we’ll multiply the first term by and the second term by : Now we can multiply both sides of the equation by so we don’t have a fraction on the left-hand side: Notice how the first two terms on the left-hand side have a common term: , let’s factor that out: Now let’s notice that the terms and both consist of only our known constants. To make the rest of our solution simpler, let’s label these constants and . That is: 5 6 With our constant-naming out of the way, let’s re-examine our equation: That’s much cleaner isn’t it? Okay, next let’s move the term to the other side: The left-hand side is very clean now: just a quadratic equation. I’m going to use a trick called completing the square to help us solve for . If we first divide everything by we get: Which is of the form . and in this case refer to constants of a quadratic equation, not the same variables we’re using. Because we have it of this form, we know that if we add to both sides, then we can easily factor the left side: Becomes: Now, since we’re interested in finding the value of we need to take a square root of both sides: Evaluating the left hand side: Now we want to find the value for , not so let’s only keep the positive root: Let’s get by itself on the left-hand side by subtracting from both sides: Because things are getting kind of messy with that big square root, I’m going to notice that it’s simply a square root of constants that we already know, and label the whole thing . That is, 7 This makes our equation much cleaner: Later I’ll resubstitute for those constants, but bear with me as I use them to solve for and . We now know that has two solutions: If we take these values for along with our equation for a line (Eqn. 2), then we can solve for : which yields solutions for : This gives us our final intersection points of and If we resubstitute back in for we can simplify it ever so slightly. From Eqn. 7: Let’s substitute back in for and on the first term (Refer to Eqns. 5 and 6): Notice how we have a common term in the numerator, let’s factor it out: Now let’s substitute on the second term: Notice again how there is a common term in the numerator. Now we can factor an out of both, and get an term outside the radical: Finally, to resubstitute everything back into our point equations, our two potential intersection points are: , 8 And , 9

## Conclusions

The final equation for the points isn’t really the cleanest is it? I myself prefer to keep the constants and that I defined. Note that the points you’ve discovered won’t necessarily lie on the ellipse if the line doesn’t intersect the ellipse at all; you should be able to substitute your discovered and values into equation1 and see if it still equals 1. So there you have it, an analytic solution for the intersection points of a line with an ellipse in a convenient equation for you to translate into code. Thanks for reading!

Well, you know how you started out by offering “the solution for an arbitrary line and an arbitrary ellipse” … but then in your equation 1 you define an ellipse as xx/aa + yy/bb = 1 … which is right for a “canonical” ellipse, e.g, with major axis aligned with X-axis, minor axis aligned with Y-axis … but that’s certainly not an “arbitrary” ellipse. The formula for a general ellipse is:

Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0 provided B^2 – 4AC < 0

Humm. I still have some work to do to get the intersection of a line with an arbitrary ellipse. Oh well, not your fault.

Cheers,

Dave.

Hi Dave,

Thanks for the clarification, and thanks for stopping by! I’ll have to look more into it, but I think a good strategy could be to determine a coordinate transform of the general ellipse such that it’s transformed into canonical form, and then you can go from there.

-Andy

The right side of the formula after “This gives us our final intersection points of” should state “- D” twice, not “+ D”. The left side is “+ D” already.

Thanks … I am using this for projecteuler.org Problem 144.

Good catch, Matthias! I’ve updated the post. It seems I also need to dig up that pdf and update it, too. It appears that I did not carry the error over to equations 8 and 9, so use those instead! Glad to hear this is helping you out.

Hi Matthias, I’ve updated the .pdf Thanks again for pointing this out. I need to write another post!!!

Another Project Euler tragic here. Thanks for the equation 🙂

Glad to hear it helped, Neil

[…] a previous post, I outlined an analytical solution for intersecting lines and ellipses. In this post I’m doing much the same thing but rather with lines on lines. I’ll […]