Are A Intersecting Ellipses
Are A Intersecting Ellipses
Contents
1 Introduction 2
2 Area of an Ellipse 2
1
1 Introduction
This document describes an algorithm for computing the area of intersection of two ellipses. The formulas
are in closed form, thus providing the exact area in terms of real-valued arithmetic. Naturally, the computer
evaluation of the trigonometric functions in the formulas has some numerical round-off errors, but the
formulas allow you to avoid (1) approximating the ellipses by convex polygons, (2) using the intersection of
convex polygons as an approximation to the intersection of ellipses, and (3) using the area of intersection of
convex polygons as an approximation to the area of intersection of ellipses.
The algorithm has two main aspects: Computing the points of intersection of the ellipses and computing
the area bounded by a line and an elliptical arc. Computing the intersection points is described in lengthy
detail in Intersection of Ellipses.
2 Area of an Ellipse
The area bounded by the ellipse is πab. Using the methods of calculus, the area A is four times that of the
area in the first quadrant, Z a Z a p
A=4 y dx = 4 b 1 − (x/a)2 dx (2)
0 0
The integral may be computed using the change of variables x = a cos φ for 0 ≤ φ ≤ π/2. The differential is
2
dx = −a sin φ dφ and the area is
Ra p
A = 4 0
b 1 − (x/a)2 dx
R0
= 4b π/2 sin φ(−a sin φ dφ)
R π/2
= 4ab 0 sin2 φ dφ
R π/2
= 2ab 0 (1 − cos(2φ)) dφ (3)
π/2
= 2ab φ − 21 sin(2φ) 0
2ab π2 − 21 sin (π) − 0 − 1
= 2 sin(0)
= πab
An elliptical arc is a portion of the ellipse bounded by two points on the ellipse. The arc is delimited by angles
θ0 and θ1 with θ0 < θ1 . An elliptical sector is the region bounded by an elliptical arc and the line segments
containing the origin and the endpoints of the arc. Figure 2 shows an elliptical arc and the corresponding
elliptical sector.
The polar-coordinate representation of the arc is obtained by substituting x = r cos θ and y = r sin θ into
Equation (1) and solving for r2 ,
a2 b2
r2 = 2 (4)
b cos2 θ + a2 sin2 θ
The area of the sector is
Z θ1 Z θ1
1 2 a2 b2 /2 dθ
A(θ0 , θ1 ) = r dθ = 2 2 2 2 (5)
θ0 2 θ0 b cos θ + a sin θ
3
where Tan−1 (z) is the principal branch of the inverse tangent function whose range is (−π/2, π/2). The area
of the elliptical sector is therefore
A(θ0 , θ1 ) = F (θ1 ) − F (θ0 ) (7)
Figure 3 shows the region bounded by an elliptical arc and the line segment connecting the arc’s endpoints.
Figure 3. The region bounded by an elliptical arc and the line segment connecting the arc’s endpoints.
The area of this region is the area of the elliptical sector minus the area of the triangle whose vertices are the
origin, (0, 0), and the arc endpoints (x0 , y0 ) = (r0 cos θ0 , r0 sin θ0 ) and (x1 , y1 ) = (r1 cos θ1 , r1 sin θ1 ), where
θi are the polar angles to the points and where ri are determined using Equation (4). The triangle area is
1 r0 r1 r0 r1
|x1 y0 − x0 y1 | = | cos θ1 sin θ0 − cos θ0 sin θ1 | = | sin(θ1 − θ0 )| (8)
2 2 2
In an implementation it is sufficient to use |x1 y0 − x0 y1 |/2 rather than compute the right-hand side of
Equation (8).
If α(θ0 , θ1 ) denotes the area of the aforementioned region, then
1
α(θ0 , θ1 ) = A(θ0 , θ1 ) − |x1 y0 − x0 y1 | (9)
2
where A(θ0 , θ1 ) is the area of the sector as defined in Equation (7) and the other term on the right-hand
side is the area of the triangle.
The region of interest could be that bounded by the line segment and the elliptical arc that is spanned
counterclockwise from θ1 to θ0 + 2π. Figure 4 illustrates.
4
Figure 4. The other region bounded by an elliptical arc and the line segment connecting the arc’s endpoints.
We need to compute the points of intersection between two ellipses, including classification whether the
intersections are transverse or tangential. A robust algorithm for this is described in Intersection of Ellipses,
and the implementation is in the file IntrEllipse2Ellipse2.h. In the following discussion, the ellipses are named
E0 and E1 . The subsections describe each geometric configuration that can arise.
One ellipse is contained strictly in the other, or the ellipses (as solids) are separated. Figure 5 illustrates.
Figure 5. Left: One ellipse is contained by the other. Right: The ellipses are separated.
In the case of containment, the area of intersection is the area of the smaller ellipse. In the case of separation,
the area of intersection is zero. The logic is shown in Listing 1.
5
Listing 1. The area computation when the ellipses are separated, one contains the other, or one is outside
the other but just touching the other.
R e a l AreaCS ( E l l i p s e E0 , E l l i p s e E1 , i n t nu mPo int s )
{
i f ( num Poi nts <= 1 )
{
V e c t o r 2<R e a l> d i f f = E0 . c e n t e r = E1 . c e n t e r ;
R e a l qform0 = Dot ( d i f f , E0 .M * d i f f ) ;
R e a l qform1 = Dot ( d i f f , E1 .M * d i f f ) ;
i f ( qform0 > 1 && qform1 > 1 )
{
// Each e l l i p s e c e n t e r i s o u t s i d e t h e o t h e r e l l i p s e , s o t h e
// e l l i p s e s a r e s e p a r a t e d ( nu mPo int s == 0 ) o r o u t s i d e e a c h
// o t h e r and j u s t t o u c h i n g ( num Poi nts == 1 ) .
return 0;
}
else
{
// One e l l i p s e i s i n s i d e t h e o t h e r . D e t e r m i n e t h i s i n d i r e c t l y by
// c o m p a r i n g a r e a s .
R e a l p r o d u c t 0 = E0 . e x t e n t [ 0 ] * E0 . e x t e n t [ 1 ] ;
R e a l p r o d u c t 1 = E1 . e x t e n t [ 0 ] * E1 . e x t e n t [ 1 ] ;
i f ( product0 < product1 )
{
// E1 c o n t a i n s E0
return pi * product0 ;
}
else
{
return pi * product1 ;
}
}
}
else // I n o u r i m p l e m e n t a t i o n , we s e t nu mPo int s t o INT MAX f o r t h i s c a s e .
{
// The e l l i p s e s a r e t h e same .
r e t u r n p i * E0 . e x t e n t [ 0 ] * E0 . e x t e n t [ 1 ] ;
}
}
One ellipse is contained in the other but the two ellipses are tangent at the point of intersection, or the
ellipses (as solids) are separated except for a single point of tangency. Figure 6 illustrates.
Figure 6. Left: One ellipse is contained by the other but they are tangent at a single point. Right: The
ellipses are separated except for a single point of tangency. The intersection point is drawn as a red dot.
6
The area of intersection is computed using the function AreaCS of Listing 1.
Two possible configurations are possible. Either one ellipse is contained in the other but the two ellipses are
tangent at the point of intersection, or the ellipses intersect at two distinct points. Figure 7 illustrates.
Figure 7. Left: One ellipse is contained by the other but they are tangent at two points. Right: The ellipses
intersect at two distinct points. The intersection points are drawn as red dots.
For the case shown in the left of the figure, the area of intersection is computed using the function AreaCS
because one ellipse is contained in the other.
The other case is shown in the right of the figure. The region of intersection is bounded by two elliptical
arcs, one from each ellipse. We must compute the areas bounded by the chord connecting the arc endpoints,
one for ellipse E0 and one for ellipse E1 . Observe that the chord partitions an ellipse into two subsets. We
need to decide which subset is the one for which we need to compute the area. This is determined by the
ordering of the two points of intersection. In Figure 7, notice that at P0 , the tangent vector pointing in the
direction of the counterclockwise traversal of E0 can be used to decide that we compute the area between
the ordered chord hP0 , P1 i and the E0 -arc above it.
Ellipse E1 has the opposite property. The tangent pointing in the direction of the counterclockwise traversal
of E1 points outside E1 , so the ordered chord is hP1 , P0 i and we compute the area between it and the
E1 -arc below it. The ordering therefore relies solely on computing the sign of the dot-perp of tangents,
or equivalently computing the sign of the dot-perp of normals (which are provided by the gradients of the
quadratic polynomials defining the ellipses).
Listing 2 is the pseudocode for computing the area for the right-most configuration of Figure 7.
Listing 2. Pseudocode for computing the area of intersection when there are two distinct points of
intersection. The code is shared by the 3-point intersection case, so the last two parameters are indices of
the proper 2 points to use in the array of intersection points.
R e a l A rea 2 ( E l l i p s e E0 , E l l i p s e E1 , i n t i 0 , i n t i 1 , V e c t o r 2<R e a l> p o i n t s [ 4 ] )
{
// The e n d p o i n t s o f t h e c h o r d .
V e c t o r 2<R e a l> P0 = p o i n t s [ i 0 ] , P1 = p o i n t s [ i 1 ] ;
// Compute l o c a t i o n s r e l a t i v e t o t h e e l l i p s e s .
V e c t o r 2<R e a l> P0mC0 = P0 = E0 . c e n t e r , P0mC1 = P0 = E1 . c e n t e r ;
7
V e c t o r 2<R e a l> P1mC0 = P1 = E0 . c e n t e r , P1mC1 = P1 = E1 . c e n t e r ;
// Compute t h e e l l i p s e n o r m a l v e c t o r s a t e n d p o i n t P0 . This i s s u f f i c i e n t
// i n f o r m a t i o n t o d e t e r m i n e c h o r d e n d p o i n t o r d e r . The m a t r i c e s M a r e
// from t h e s t a n d a r d form , (X=C) ˆT * M * (X=C) = 1 .
V e c t o r 2<R e a l> N0 = E0 .M * P0mC0 , N1 = E1 .M * P0mC1 ;
R e a l d o t p e r p = DotPerp ( N1 , N0 ) ;
// Choose t h e e n d p o i n t o r d e r f o r t h e c h o r d r e g i o n a s s o c i a t e d w i t h E0 .
i f ( dotperp > 0)
{
// The c h o r d o r d e r f o r E0 i s <P0 , P1> and f o r E1 i s <P1 , P0>.
return
ComputeAreaChordRegion ( E0 , P0mC0 , P1mC0) +
ComputeAreaChordRegion ( E1 , P1mC1 , P0mC1 ) ;
}
else
{
// The c h o r d o r d e r f o r E0 i s <P1 , P0> and f o r E1 i s <P0 , P1>.
return
ComputeAreaChordRegion ( E0 , P1mC0 , P0mC0) +
ComputeAreaChordRegion ( E1 , P0mC1 , P1mC1 ) ;
}
}
Listing 3. Pseudocode for computing the area bounded by an elliptical arc and the chord that connects
the endpoints of the arc.
R e a l ComputeAreaChordRegion ( E l l i p s e E , V e c t o r 2<R e a l> P0mC , V e c t o r 2<R e a l> P1mC)
{
// Compute p o l a r c o o r d i n a t e s f o r P0 and P1 on t h e e l l i p s e .
R e a l x0 = Dot ( E . a x i s [ 0 ] , P0mC ) ;
R e a l y0 = Dot ( E . a x i s [ 1 ] , P0mC ) ;
R e a l t h e t a 0 = a t a n 2 ( y0 , x0 ) ;
R e a l x1 = Dot ( E . a x i s [ 0 ] , P1mC ) ;
R e a l y1 = Dot ( E . a x i s [ 1 ] , P1mC ) ;
R e a l t h e t a 1 = a t a n 2 ( y1 , x1 ) ;
// The a r c s t r a d d l e s t h e a t a n 2 d i s c o n t i n u i t y on t h e n e g a t i v e x= a x i s . Wrap
// t h e s e c o n d a n g l e t o be l a r g e r t h a n t h e f i r s t a n g l e .
i f ( theta1 < theta0 )
{
t h e t a 1 += t w o P i ;
}
// Compute t h e a r e a p o r t i o n o f t h e s e c t o r due t o t h e t r i a n g l e .
R e a l t r i A r e a = f a b s ( DotPerp (P0mC , P1mC ) ) / 2 ;
// Compute t h e c h o r d r e g i o n a r e a .
Real dtheta = theta1 = theta0 ;
R e a l F0 , F1 , s e c t o r A r e a ;
i f ( d t h e t a <= p i )
{
// Use t h e a r e a f o r m u l a d i r e c t l y .
// area ( theta0 , theta1 ) = F( theta1 ) = F( theta0 ) = area ( t r i a n g l e )
F0 = C o m p u t e I n t e g r a l ( E , t h e t a 0 ) ;
F1 = C o m p u t e I n t e g r a l ( E , t h e t a 1 ) ;
s e c t o r A r e a = F1 = F0 ;
return sectorArea = triArea ;
}
else
{
// The a n g l e o f t h e e l l i p t i c a l s e c t o r i s l a r g e r t h a n p i r a d i a n s .
8
// Use t h e a r e a f o r m u l a
// a r e a ( t h e t a 0 , t h e t a 1 ) = p i *a*b = a r e a ( t h e t a 1 , t h e t a 0 )
t h e t a 0 += t w o P i ; // e n s u r e t h e t a 0 > t h e t a 1
F0 = C o m p u t e I n t e g r a l ( E , t h e t a 0 ) ;
F1 = C o m p u t e I n t e g r a l ( E , t h e t a 1 ) ;
s e c t o r A r e a = F0 = F1 ;
return pi * E. extent [0] * E. extent [1] = ( sectorArea = triArea );
}
}
The two ellipses intersect tangentially at one point and transversely at two points. Figure 8 illustrates.
The point of tangency is irrelevant here. The region of intersection is still bounded by two elliptical arcs,
one from each ellipse, so the algorithm for 2 intersection points still applies. In the figure, P2 is not required
in the area calculation. However, it is required to know which 2 of the 3 intersections are the endpoints of
the chord of interest.
9
Figure 9. Ellipses that intersect in 4 points.
The region of intersection is the union of a convex quadrilateral and four chord regions, each region bounded
by an elliptical arc and the line segment connecting the endpoints of the arc.
The key observations are (1) we need an ordering of the quadrilateral vertices for each ellipse [the code uses
counterclockwise] and (2) to compute the areas is that we need to know which elliptical arc to use for an
ordered chord. The ordering is obtained by computing polar coordinates for the points relative to the ellipse.
The dot-perp concept used for the function Area2 allows us to identify the correct arc to use.
Pseudocode for the function that computes the area of intersection is shown in Listing 4.
Listing 4. Pseudocode for computing the area of intersection of ellipses when there are 4 points of
intersection.
R e a l A rea 4 ( E l l i p s e E0 , E l l i p s e E1 , V e c t o r 2<R e a l> p o i n t s [ 4 ] )
{
// S e l e c t a c o u n t e r c l o c k w i s e o r d e r i n g o f t h e p o i n t s o f i n t e r s e c t i o n . Use
// t h e p o l a r c o o r d i n a t e s f o r E0 t o do t h i s . The m u l t i m a p i s u s e d i n t h e
// e v e n t t h a t c o m p u t i n g t h e i n t e r s e c t i o n s i n v o l v e d n u m e r i c a l r o u n d i n g
// e r r o r s t h a t l e a d t o a d u p l i c a t e i n t e r s e c t i o n , e v e n t h o u g h t h e
// i n t e r s e c t i o n s a r e a l l l a b e l e d a s t r a n s v e r s e . [ See t h e comment i n t h e
// GTE f u n c t i o n S p e c i a l I n t e r s e c t i o n . ]
multimap<R e a l , i n t > o r d e r i n g ;
int i ;
f o r ( i = 0 ; i < 4 ; ++i )
{
V e c t o r 2<R e a l> PmC = p o i n t s [ i ] = E0 . c e n t e r ;
R e a l x = Dot ( E0 . a x i s [ 0 ] , PmC ) ;
R e a l y = Dot ( E0 . a x i s [ 1 ] , PmC ) ;
Real theta = atan2 ( y , x ) ;
or de ri ng . i n s e r t ( make pair ( theta , i ) ) ;
}
a r r a y <i n t , 4> p e r m u t e ;
i = 0;
f o r e a c h ( element in o rd er ing )
{
p e r m u t e [ i ++] = e l e m e n t . s e c o n d ;
}
// S t a r t w i t h t h e a r e a o f t h e c o n v e x q u a d r i l a t e r a l .
V e c t o r 2<R e a l> d i a g 2 0 = p o i n t s [ p e r m u t e [ 2 ] ] = p o i n t s [ p e r m u t e [ 0 ] ] ;
V e c t o r 2<R e a l> d i a g 3 1 = p o i n t s [ p e r m u t e [ 3 ] ] = p o i n t s [ p e r m u t e [ 1 ] ] ;
R e a l a r e a = f a b s ( DotPerp ( d i a g 2 0 , d i a g 3 1 ) ) / 2 ;
// V i s i t e a c h p a i r o f c o n s e c u t i v e p o i n t s . The s e l e c t i o n o f e l l i p s e f o r
// t h e c h o r d = r e g i o n a r e a c a l c u l a t i o n u s e s t h e ” most c o u n t e r c l o c k w i s e ”
// t a n g e n t v e c t o r .
f o r ( i n t i 0 = 3 , i 1 = 0 ; i 1 < 4 ; i 0 = i 1 ++)
10
{
// Get a p a i r o f c o n s e c u t i v e p o i n t s .
V e c t o r 2<R e a l> P0 = p o i n t s [ p e r m u t e [ i 0 ] ] ;
V e c t o r 2<R e a l> P1 = p o i n t s [ p e r m u t e [ i 1 ] ] ;
// Compute l o c a t i o n s r e l a t i v e t o t h e e l l i p s e s .
V e c t o r 2<R e a l> P0mC0 = P0 = E0 . c e n t e r , P0mC1 = P0 = E1 . c e n t e r ;
V e c t o r 2<R e a l> P1mC0 = P1 = E0 . c e n t e r , P1mC1 = P1 = E1 . c e n t e r ;
// Compute t h e e l l i p s e n o r m a l v e c t o r s a t e n d p o i n t P0 . The m a t r i c e s
// M a r e from t h e s t a n d a r d form , (X=C) ˆT * M * (X=C) = 1 .
V e c t o r 2<R e a l> N0 = E0 .M * P0mC0 , N1 = E1 .M * P0mC1 ;
R e a l d o t p e r p = DotPerp ( N1 , N0 ) ;
i f ( dotperp > 0)
{
// The c h o r d g o e s w i t h e l l i p s e E0 .
a r e a += ComputeAreaChordRegion ( E0 , P0mC0 , P1mC0 ) ;
}
else
{
// The c h o r d g o e s w i t h e l l i p s e E1 .
a r e a += ComputeAreaChordRegion ( E1 , P0mC1 , P1mC1 ) ;
}
}
return area ;
}
5.6 An Implementation
The general logic for the area computation is shown in Listing 5 and is based on the discussions in the
previous subsections.
Listing 5. The top-level dispatch function for computing the area of intersection of two ellipses. The
points of intersection are computed first, including whether each is transverse or tangential. The dispatcher
calls the correct function for each geometrically distinct case.
R e a l A r e a O f I n t e r s e c t i o n ( E l l i p s e E0 , E l l i p s e E1 )
{
// Each p a i r ( p o i n t s [ i ] , i s T r a n s v e r s e [ i ] ) s t o r e s t h e p o i n t o f i n t e r s e c t i o n
// and a B o o l e a n v a l u e t h a t i s ’ t r u e ’ when t h e i n t e r s e c t i o n i s t r a n s v e r s e
// b u t ’ f a l s e ’ when t h e i n t e r s e c t i o n i s t a n g e n t i a l .
V e c t o r 2<R e a l> p o i n t s [ 4 ] ;
bool i s T r a n s v e r s e [ 4 ] ;
i n t nu mPo int s ;
F i n d I n t e r s e c t i o n s ( E0 , E1 , numPoints , p o i n t s , i s T r a n s v e r s e ) ;
11
// i s c o n t a i n e d i n t h e o t h e r .
r e t u r n AreaCS ( E0 , E1 , n umP oin ts ) ;
}
}
else i f ( n um Poi nts == 3 )
{
// The t a n g e n t i a l i n t e r s e c t i o n i s i r r e l e v a n t i n the area computation .
i f (! isTransverse [0])
{
r e t u r n A r e a 2 ( E0 , E1 , 1 , 2 , p o i n t s ) ;
}
else i f (! isTransverse [1])
{
r e t u r n A r e a 2 ( E0 , E1 , 2 , 0 , p o i n t s ) ;
}
else // i s T r a n s v e r s e [ 2 ] == f a l s e
{
r e t u r n A r e a 2 ( E0 , E1 , 0 , 1 , p o i n t s ) ;
}
}
else // n umP oin ts == 4
{
r e t u r n A r e a 4 ( E0 , E1 , p o i n t s ) ;
}
}
else
{
// C o n t a i n m e n t , s e p a r a t i o n , o r same e l l i p s e .
r e t u r n AreaCS ( E0 , E1 , nu mP oints ) ;
}
}
12