Ellipse Intersections¶
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.
Origin-Centered Axis-Aligned Ellipses¶
Let \(\mathcal{E}\) be a ellipse with semi-major axis \(a\) and semi-minor axis \(b\), i.e., \(\, a \geq b > 0\). Let us first suppose that \(\mathcal{E}\) 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,
Ellipse Area¶
Using the symmetry in the ellipse, the area of ellipse \(\mathcal{E}\) 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 [Riemann sum approximating the upper quadrant area of the ellipse.].
Let us detail the computation. We use the \(\mathcal{C}^1\)-diffeomorphism change of variable \(\frac{x}{a} = \sin \theta\) which is valid for \([0, a] \rightarrow [0, \pi/2]\). We recall that a \(\mathcal{C}^1\)-diffeomorphism is an invertible differentiable function with continuous derivative.
The differential is \(\mathop{\mathrm{d}x} = a \cos(\theta) \mathop{\mathrm{d}\theta}\) and hence,
Important
Area of an Elliptical Sector¶
In this part, we review the computation of the area of an ellipse sector. We complement [Eberly] with a bit more details.
The elliptic sector area is delimited in polar coordinates by \([\theta_1, \theta_2]\) (with \(\theta_1 < \theta_2\)) as illustrated in Figure [The ellipse sector delimited by the polar angles (\theta_1, \theta_2) is colored in blue]. Using polar coordinates, it equals to the following nonnegative integral
The change of variable in polar coordinates is \(x = r \cos\theta\) and \(y = r \sin\theta\) and, thus with Equation (1), \(\displaystyle\frac{r^2 \cos^2(\theta)}{a^2} + \frac{r^2 \sin^2(\theta)}{b^2} = 1\), therefore
Plugging the formula of \(r\) in the integral,
Now the integrand \(\frac{\mathop{\mathrm{d}\theta}}{b^2 \cos^2(\theta) + a^2 \sin^2(\theta)}\) is invariant by the transformation \(\theta \mapsto \theta+\pi\), i.e.,
According to Bioche’s rule, a relevant change of variable is the \(\mathcal{C}^1\)-diffeomorphism change of variable \(t = \tan(\theta)\) which is valid for \(]-\pi/2, \pi/2[ \rightarrow ]-\infty, \infty[\). Let us first rewrite
Differentiating \(t=\tan\theta\), \(\mathop{\mathrm{d}t} = \frac{\mathop{\mathrm{d}\theta}}{\cos^2(\theta)}\), thus
Hence,
Warning
The integral is properly defined for \((\theta_1, \theta_2) \in ]-\pi/2, \pi/2[\). But, using symmetry properties of the ellipse, we can easily retrieve the elliptical sector for any \((\theta_1, \theta_2) \in ]-\pi, \pi[\).
Alternatively, [Eberly] provides a more convenient antiderivative because it is defined in \(]-\pi, \pi]\) as follows
Hence, the elliptic sector area equals to the following nonnegative quantity
Important
Area Bounded by a Line Segment and an Elliptical Arc¶
We are interested in computing the elliptic portion by a line segment and the elliptical arc \((\theta_1, \theta_2)\) such that
This condition is important as a such elliptic portion always corresponds to the blue elliptic portion in Figure [The ellipse sector bounded by a line segment and the elliptical arc (\theta_1, \theta_2) is colored in blue.]. Let us denote the area of such portion by \(B(\theta_1, \theta_2)\). Geometrically, we see that, if \(|\theta_2 - \theta_1| \leq \pi\), then
where \((x_i,y_i) = (r_i\cos\theta_i, r_i\sin\theta_i)\) and \(\displaystyle r_i = \frac{ab}{\sqrt{b^2 \cos^2(\theta_i)+a^2 \sin^2(\theta_i)}}\) for \(i \in \{1,2\}\).
Note that the other portion corresponding to the red one in Figure 3 has an area which equals to \(\pi a b - B(\theta_1, \theta_2) \geq B(\theta_1, \theta_2)\) if \(|\theta_2 - \theta_1| \leq \pi\).
To summarize, our portion of interest, illustrated by the blue elliptic portion in Figure 3, has an area which equals to
Important
For any \((\theta_1, \theta_2) \in ]-\pi, \pi]\),
General Ellipse Parameterization¶
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 \(\mathcal{E}\) is entirely defined by the following geometric information
- a center \(\mathbf{x}_{\mathcal{E}}\),
- axis radii \((a_{\mathcal{E}}, b_{\mathcal{E}})\),
- an orientation \(\theta_{\mathcal{E}}\), i.e., the oriented angle between the \(x\)-axis and the axis of radius \(a_{\mathcal{E}}\).
or more concisely by the pair \((\mathbf{x}_{\mathcal{E}}, \mathbf{\Sigma}_{\mathcal{E}})\) where the positive definite matrix \(\mathbf{\Sigma}_{\mathcal{E}} \in \mathcal{S}^{++}_2\) is such that
where \(\mathbf{R}_{\mathcal{E}}\) is a rotation matrix defined as
and \(\mathbf{D}_{\mathcal{E}}\) is the diagonal matrix defined as
Note that Equation (2) is the singular value decomposition of \(\mathbf{\Sigma}_{\mathcal{E}}\) if the axis radii satisfy \(a_{\mathcal{E}} \geq b_{\mathcal{E}}\). Thus more generally,
Important
The ellipse \(\mathcal{E}\) is characterized by the equation
Or
where \(\mathbf{A}_{\mathcal{E}} = \mathbf{\Sigma}_{\mathcal{E}}\), \(\mathbf{b}_{\mathcal{E}} = 2 \mathbf{\Sigma}_{\mathcal{E}} \mathbf{x}_{\mathcal{E}}\) and \(c_{\mathcal{E}} = \mathbf{x}_{\mathcal{E}}^T \mathbf{\Sigma}_{\mathcal{E}} \mathbf{x}_{\mathcal{E}} - 1\). Denoting \(\mathbf{x}^T = [x, y]\), ellipse \(\mathcal{E}\) can be defined algebraically as
where \(\mathbf{A}_{\mathcal{E}} = \begin{bmatrix} e_1 & e_2/2 \\ e_2/2 & e_3 \end{bmatrix}\), \(\mathbf{b}_{\mathcal{E}}^T = [e_4, e_5]\) and \(c_{\mathcal{E}} = e_6\). This algebraic form is the convenient one that we will use in order to compute the intersection points of two intersecting ellipses.
Intersection Points of Two Ellipses¶
We explain how we can retrieve the intersection points of two ellipses. Our presentation complements [Eberly].
First let \((\mathcal{E}_i)_{1 \leq i \leq 2}\) be two ellipses defined as
The intersection points of ellipses \((\mathcal{E}_i)_{1 \leq i \leq 2}\) satisfy Equation (3) for \(i \in \{1, 2\}\), i.e., the following equation system holds for intersection points
Now let us rewrite \(E_i(x,y)\) as a quadratic polynomial in \(x\), i.e.,
Conveniently we define auxiliary polynomials in \(y\)
Introducing the polynomials above, Equation (3) 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 \(q_2(y)\) and the second equation by \(p_2(y)\).
Then subtracting the first equation from the second equation, the monomial \(x^2\) disappears. Thus:
Important
Furthermore, Equation (4) is equivalent to the following augmented equation system
And we see more clearly in matrix notation that
Important
\([1, x, x^2, x^3]^T\) is in the nullspace of \(\mathbf{B}(y)\), where \(\mathbf{B}(y)\) is defined as
We observe that the vector \([1, x, x^2, x^3]^T\) is never zero for any real value \(x\). Thus necessarily the nullspace \(\text{Null}(\mathbf{B}(y))\) is always nontrivial and that means the determinant of \(\mathbf{B}(y)\) has to be zero.
Important
Let the polynomial \(R\) be defined as
Equation (4) is equivalent to the following quartic equation in \(y\).
Using any polynomial solver, we get the \(4\) roots \((y_i)_{1\leq i\leq 4}\) of the quartic polynomial \(R\) and only keep those that are real. Finally \((x_i)_{1\leq i \leq 4}\) are deduced from Equation (5).
Implementation Notes¶
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.
Intersection Area of Two Ellipses¶
Our presentation complements [Eberly]. In the rest of the section, we consider two ellipses \((\mathcal{E}_i)_{1 \leq i \leq 2}\) and we respectively denote
the axes of ellipse \(\mathcal{E}_i\) by \((a_i, b_i)\), the ellipse center by \(\mathbf{x}_i\), the orientation by \(\theta_i\), and the direction vectors of axis \(a_i\) and \(b_i\) by
\[\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}\]the area of the elliptic portion bounded a line segment and an arc for ellipse \(\mathcal{E}_i\) by \(B_i\),
the number of intersection points by \(L\),
the intersection points by \(\mathbf{p}_i\) for \(i \in \llbracket 1, L \rrbracket\), sorted in a counter-clockwise order, i.e.,
(8)¶\[\forall i \in \llbracket 1, L-1\rrbracket,\quad \angle\left([1,0]^T, \mathbf{p}_i\right) \ < \ \angle\left([1,0]^T, \mathbf{p}_{i+1}\right)\]where \(\angle(.,.)\) denotes the angle between two vectors in the plane \(\mathbb{R}^2\).
the polar angles of points \((\mathbf{p}_i)_{1\leq i \leq L}\) with respect to ellipses \(\mathcal{E}_1\) and \(\mathcal{E}_2\) by \((\phi_i)_{1\leq i \leq 2}\) and \((\psi_i)_{1\leq i \leq 2}\), i.e.,
\[\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}\]
Retrieving the polar angles¶
To retrieve the polar angles, we need to place ourselves in the coordinate system \((\mathbf{x}_i, \mathbf{u}_i, \mathbf{v}_i)\). Using the convenient function \(\mathrm{atan2}\) giving values in \(]-\pi,\pi]\), we have
0 or 1 intersection point¶
Either one ellipse is contained in the other or there are separated as illustrated in Figure [Cases where there is zero or one intersection point.].
An ellipse, say \(\mathcal{E}_1\), is contained in the other \(\mathcal{E}_2\) if and only if its center satisfies \(E_2(\mathbf{x}_1) < 0\). In that case, the area of the intersection is just the area of ellipse \(\mathcal{E}_1\). Otherwise, if there is no containment, the intersection area is zero. In summary,
2 intersection points¶
We will not detail the case when Polynomial (7) 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 [Cases where there are two intersection points.], which [Eberly] apparently forgot to consider. Namely, the cases correspond to whether the center of ellipses \(\mathcal{E}_1\) and \(\mathcal{E}_2\) are on the same side or on opposite side with respect to the line \((\mathbf{p}_1, \mathbf{p}_2)\).
Denoting a unit normal of the line going across the intersection points \((\mathbf{p}_1, \mathbf{p}_2)\) by \(\mathbf{n}\) (cf. Figure 1.9). If the ellipse centers \(\mathbf{x}_1\) and \(\mathbf{x}_2\) are on opposite side with respect to the line \((\mathbf{p}_1, \mathbf{p}_2)\), i.e.,
then
If they are on the same side with respect to the line \((\mathbf{p}_1, \mathbf{p}_2)\), i.e.,
then
Note that the condition \(|\langle\mathbf{n},\mathbf{x}_1-\mathbf{p}_1\rangle| \leq |\langle\mathbf{n},\mathbf{x}_2-\mathbf{p}_1\rangle|\) in Equation (10) just expresses the fact that the distance of ellipse center \(\mathbf{x}_1\) to the line \((\mathbf{p}_1, \mathbf{p}_2)\) is smaller than the distance of ellipse center \(\mathbf{x}_2\) to the line \((\mathbf{p}_1, \mathbf{p}_2)\).
3 and 4 intersection points¶
These cases are rather easy to handle. Indeed, we see geometrically from Figure [Cases where there are three of four intersection points.],
with \(\phi_{L+1} = \phi_1\), \(\psi_{L+1} = \psi_1\) and \(\mathbf{p}_{L+1} = \mathbf{p}_1\).