Data Loading...

The Area of Intersecting Ellipses - Geometric Tools Flipbook PDF

Figure 4. The other region bounded by an elliptical arc and the line segment connecting the arc’s endpoints. The area of


109 Views
23 Downloads
FLIP PDF 280.41KB

DOWNLOAD FLIP

REPORT DMCA

The Area of Intersecting Ellipses David Eberly Geometric Tools, LLC http://www.geometrictools.com/ c 1998-2016. All Rights Reserved. Copyright Created: September 4, 2008 Last Modified: April 20, 2015

Contents 1 Introduction

2

2 Area of an Ellipse

2

3 Area of an Elliptical Sector

3

4 Area Bounded by a Line Segment and an Elliptical Arc

4

5 Area of Intersecting Ellipses

5

5.1

No Intersection Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

5.2

One Intersection Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

5.3

Two Intersection Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

5.4

Three Intersection Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

5.5

Four Intersection Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10

5.6

An Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

12

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

An axis-aligned ellipse centered at the origin is  x 2 a

+

 y 2 b

=1

(1)

where I assume that a > b, in which case the major axis is along the x-axis. Figure 1 shows such an ellipse.

Figure 1. An axis-aligned ellipse centered at the origin with a > b.

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

2

The integral may be computed using the change of variables x = a cos φ for 0 ≤ φ ≤ π/2. The differential is dx = −a sin φ dφ and the area is

=

Ra p b 1 − (x/a)2 dx 0 R0 4b π/2 sin φ(−a sin φ dφ) R π/2 4ab 0 sin2 φ dφ R π/2 2ab 0 (1 − cos(2φ)) dφ  π/2 2ab φ − 21 sin(2φ) 0   2ab π2 − 21 sin (π) − 0 −

=

πab

A = = = = =

3

4

(3)

1 2

 sin(0)

Area of an Elliptical Sector

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.

Figure 2. An elliptical arc and its 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 a2 b2 /2 dθ 1 2 r dθ = A(θ0 , θ1 ) = (5) 2 2 2 2 θ0 b cos θ + a sin θ θ0 2

3

An antiderivative of the integrand is    ab (b − a) sin 2θ −1 F (θ) = θ − Tan 2 (b + a) + (b − a) cos 2θ

(6)

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)

4

Area Bounded by a Line Segment and an Elliptical Arc

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 )| 2 2 2

(8)

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 | 2

(9)

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.

The area of this region is α(θ1 , θ0 + 2π) = πab − α(θ0 , θ1 )

(10)

The bounded region has the area of the ellipse minus the area of the smaller region bounded by the line segment and elliptical arc. In Equation (9), the origin is outside the bounded region. In Equation (10), the origin is inside the bounded region.

5

Area of Intersecting Ellipses

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 GteIntrEllipse2Ellipse2.h. In the following discussion, the ellipses are named E0 and E1 . The subsections describe each geometric configuration that can arise.

5.1

No Intersection Points

One ellipse is contained strictly in the other, or the ellipses (as solids) are separated. Figure 5 illustrates.

5

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.

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 && 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 ] ; } }

6

5.2

One Intersection Point

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.

The area of intersection is computed using the function AreaCS of Listing 1.

5.3

Two Intersection Points

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, 7

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 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 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 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 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 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 and f o r E1 i s . 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 and f o r E1 i s . return ComputeAreaChordRegion ( E0 , P1mC0 , P0mC0) + ComputeAreaChordRegion ( E1 , P0mC1 , P1mC1 ) ; } }

The function ComputeAreaChordRegion is an implementation of the algorithm described in Section 4. Listing 3 is pseudocode for this method.

8

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 P0mC , V e c t o r 2 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 . // 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 ; }

Wrap

// 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 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 ); } } Real ComputeIntegral ( E l l i p s e E , Real theta ) { R e a l twoTheta = 2 ∗ t h e t a , s n = s i n ( twoTheta ) , c s = c o s ( twoTheta ) ; Real halfAB = E . e x t e n t [ 0 ] ∗ E . e x t e n t [ 1 ] / 2 ; R e a l BpA = E . e x t e n t [ 1 ] + E . e x t e n t [ 0 ] ; R e a l BmA = E . e x t e n t [ 1 ] − E . e x t e n t [ 0 ] ; R e a l a r g = BmA ∗ s n / (BpA + BmA ∗ c s ) ; r e t u r n halfAB ∗ ( t h e t a − atan ( arg ) ) ; }

5.4

Three Intersection Points

The two ellipses intersect tangentially at one point and transversely at two points. Figure 8 illustrates.

9

Figure 8. Ellipses that intersect in 3 points.

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.

5.5

Four Intersection Points

The two ellipses intersect transversely at four points. Figure 9 illustrates.

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. 10

Listing 4. intersection.

Pseudocode for computing the area of intersection of ellipses when there are 4 points of

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 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 // GTEngine 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 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 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 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 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 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 ++) { // 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 P0 = p o i n t s [ p e r m u t e [ i 0 ] ] ; V e c t o r 2 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 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 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 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 ; }

11

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 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 ) ; i f ( num Poi nts > 0 ) { i f ( num Poi nts == 1 ) { // C o n t a i n m e n t o r s e p a r a t i o n . r e t u r n AreaCS ( E0 , E1 , nu mPo int s ) ; } e l s e i f ( n um Poi nts == 2 ) { i f ( isTransverse [0]) { // Both i n t e r s e c t i o n p o i n t s a r e t r a n s v e r s e . r e t u r n A r e a 2 ( E0 , E1 , 0 , 1 , p o i n t s ) ; } else { // Both i n t e r s e c t i o n p o i n t s a r e t a n g e n t i a l , s o one e l l i p s e // 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 ) ; } } e l s e 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 t h e a r e a c o m p u t a t i o n . 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 oi nts == 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

}

13