Intersection of Ellipses
Intersection of Ellipses
David Eberly
Magic Software, Inc.
http://www.magic-software.com
1 Introduction
This article describes how to compute the points of intersection of two ellipses, a geometric query labeled
find intersections. It also shows how to determine if two ellipses intersect without computing the points of
intersection, a geometric query labeled test intersection. Specifically, the geometric queries for the ellipses
E0 and E1 are:
An implementation of the find query, in the event of no intersections, might not necessarily determine if one
ellipse is contained in the other or if the two ellipses are separated. Let the ellipses Ei be defined by the
quadratic equations
~
Qi (X) ~ T Ai X
= X ~ +B~TX ~ + Ci
i
h i a(i) a(i) x h i x
00 01 + b(i) (i) + c(i)
= x y (i) (i) 0 b1
a01 a11 y y
= 0
~ < 0 defines the inside of the
for i = 0, 1. It is assumed that the Ai are positive definite. In this case, Qi (X)
~
ellipse and Qi (X) > 0 defines the outside.
2 Find Intersection
The two polynomials f (x) = α0 + α1 x + α2 x2 and g(x) = β0 + β1 x + β2 x2 have a common root if and only
if the Bézout determinant is zero,
(α2 β1 − α1 β2 )(α1 β0 − α0 β1 ) − (α2 β0 − α0 β2 )2 = 0.
1
This is constructed by the combinations
0 = α2 g(x) − β2 f (x) = (α2 β1 − α1 β2 )x + (α2 β0 − α0 β2 )
and
0 = β1 f (x) − α1 g(x) = (α2 β1 − α1 β2 )x2 + (α0 β1 − α1 β0 ),
solving the first equation for x and substituting it into the second equation. When the Bézout determinant
is zero, the common root of f (x) and g(x) is
α2 β0 − α0 β2
x̄ = .
α1 β2 − α2 β1
The common root to f (x) = 0 and g(x) = 0 is obtained from the linear equation α2 g(x) − β2 f (x) = 0 by
solving for x.
The ellipse equations can be written as quadratics in x whose coefficients are polynomials in y,
(i) (i) (i) (i) (i)
Qi (x, y) = a11 y 2 + b1 y + c(i) + 2a01 y + b0 x + a00 x2 .
Using the notation of the previous paragraph with f corresponding to Q0 and g corresponding to Q1 ,
(0) (0) (0) (0) (0)
α0 = a11 y 2 + b1 y + c(0) , α1 = 2a01 y + b0 , α2 = a00 ,
(1) (1) (1) (1) (1)
β0 = a11 y 2 + b1 y + c(1) , β1 = 2a01 y + b0 , β2 = a00 .
u0 = v2 v10 − v42
u1 = v0 v10 + v2 (v7 + v9 ) − 2v3 v4
u2 = v0 (v7 + v9 ) + v2 (v6 − v8 ) − v32 − 2v1 v4
u3 = v0 (v6 − v8 ) + v2 v5 − 2v1 v3
u4 = v0 v5 − v12
with
(0) (1) (1) (0)
v0 = 2 a00 a01 − a00 a01
(0) (1) (1) (0)
v1 = a00 a11 − a00 a11
(0) (1) (1) (0)
v2 = a00 b0 − a00 b0
(0) (1) (1) (0)
v3 = a00 b1 − a00 b1
(0) (1)
v4 = a00 c(1) − a00 c(0)
(0) (1) (1) (0)
v5 = 2 a01 a11 − a01 a11
(0) (1) (1) (0)
v6 = 2 a01 b1 − a01 b1
(0) (1)
v7 = 2 a01 c(1) − a01 c(0)
(0) (1) (1) (0)
v8 = a11 b0 − a11 b0
(0) (1) (1) (0)
v9 = b0 b1 − b0 b1
(0) (1)
v10 = b0 c(1) − b0 c(0)
2
For each ȳ solving R(ȳ) = 0 solve Q0 (x, ȳ) = 0 for up to two values x̄. Eliminate any false solution (x̄, ȳ) by
verifying that Pi (x̄, ȳ) = 0 for i = 0, 1.
3 Test Intersection
3.1 Variation 1
All level curves defined by Q0 (x, y) = λ are ellipses, except for the minimum (negative) value λ for which
the equation defines a single point, the center of every level curve ellipse. The ellipse defined by Q1 (x, y) = 0
is a curve that generally intersects many level curves of Q0 . The problem is to find the minimum level value
λ0 and maximum level value λ1 attained by any (x, y) on the ellipse E1 . If λ1 < 0, then E1 is properly
contained in E0 . If λ0 > 0, then E0 and E1 are separated. Otherwise, 0 ∈ [λ0 , λ1 ] and the two ellipses
intersect.
This can be formulated as a constrained minimization that can be solved by the method of Lagrange mul-
~ subject to the constraint Q1 (X)
tipliers: Minimize Q0 (X) ~ = 0. Define F (X,
~ t) = Q0 (X)~ + tQ1 (X).
~ Differ-
~ = ∇Q
entiating yields ∇F ~ 0 + t∇Q~ 1 where the gradient indicates the derivatives in X.
~ Also, ∂F/∂t = Q1 .
~
Setting the t–derivative equal to zero reproduces the constraint Q1 =. Setting the X–derivative equal to
~ ~ ~
zero yields ∇Q0 + t∇Q1 = 0 for some t. Geometrically this means that the gradients are parallel.
~ i = 2Ai X
Note that ∇Q ~ +B
~ i , so
~ 0 + t∇Q
~0 = ∇Q ~ 1 = 2(A0 + tA1 )X
~ + (B
~ 0 + tB
~ 1 ).
~ yields
Formally solving for X
~ (t)T A1 Y
Y ~ (t) + δ(t)B
~ 1T Y
~ (t) + δ(t)2 C1 = 0,
3.2 Variation 2
A less expensive test query is based on the find query, but cannot answer the question of proper containment
or separation when there is no intersection. Rather than solve the quartic equation R(y) = 0 that was
derived in the section on finding intersections, it is enough to determine if R(y) has any real roots. The
3
ellipses intersect if and only if there are real roots. If u4 = 0 and u3 = 0, then there are real roots as long
as u21 − 4u0 u2 ≥ 0. If u4 = 0 and u3 6= 0, then the cubic polynomial necessarily has a real root. If u4 6= 0,
then multiply the equation, if necessary, by −1 to make the leading coefficient positive. The polynomial has
no real roots if and only if R(y) > 0 for all y. It is enough to compute the local minima of R and show they
are all positive. This requires finding the roots of the cubic polynomial R0 (y) = 0 and evaluating R(y) and
testing if it is positive at those roots.
But it is even possible to avoid finding roots whatsoever. This uses the method of bounding roots by
Sturm sequences. Consider a polynomial f (t) defined on interval [a, b]. A Sturm sequence for f is a set of
polynomials fi (t), 0 ≤ i ≤ m such that Degree(fi+1 ) > Degree(fi ) and the number of distinct real roots for f
in [a, b] is N = s(a)−s(b) where s(a) is the number of sign changes of f0 (a), . . . , fm (a) and s(b) is the number
of sign changes of f1 (b), . . . , fm (b). The total number of real–valued roots of f on IR is s(−∞) − s(∞). It
is not always the case that m = Degree(f ). The classic Sturm sequence is f0 (t) = f (t), f1 (t) = f 0 (t), and
fi (t) = −Remainder(fi−2 /fi−1 ) for i ≥ 2. The polynomials are generated by this method until the remainder
term is a constant. This method is applied to R(y) on (−∞, ∞) to determine the number of real roots.
3.3 Variation 3
This test is similar to variation 2, but it requires that one of the ellipses be axis–aligned (let it be E0 for the
argument). It is possible to force this to happen by an affine change of variables, the correct transformation
requiring determining the eigenvalues of A0 , an operation that involves solving a quadratic equation. If the
application already knows the axes of the ellipses, then this only reduces the computation time. I believe
this argument also shows that R(y) can never be cubic, only quadratic or quartic.
The quadratic equation for the axis–aligned ellipse can be written as (y − y0 )2 = a0 + a1 x + a2 x2 where
a2 < 0. The other ellipse equation can be written as (y − y0 )2 + (b10 + b11 x)(y − y0 ) + (b00 + b01 x + b02 x2 ) = 0.
Substituting (y − y0 )2 from the first equation into the second one, solving the second for (y − y0 ), replacing
it in the first, and cross–multiplying leads to the polynomial P (x) = c0 + c1 x + c2 x2 + c3 x3 + c4 x4 where
c0 = (b00 + a0 )2 − a0 b210
c1 = 2(b00 + a0 )(b01 + a1 ) − 2a0 b10 b11 − a1 b210
c2 = 2(b00 + a0 )(b02 + a2 ) + (b01 + a1 )2 − a0 b211 − a2 b210 − 2a1 b10 b11
c3 = 2(b01 + a1 )(b02 + a2 ) − a1 b211 − 2a2 b10 b11
c4 = (b02 + a2 )2 − a2 b211
Since a2 < 0, the only way c4 = 0 is if a2 = −b02 and b11 = 0. In this case, c3 = 0 is forced. If both
c4 = c3 = 0, then c2 = (b01 + a1 )2 − a2 b210 . The only way c2 = 0 is if a1 = −b01 and b10 = 0. In this
case, c1 = 0 is forced and the polynomial is c0 = 0, finally leading to P (x) being identically zero. The two
quadratic equations are for the same ellipse. So the only three cases to trap in the code are via the Boolean
short circuit, c4 6= 0 or c2 6= 0 or ellipses are the same. The hard case is c4 6= 0, but as in variation 2, it is
enough just to argue whether or not P (y) has roots. This only requires solving a cubic polynomial equation
P 0 (y) = 0 and testing the values of P (y).
The method of Sturm sequences, as shown in variation 2, can also be applied here for the fastest possible
test query.