Move the sliders. Each time the curve on the right crosses the dashed line, a point appears on the left.

The ellipse

An ellipse with centre (h,k)(h, k), semi-axes aa and bb, turned by an angle θ\theta, is the set of points (x,y)(x, y) where

u=(x−h)cos⁡ θ+(y−k)sin⁡ θu = (x-h)\cos\,\theta + (y-k)\sin\,\thetav=−(x−h)sin⁡ θ+(y−k)cos⁡ θv = -(x-h)\sin\,\theta + (y-k)\cos\,\thetau2a2+v2b2=1\frac{u^2}{a^2} + \frac{v^2}{b^2} = 1

uu and vv are the coordinates of the point in the ellipse’s own axes.

From two unknowns to one

A point on both ellipses satisfies both equations. Instead of solving for xx and yy together, write any point of the first ellipse with one angle tt:

(xy)=(h1k1)+R(θ1)(a1cos⁡ tb1sin⁡ t)\begin{pmatrix} x \\ y \end{pmatrix} = \begin{pmatrix} h_1 \\ k_1 \end{pmatrix} + R(\theta_1) \begin{pmatrix} a_1 \cos\, t \\ b_1 \sin\, t \end{pmatrix}

where R(θ)R(\theta) is the rotation by θ\theta. Put this point into the second ellipse’s equation:

g(t)=u22a22+v22b22−1=0g(t) = \frac{u_2^2}{a_2^2} + \frac{v_2^2}{b_2^2} - 1 = 0

gg is negative inside the second ellipse and positive outside. Expanding it gives

g(t)=c0+c1cos⁡ t+s1sin⁡ t+c2cos⁡ 2t+s2sin⁡ 2t\begin{aligned} g(t) = {} & c_0 + c_1 \cos\, t + s_1 \sin\, t \\ & + c_2 \cos\, 2t + s_2 \sin\, 2t \end{aligned}

The coefficients come from three vectors. q0q_0 is the centre of the first ellipse in the second ellipse’s axes: u2u_2 and v2v_2 at (h1,k1)(h_1, k_1), divided by a2a_2 and b2b_2. With δ=θ1−θ2\delta = \theta_1 - \theta_2:

qc=(a1cos⁡ δ/a2a1sin⁡ δ/b2)q_c = \begin{pmatrix} a_1 \cos\,\delta / a_2 \\ a_1 \sin\,\delta / b_2 \end{pmatrix}qs=(−b1sin⁡ δ/a2b1cos⁡ δ/b2)q_s = \begin{pmatrix} -b_1 \sin\,\delta / a_2 \\ b_1 \cos\,\delta / b_2 \end{pmatrix}
  • c0=∣q0∣2+(∣qc∣2+∣qs∣2)/2−1c_0 = |q_0|^2 + (|q_c|^2 + |q_s|^2)/2 - 1
  • c1=2 q0⋅qcc_1 = 2\, q_0 \cdot q_c and s1=2 q0⋅qss_1 = 2\, q_0 \cdot q_s
  • c2=(∣qc∣2−∣qs∣2)/2c_2 = (|q_c|^2 - |q_s|^2)/2 and s2=qc⋅qss_2 = q_c \cdot q_s

A polynomial of degree 4

Write w=tan⁡(t/2)w = \tan(t/2). Then cos⁡ t=(1−w2)/(1+w2)\cos\, t = (1 - w^2)/(1 + w^2) and sin⁡ t=2w/(1+w2)\sin\, t = 2w/(1 + w^2), and multiplying gg by (1+w2)2(1 + w^2)^2 gives

Aw4+Bw3+Cw2+Dw+E=0A w^4 + B w^3 + C w^2 + D w + E = 0
  • A=c0−c1+c2A = c_0 - c_1 + c_2
  • B=2s1−4s2B = 2 s_1 - 4 s_2
  • C=2c0−6c2C = 2 c_0 - 6 c_2
  • D=2s1+4s2D = 2 s_1 + 4 s_2
  • E=c0+c1+c2E = c_0 + c_1 + c_2

A polynomial of degree 4 has at most four real roots, so two ellipses cross in at most four points. ww can’t reach t=πt = \pi, so if that point is an intersection, A=0A = 0. To avoid this, choose where tt starts so that the point at t=πt = \pi is clearly inside or outside the second ellipse.

Solving the quartic

Divide by AA to get w4+p3w3+p2w2+p1w+p0=0w^4 + p_3 w^3 + p_2 w^2 + p_1 w + p_0 = 0. Ferrari’s idea is to split it into two quadratics:

(w2+α1w+β1)(w2+α2w+β2)=0(w^2 + \alpha_1 w + \beta_1)(w^2 + \alpha_2 w + \beta_2) = 0

Matching the coefficients gives α1+α2=p3\alpha_1 + \alpha_2 = p_3, β1+β2+α1α2=p2\beta_1 + \beta_2 + \alpha_1 \alpha_2 = p_2, α1β2+α2β1=p1\alpha_1 \beta_2 + \alpha_2 \beta_1 = p_1 and β1β2=p0\beta_1 \beta_2 = p_0. With m=β1+β2m = \beta_1 + \beta_2, these reduce to a cubic:

m3−p2m2+(p3p1−4p0) m+4p2p0−p32p0−p12=0\begin{aligned} m^3 & - p_2 m^2 + (p_3 p_1 - 4 p_0)\, m \\ & + 4 p_2 p_0 - p_3^2 p_0 - p_1^2 = 0 \end{aligned}

Cardano’s formula solves it. With m=z+p2/3m = z + p_2/3 it becomes z3+Pz+Q=0z^3 + P z + Q = 0, where

P=p3p1−4p0−p223P = p_3 p_1 - 4 p_0 - \frac{p_2^2}{3}Q=4p2p0−p32p0−p12+p2(p3p1−4p0)3−2p2327\begin{aligned} Q = {} & 4 p_2 p_0 - p_3^2 p_0 - p_1^2 \\ & + \frac{p_2 (p_3 p_1 - 4 p_0)}{3} - \frac{2 p_2^3}{27} \end{aligned}

If Δ=Q2/4+P3/27>0\Delta = Q^2/4 + P^3/27 > 0,

z=−Q2+Δ3+−Q2−Δ3z = \sqrt[3]{-\frac{Q}{2} + \sqrt{\Delta}} + \sqrt[3]{-\frac{Q}{2} - \sqrt{\Delta}}

and if Δ≤0\Delta \le 0, the three real roots are

zk=2−P3cos⁡(φ−2πk3)z_k = 2 \sqrt{-\frac{P}{3}} \cos\left(\frac{\varphi - 2\pi k}{3}\right)cos⁡ φ=−Q2(−3P)3/2\cos\,\varphi = -\frac{Q}{2} \left(-\frac{3}{P}\right)^{3/2}

Any real mm gives the two quadratics:

α1,2=p32±p324−p2+m\alpha_{1,2} = \frac{p_3}{2} \pm \sqrt{\frac{p_3^2}{4} - p_2 + m}β1,2=m2±m24−p0\beta_{1,2} = \frac{m}{2} \pm \sqrt{\frac{m^2}{4} - p_0}

with the signs paired so that α1β2+α2β1=p1\alpha_1 \beta_2 + \alpha_2 \beta_1 = p_1. Each quadratic gives two roots ww. Every real one is an intersection: t=2arctan⁡ wt = 2 \arctan\, w, and the first equation of this page gives the point (x,y)(x, y).

Two details matter on a computer. When one of the two square roots is close to zero, rounding makes it inaccurate, so I take only the other one and get the remaining pair from α1β2+α2β1=p1\alpha_1 \beta_2 + \alpha_2 \beta_1 = p_1. And when the cubic has three real roots, I keep the one that satisfies the four matching equations best.

A double root is a point where the ellipses touch without crossing. If c1c_1, s1s_1, c2c_2 and s2s_2 are all zero, gg doesn’t depend on tt: it’s the same ellipse twice if c0=0c_0 = 0, and no common point otherwise.

Example

For “Four points” in the demo, the first ellipse has centre (0,0)(0, 0), a=3a = 3, b=1.2b = 1.2, θ=20°\theta = 20°, and the second has centre (0.5,0.3)(0.5, 0.3), a=2.6a = 2.6, b=1b = 1, θ=110°\theta = 110°. The quartic is

11.764 w4−0.079 w3−18.489 w2−0.079 w+4.895=0\begin{aligned} 11.764\, w^4 & - 0.079\, w^3 - 18.489\, w^2 \\ & - 0.079\, w + 4.895 = 0 \end{aligned}

The root m=−1.290m = {-1.290} of the cubic splits it into

(w2+0.527 w−0.635)×(w2−0.534 w−0.656)=0\begin{aligned} & (w^2 + 0.527\, w - 0.635) \\ & \times (w^2 - 0.534\, w - 0.656) = 0 \end{aligned}

The four roots are w=0.576w = 0.576, 1.1191.119, −1.103-1.103 and −0.586-0.586, so t=59.85°t = 59.85°, 96.45°96.45°, 264.41°264.41° and 299.29°299.29°. They give the four points (1.061,1.490)(1.061, 1.490), (−0.724,1.005)({-0.724}, 1.005), (0.134,−1.222)(0.134, {-1.222}) and (1.737,−0.481)(1.737, {-0.481}).

I checked the solver against a separate method with much higher precision, on thousands of cases, including ellipses that touch and very thin ones. Both always gave the same points.