I need to scale an ellipse to be tangent to another ellipse without moving them from their centers $(h_1,k_1)$ $(h_2,k_2)$ or modifying their a/b ratio.
$$\frac{(x-h)^2}{(a*s)^2} + \frac{(y-k)^2}{(b*s)^2} = 1$$
I'm looking for a fast analytical geometry solution to this problem, I understand that we could use numerical approach to solve this problem (e.g. Newtons Method)
If the a/b ratio of both ellipses were the same we could simply take advantage of the distance formula and radius at angle formula for ellipse to calculate a scale modifier and it would work in any situation and angle(demo of a working example):
$$ \frac{a \cdot b}{\sqrt{a^2 \sin^2 \theta + b^2 \cos^2 \theta}} $$
I believe this could be done by finding the two non-vertical common tangent points (NOT tangent lines) of the two ellipses. However, I couldn’t find a working solution anywhere. Since I’m using the solution in software, I need to be able to solve any possible equations with the quadratic formula.
In this demo, the scale of one of the ellipses is calculated by approximately finding the common tangent point on the first ellipse using the following formula:
$$ \begin{aligned} \theta &= \tan^{-1} \left( -\frac{b}{a} \cdot m \right) \\ x_1 &= h + a \cos \theta \\ y_1 &= k + b \sin \theta \\ x_2 &= h - a \cos \theta \\ y_2 &= k - b \sin \theta \\ \end{aligned} $$
Please let me know if you have any other questions or concerns.
If $r = \begin{bmatrix} x \\ y \end{bmatrix}$,
$C_1 = \begin{bmatrix} h_1 \\ k_1 \end{bmatrix} $
$ C_2 =\begin{bmatrix} h_2 \\ k_2 \end{bmatrix} $
$ Q_1 = \begin{bmatrix} \dfrac{1}{a_1^2} && 0 \\ 0 && \dfrac{1}{b_1^2} \end{bmatrix} $
$ Q_2 = \begin{bmatrix} \dfrac{1}{a_2^2} && 0 \\ 0 && \dfrac{1}{b_2^2} \end{bmatrix} $
Then the first ellipse is given by
$ (r - C_1)^T Q_1 (r - C_1) = 1 \tag{1} $
And the scaled second ellipse is given by
$ (r - C_2)^T Q_2 (r - C_2) = s^2 \tag{2}$
The gradient (normal vector) of the first ellipse is
$ \nabla_1 = 2 Q_1 (r - C_1 ) $
And the gradient of the second scaled ellipse is
$ \nabla_2 = 2 Q_2 (r - C_2 ) $
And we want
$ \nabla_1 = k \ \nabla_2 $
Define the matrix
$E = e_1 e_2^T - e_2 e_1^T = \begin{bmatrix} 0 && 1 \\ -1 && 0 \end{bmatrix} $
Then we require that
$ (r - C_1)^T Q_1^T E Q_2 (r - C_2) = 0 \tag{3}$
Equations $(1)$ and $(3)$ are a system of two quadratic equations in two unknowns, which are the $x$ and $y$ coordinates of the $r$, The tangency point that lies on the first (fixed) ellipse.
To solve them analytically, express the solutions of $(1)$ in parametric form, then you'll have
$ r = (x,y) = (h_1 + a_1 \cos t , k_1 + b_1 \sin t ) , \ t \in [0, 2 \pi) \tag{4}$
On the other hand, equation $(3)$ when expanded, we give
$ H x y + A x + B y + C = 0 \tag{5} $
where
$ H = \dfrac{1}{(a_1 b_2)^2} - \dfrac{1}{(a_2 b_1)^2} $
$ A = \dfrac{k_1}{(a_2 b_1)^2} - \dfrac{k_2}{(a_1 b_2)^2} $
$ B = \dfrac{h_2}{(a_2 b_1)^2} - \dfrac{h_1}{(a_1 b_2)^2} $
$ C = \dfrac{h_1 k_2}{(a_1 b_2)^2} - \dfrac{h_2 k_1}{(a_2 b_1)^2} $
Substituting $(4)$ into $(5)$ give us
$ H ( h_1 + a_1 \cos t )(k_1 + b_1 \sin t) + A (h_1 + a_1 \cos t) + B ( k_1 + b_1 \sin t ) + C = 0 $
Multiplying this we get the equation
$ A_1 \cos t + A_2 \sin t + A_3 \cos t \sin t + A_4 = 0 \tag{6}$
where
$ A_1 = A a_1 + H a_1 k_1 $
$ A_2 = B b_1 + H b_1 h_1 $
$ A_3 = H a_1 b_1 $
$ A_4 = H h_1 k_1 + A h_1 + B k_1 + C $
To solve equation $(6)$, we use the transformation
$ z = \tan \left( \dfrac{t}{2} \right) $
Then it would follow that
$ \cos t = \dfrac{1 - z^2}{1 + z^2} $ and $ \sin t =\dfrac{2 z }{1 + z^2} $
Plug these into $(6)$ and multiply through by $(1 + z^2)^2$. This gives you,
$ A_1 (1 - z^2) (1 + z^2) + A_2 (2 z)(1 + z^2) + A_3 (2 z)(1 - z^2) + A_4 (1 + z^2)^2 = 0 \tag{7}$
Expanding $(7)$ gives
$ a_4 z^4 + a_3 z^3 + a_2 z^2 + a_1 z + a_0 = 0 \tag{8} $
where
$a_4 = - A_1 + A_4 $
$a_3 = 2 A_2 - 2 A_3 $
$a_2 = 2 A_4 $
$a_1 = 2 A_2 + 2 A_3 $
$a_0 = A_1 + A_4 $
Feeding this quartic polynomial (quartic means of degree 4) to a library routine that finds polynomial roots generates all the $4$ roots, out of which, we're only interested in the real roots. There should be exactly two real roots. You can write your own quartic polynomial solver. See here for details. I've included my routine that I use to find the roots of a quartic polynomial below.
Once a root $z$ are found, the corresponding $t$ is calculated using $t = 2 \tan^{-1} z$, and from there, we can calculate $r$ from equation $(4)$. Once $r$ is found, the value of $s$ can be calculated from equation $(2)$.
The above analysis applies only to the case where the ellipses have their axes parallel to the coordinate axes. But the quadratic equations $(1), (2), (3)$ apply to any two ellipses in general orientation. In this case, we can still apply the same procedure, but the equations are far more complex. However, the two equations $(1)$ and $(3)$ can be solved directly for $r$ by a general two-variable quadratic equation solver.
Numerical Example:
Let $C_1 = (10, 0) $, $C_2 = (0, 6) $, $a_1 = 3 , b_1 = 2 $, $a_2 = 2, b_2 = 1 $. Feeding the above quadratic system to a solver generates the following solutions. The two values of $s$ are
$s_1 = 6.01335710947491 $
and
$ s_2 = 9.63595852170716 $
$s_1$ corresponds to external tangency, and $s_2$ corresponds to internal tangency.
Below are the two solutions (external and internal tangency)
The system of three quadratic equations was obtained for a pair of rotated ellipses. The first ellipse, has its center at $C_1 = (8,0)$, with $a_1 = 5, b_1 = 3$, and rotation angle of $30^\circ$. The second ellipse is centered at $C_2 = (0, 4)$, with $a_2 = 4 , b_2 = 2 $ and a rotation angle of $60^\circ$.
The scale factors obtained were
$s_1 = 2.679569 $ (for external tangency)
and
$ s_2 = 6.3 $ (for internal tangency)
They are plotted below.