![](/style/images/good.png)
![](/style/images/bad.png)
An Analytic Solution for Ellipse and Line Intersection
source link: https://gieseanw.wordpress.com/2013/07/19/an-analytic-solution-for-ellipse-and-line-intersection/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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 ![P_1](https://s0.wp.com/latex.php?latex=P_1&bg=ffffff&fg=333333&s=0&c=20201002)
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!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK