This section details the implementation of ellipse intersections in Sara. This section is extracted from the appendix of my thesis Ok:2013:phdthesis
and has been retouched a bit.
Eberly
provides a comprehensive study on the computation of ellipses intersection, namely the computation of its area and its intersection points. This is non a trivial geometric problem. We complement Eberly
’s study with additional technical details about the area computation of two intersecting ellipses.
Let ℰ be a ellipse with semi-major axis a and semi-minor axis b, i.e., a ≥ b > 0. Let us first suppose that ℰ is centered at the origin and is axis-aligned oriented, i.e., such that the axis a is along the x-axis and the axis b along the y-axis. Then,
Important
the equation of ellipse ℰ is
Riemann sum approximating the upper quadrant area of the ellipse.
Using the symmetry in the ellipse, the area of ellipse ℰ is 4 times the upper quadrant area of the ellipse, i.e.,
The integral is the limit of the Riemann sum as illustrated in Figure [figriemannsum
].
Let us detail the computation. We use the 𝒞1-diffeomorphism change of variable
The differential is dx = acos (θ)dθ and hence,
Important
.. math:
\begin{aligned}
\mathop{\mathrm{area}}(\mathcal{E})
&= 4ab int{0}^{pi/2} cos^2(theta) mathop{mathrm{d}theta} \ &= 4ab int{0}^{pi/2} frac{1 + cos(2theta)}{2} mathop{mathrm{d}theta} \ &= 4ab left[ frac{theta}{2} + frac{sin(2theta)}{4} right]_{0}^{pi/2} \ &= pi a b.
end{aligned}
In this part, we review the computation of the area of an ellipse sector. We complement Eberly
with a bit more details.
The ellipse sector delimited by the polar angles (θ1, θ2) is colored in blue
The elliptic sector area is delimited in polar coordinates by [θ1, θ2] (with θ1 < θ2) as illustrated in Figure [fig-ellsector
]. Using polar coordinates, it equals to the following nonnegative integral
The change of variable in polar coordinates is x = rcos θ and y = rsin θ and, thus with Equation eq-elleq
,
Plugging the formula of r in the integral,
Now the integrand
According to Bioche’s rule, a relevant change of variable is the 𝒞1-diffeomorphism change of variable t = tan (θ) which is valid for ] − π/2, π/2[ → ] − ∞, ∞[. Let us first rewrite
Differentiating t = tan θ,
Hence,
Warning
The integral is properly defined for (θ1, θ2) ∈ ] − π/2, π/2[. But, using symmetry properties of the ellipse, we can easily retrieve the elliptical sector for any (θ1, θ2) ∈ ] − π, π[.
Alternatively, Eberly
provides a more convenient antiderivative because it is defined in ] − π, π] as follows
Hence, the elliptic sector area equals to the following nonnegative quantity
Important
.. math:
\forall (\theta_1, \theta_2) \in ]-\pi, \pi], \ A(\theta_1, \theta_2) =
\left| F(\theta_2) - F(\theta_1) \right|.
The ellipse sector bounded by a line segment and the elliptical arc (θ1, θ2) is colored in blue.
We are interested in computing the elliptic portion by a line segment and the elliptical arc (θ1, θ2) such that
|θ2 − θ1| ≤ π
This condition is important as a such elliptic portion always corresponds to the blue elliptic portion in Figure [figellsector2
]. Let us denote the area of such portion by B(θ1, θ2). Geometrically, we see that, if |θ2 − θ1| ≤ π, then
where (xi, yi) = (ricos θi, risin θi) and
Note that the other portion corresponding to the red one in Figure 3 has an area which equals to πab − B(θ1, θ2) ≥ B(θ1, θ2) if |θ2 − θ1| ≤ π.
To summarize, our portion of interest, illustrated by the blue elliptic portion in Figure 3, has an area which equals to
Important
For any (θ1, θ2) ∈ ] − π, π],
The previous sections has provided the basis for area of intersecting ellipses. However, ellipses are neither centered at the origin nor aligned with the axes of the reference frame in general. Therefore, an ellipse ℰ is entirely defined by the following geometric information
- a center xℰ,
- axis radii (aℰ, bℰ),
- an orientation θℰ, i.e., the oriented angle between the x-axis and the axis of radius aℰ.
or more concisely by the pair (xℰ, Σℰ) where the positive definite matrix Σℰ ∈ 𝒮2 + + is such that
Σℰ = RℰDℰRℰT
where Rℰ is a rotation matrix defined as
and Dℰ is the diagonal matrix defined as
Note that Equation eq-sigma_eps
is the singular value decomposition of Σℰ if the axis radii satisfy aℰ ≥ bℰ. Thus more generally,
Important
The ellipse ℰ is characterized by the equation
(x − xℰ)TΣℰ(x − xℰ) = 1
Or
xTAℰx + bℰTx + cℰ = 0
where Aℰ = Σℰ, bℰ = 2Σℰxℰ and cℰ = xℰTΣℰxℰ − 1. Denoting xT = [x, y], ellipse ℰ can be defined algebraically as
E(x, y) = e1x2 + e2xy + e3y2 + e4x + e5y + e6 = 0,
where $\mathbf{A}_{\mathcal{E}} = \begin{bmatrix} e_1 & e_2/2 \\ e_2/2 & e_3 \end{bmatrix}$, bℰT = [e4, e5] and cℰ = e6. This algebraic form is the convenient one that we will use in order to compute the intersection points of two intersecting ellipses.
We explain how we can retrieve the intersection points of two ellipses. Our presentation complements Eberly
.
First let (ℰi)1 ≤ i ≤ 2 be two ellipses defined as
(x, y) ∈ ℰi ⇔ Ei(x, y) = ei1x2 + ei2xy + ei3y2 + ei4x + ei5y + ei6 = 0
The intersection points of ellipses (ℰi)1 ≤ i ≤ 2 satisfy Equation eq-twoellipses
for i ∈ {1, 2}, i.e., the following equation system holds for intersection points
Now let us rewrite Ei(x, y) as a quadratic polynomial in x, i.e.,
Ei(x, y) = ei1x2 + (ei2y + ei4)x + (ei3y2 + ei5y + ei6) = 0
Conveniently we define auxiliary polynomials in y
Introducing the polynomials above, Equation eq-twoellipses
is rewritten as
Suppose we know the y-coordinate of an intersection point, we can calculate the x-coordinate of this intersection point.
Indeed we multiply the first equation by q2(y) and the second equation by p2(y).
Then subtracting the first equation from the second equation, the monomial x2 disappears. Thus:
Important
Furthermore, Equation eq-system
is equivalent to the following augmented equation system
And we see more clearly in matrix notation that
Important
[1, x, x2, x3]T is in the nullspace of B(y), where B(y) is defined as
We observe that the vector [1, x, x2, x3]T is never zero for any real value x. Thus necessarily the nullspace Null(B(y)) is always nontrivial and that means the determinant of B(y) has to be zero.
Important
Let the polynomial R be defined as
Equation eq-system
is equivalent to the following quartic equation in y.
det (B(y)) = R(y) = 0,
Using any polynomial solver, we get the 4 roots (yi)1 ≤ i ≤ 4 of the quartic polynomial R and only keep those that are real. Finally (xi)1 ≤ i ≤ 4 are deduced from Equation eq:xinter
.
In Sara, we can use several solvers to retrieve the roots of polynomial R.
- Companion matrix approach: since Sara depends on Eigen, Eigen has an unsupported Polynomial solver using this simple approach.
- Jenkins-Traub iterative but very accurate approach also available in Sara.
- Ferrari’s method available in Sara.
The implementation in Sara uses Ferrari's method. While more tedious to implement, the method has the advantage of being direct. Also, we experimentally observe Ferrari’s method can sometimes be numerically inaccurate in particular situations where for example one of the ellipse is quasi-degenerate.
In the future, depending on the use case, we can polish the roots to refine the root values.
Our presentation complements Eberly
. In the rest of the section, we consider two ellipses (ℰi)1 ≤ i ≤ 2 and we respectively denote
-
the axes of ellipse ℰi by (ai, bi), the ellipse center by xi, the orientation by θi, and the direction vectors of axis ai and bi by
$$\begin{aligned} \begin{aligned} \mathbf{u}_i &\overset{\textrm{def}}{=}\begin{bmatrix} \cos(\theta_i) \\ \sin(\theta_i) \end{bmatrix} & \mathbf{v}_i &\overset{\textrm{def}}{=}\begin{bmatrix} -\sin(\theta_i) \\ \cos(\theta_i) \end{bmatrix}\end{aligned} \end{aligned}$$ - the area of the elliptic portion bounded a line segment and an arc for ellipse ℰi by Bi,
- the number of intersection points by L,
-
the intersection points by pi for i ∈ ⟦1, L⟧, sorted in a counter-clockwise order, i.e.,
∀i ∈ ⟦1, L − 1⟧, ∠([1,0]T,pi) < ∠([1,0]T,pi + 1)where ∠(., .) denotes the angle between two vectors in the plane ℝ2.
-
the polar angles of points (pi)1 ≤ i ≤ L with respect to ellipses ℰ1 and ℰ2 by (ϕi)1 ≤ i ≤ 2 and (ψi)1 ≤ i ≤ 2, i.e.,
$$\begin{aligned} \begin{gathered} \forall i \in \llbracket 1, L\rrbracket, \phi_i \overset{\textrm{def}}{=}\angle\left(\mathbf{u}_1, \mathbf{p}_i - \mathbf{x}_1\right) \\ \forall i \in \llbracket 1, L\rrbracket, \psi_i \overset{\textrm{def}}{=}\angle\left(\mathbf{u}_2, \mathbf{p}_i - \mathbf{x}_2\right)\end{gathered} \end{aligned}$$
To retrieve the polar angles, we need to place ourselves in the coordinate system (xi, ui, vi). Using the convenient function atan2 giving values in ] − π, π], we have
Either one ellipse is contained in the other or there are separated as illustrated in Figure [figinter01
].
An ellipse, say ℰ1, is contained in the other ℰ2 if and only if its center satisfies E2(x1) < 0. In that case, the area of the intersection is just the area of ellipse ℰ1. Otherwise, if there is no containment, the intersection area is zero. In summary,
We will not detail the case when Polynomial eq-detBy
have 2 roots with multiplicity 2. This still corresponds to the case where there are two intersection points. But because of the root multiplicities, one ellipse is contained in the other one and then Equation eq:eq-area01 gives the correct intersection area.
Otherwise, we have to consider two cases as illustrated in Figure [figinter2
], which Eberly
apparently forgot to consider. Namely, the cases correspond to whether the center of ellipses ℰ1 and ℰ2 are on the same side or on opposite side with respect to the line (p1, p2).
Denoting a unit normal of the line going across the intersection points (p1, p2) by n (cf. Figure 1.9). If the ellipse centers x1 and x2 are on opposite side with respect to the line (p1, p2), i.e.,
⟨n, x1 − p1⟩⟨n, x2 − p1⟩ < 0,
then
area (ℰ1 ∩ ℰ2) = B1(ϕ1, ϕ2) + B2(ψ1, ψ2)
If they are on the same side with respect to the line (p1, p2), i.e.,
⟨n, x1 − p1⟩⟨n, x2 − p1⟩ > 0,
then
Note that the condition |⟨n, x1 − p1⟩| ≤ |⟨n, x2 − p1⟩| in Equation eqinter2b
just expresses the fact that the distance of ellipse center x1 to the line (p1, p2) is smaller than the distance of ellipse center x2 to the line (p1, p2).
These cases are rather easy to handle. Indeed, we see geometrically from Figure [fig-inter34
],
with ϕL + 1 = ϕ1, ψL + 1 = ψ1 and pL + 1 = p1.