TITLE: Ellipse tangent to two circles
QUESTION [8 upvotes]: We're given two circles with radii $p$ and $q$, one centered at the origin, and one centered at the point $(w,0)$. 
I want to construct an ellipse of the form
$$
\frac{(x-c)^2}{a^2} + \frac{y^2}{b^2} = 1
$$
that is tangent to the two circles, as shown here:

If $b$ is known, can we obtain closed form expressions for $a$ and $c$ as functions of $p$, $q$, $w$ and $b$.

REPLY [2 votes]: I figured it out myself, eventually. Code is:
    p2 = p^2
    q2 = q^2
    b2 = b^2

    h = b2 - p2
    k = b2 - q2

    c = w * (h - sqrt(h*k)) / (h-k)

    a2 = (h + c^2) * b2 / h

    a = sqrt(a2)

In traditional notation, we first define:
$$
h = b^2 - p^2
$$
$$
k = b^2 - q^2
$$
Note that $h$ and $k$ are both positive in "reasonable" configurations. Then
$$
c = \frac{w(h - \sqrt{hk})} { h-k}
$$
$$       
a = \sqrt{\frac{(h + c^2)b^2 }{ h }}
$$
I'll write up derivation later. Wanted to get this posted before people spent more time on it.