Find the intersection of a circle and an ellipse

analytic geometrygeometrypolynomials

I was hoping to write some code to find the intersection of a circle and an ellipse but it's turned out to be more difficult than I expected. Both have shifted centres and are not rotated. I also do not know the polynomial representation of the ellipse, so my question does not match others posted on this site so far, which present the problem as solving a polynomial in two variables ($x$ and $y$).

The Cartesian equations are:

  • a circle: $(x – X_c)^2 + (y – Y_c)^2 = r^2$
  • an ellipse: $\frac{(x – x_c)^2}{a^2} + \frac{(y – y_c)^2}{b^2} = 1$

The parametric equations ($t \in [0, 2\pi]$) are:

  • a circle: $x = X_c + r\frac{1-t^2}{1+t^2}$; $y = Y_c + r\frac{2t}{1+t^2}$
  • an ellipse: $x = x_c + a \cdot cos(t)$; $y = y_c + b \cdot sin(t)$

[N.B. I've tried to distinguish the centre of the circle $(X_c, Y_c)$ and the centre of the ellipse $(x_c, y_c)$]

I've seen it suggested that substituting $(x,y)$ from the parametric circle definitions into the Cartesian equation for the ellipse will give a polynomial of degree 4 in terms of $t$ whose real solutions (polynomial roots) give the coordinates of the point(s) of intersection:

$\frac{1}{a^2}(X_c – x_c + r\frac{1-t^2}{1+t^2})^2 + \frac{1}{b^2}(Y_c – y_c + r\frac{2t}{1+t^2})^2$

… however I've been unable to figure out what algebraic rearrangement is needed to get these solutions.

I have also had no hints from trying to provide the formula to rearranging programs like Wolfram Alpha, and cannot see how to rearrange into the form $At^4 + Bt^3 + Ct^2 + Dt + E = 0$ so as to use a polynomial solver in a package such as numpy.

Below is the plot I made so far to view the intersecting shapes in question, and in case it is of help to anyone to check any suggested approaches, the values used to produce this diagram are:

$(X_c, Y_c) = (550, 1000)$; $r = 225$; $(x_c, y_c) = (1000, 1000)$; $a = 675$; $b = 275$.

Plugging in these values, I can reduce the substituted equation to the following, which Wolfram Alpha cannot find any simpler rearrangement of:

$\frac{(3t^2 + 1)^2}{9(t^2 + 1)^2} + \frac{324t^2}{121(t^2 + 1)} – \frac{1}{275}$

sketch of ellipse and circle intersection

The intersection in question is between the lime green circle and the orange [inner] ellipse. From inspecting this plot, the intersection (x,y) coords are (592, 780) in the top left quadrant of the ellipse, and (592, 1220) in the bottom left quadrant of the ellipse.

The yellow arrow points to the midpoint of the major axis radius, which is the centre of the lime green circle. The orange ellipse represents the locus of all possible centre points of circles 'rolled around the inside' of the outer [black] ellipse.

I don't know how to proceed solving this as a pair of simultaneous equations, as it doesn't seem to be in a form amenable to factoring, nor solving as a polynomial (e.g. no clear coefficients for the fractions with terms in $t^4$ on both numerator and denominator).

I've checked the coordinates of $(592, 780)$ and $(1220, 780)$ with the parametric equations of the [lime green] circle, which give:

  • $t = cos^{-1}(\frac{x – x_c}{r})$$t = cos^{-1}(\frac{592 – 1000}{225})$ = $4.9235998607438685$
  • $t = cos^{-1}(\frac{x – x_c}{r})$$t = cos^{-1}(\frac{1220 – 1000}{225})$ = $1.3595854464357182$

and plotted this on the circumference of the circle accordingly (blue and red dot, respectively).

Best Answer

Since the circumference is the place of the points distant $R > 0$ from a point $\left(x_c, \, y_c\right)$, its cartesian equation is:

$$ \left(x - x_c\right)^2 + \left(y - y_c\right)^2 = R^2 $$

whose rational parameterization is $\left(x, \, y\right) = \left(x_c + R\,\frac{1-z^2}{1+z^2}, \, y_c + R\,\frac{2\,z}{1+z^2}\right)$, with $z \in \mathbb{R}$.

At this point, an ellipse assigned in canonical form:

$$\frac{\left(x - x_c'\right)^2}{a^2} + \frac{\left(y - y_c'\right)^2}{b^2} = 1$$

with $a, \, b > 0$, substituting for $x, \, y$ the respective parametric equations of the circumference is obtained:

$$ \frac{\left(x_c + R\,\frac{1-z^2}{1+z^2} - x_c'\right)^2}{a^2} + \frac{\left(y_c + R\,\frac{2\,z}{1+z^2} - y_c'\right)^2}{b^2} = 1 $$

ie: $$ \small \left(x_c - x_c' + R\,\frac{1-z^2}{1+z^2}\right)^2\,b^2\left(1+z^2\right)^2 + \left(y_c - y_c' + R\,\frac{2\,z}{1+z^2}\right)^2\,a^2\left(1+z^2\right)^2 = a^2\,b^2\left(1+z^2\right)^2 $$

from which:

$$ \small b^2\left(x_c - x_c'\right)^2\left(1 + z^2\right)^2 + 2\,b^2\,R\left(x_c - x_c'\right)\left(1 - z^4\right) + b^2\,R^2\left(1 - z^2\right)^2 + \\ \small a^2\left(y_c - y_c'\right)^2\left(1 + z^2\right)^2 + 2\,a^2\,R\left(y_c - y_c'\right)\left(2\,z\right)\left(1 + z^2\right) + a^2\,R^2\left(2\,z\right)^2 = a^2\,b^2\left(1+z^2\right)^2 $$

which simplified is equivalent to the equation:

$$ a_4\,z^4 + a_3\,z^3 + a_2\,z^2 + a_1\,z + a_0 = 0 $$

with:

$$ \begin{aligned} & a_4 = a^2\left(\left(y_c - y_c'\right)^2 - b^2\right) + b^2\left(\left(x_c - x_c'\right) - R\right)^2 \\ & a_3 = 4\,a^2\,R\left(y_c - y_c'\right) \\ & a_2 = 2\left(a^2\left(\left(y_c - y_c'\right)^2 - b^2 + 2\,R^2\right) + b^2\left(\left(x_c - x_c'\right)^2 - R^2\right)\right) \\ & a_1 = 4\,a^2\,R\left(y_c - y_c'\right) \\ & a_0 = a^2\left(\left(y_c - y_c'\right)^2 - b^2\right) + b^2\left(\left(x_c - x_c'\right) + R\right)^2 \end{aligned} $$

All that remains is to solve this equation and replace the real solutions in the parametric equations of the circumference to obtain the coordinates of the desired points of intersection.

Furthermore, in the event of:

$$ \Delta := \left(\left(x_c - x_c'\right) - (R - a)\right)\left((R + a) - \left(x_c - x_c'\right)\right) \ge 0 $$

and:

$$ a\left(y_c - y_c'\right) - b\,\sqrt{\Delta} = 0 \; \; \; \vee \; \; \; a\left(y_c - y_c'\right) + b\,\sqrt{\Delta} = 0 $$

then the circumference and the ellipse intersect in the singular point $\left(x_c - R, \; y_c\right)$.

This algorithm can be implemented relatively easily in Microsoft Excel as follows:

enter image description here

referring, for example, to what is reported in the following excellent books:

  • Handbook of Mathematical Functions with Formulas, Graphs and Mathematical Tables - Abramowitz, Stegun - USA
  • Handbook of Mathematical Functions - Frank W. J. Olver - NIST