Ellipse bounding rectangle I'm trying to find the ellipse that bounds a rectangle in a way that the "distance" between the rectangle and the ellipse is the same vertically and horizontally.
Here is an image to illustrate what I mean:

It's not perfectly drawn but the two "x" dimension need to be the same.
What I tried so far is:
The ellipse equation is $$\left(\frac{x}{a}\right)^2 + \left(\frac{y}{b}\right)^2 = 1$$

*

*The "ellipse bounding the rectangle" constraint gives us $$\left(\frac{w}{a}\right)^2 + \left(\frac{h}{b}\right)^2 = 1$$

*The "x dimension must be equal" constraint gives us $a-w = b-h$
So it's basically a $2$ equations system with $2$ unknowns.
But I got stuck at this point. If I do a substitution I have

*

*$$a = b-h+w$$

*$$\left(\frac{w}{b-h+w}\right)^2 + \left(\frac{y}{b}\right)^2 = 1$$
and I don't know how to solve it.
 A: If you centre the rectangle on the origin, then it has vertices at $(-\frac{w}{2}, -\frac{h}{2})$, $(\frac{w}{2}, -\frac{h}{2})$, $(\frac{w}{2}, \frac{h}{2}),$ and $(-\frac{w}{2}, \frac{h}{2})$.
An ellipse centred at the origin has equation $\frac{x^2}{a^2} + \frac{y^2}{b^2} = 1$ where $a$ is the 'horizontal' radius, and $b$ is the 'vertical' radius (here I am taking $a$, $b$ positive). Then the ellipse you are looking for must satisfy 
$$a-\frac{w}{2} = b - \frac{h}{2}$$
or equivalently
$$2a - w = 2b - h.$$
If $w$ and $h$ are given, this is one equation between the two unknowns $a$ and $b$. This corresponds to the fact that there is an infinite family of ellipses which satisfies the distance condition.
You need one more piece of information to uniquely determine the ellipse. For example, if you would like the ellipse to go through the vertices of the rectangle, then we must have
$$\frac{\left(\dfrac{w}{2}\right)^2}{a^2} + \frac{\left(\dfrac{h}{2}\right)^2}{b^2} = \frac{w^2}{(2a)^2} + \frac{h^2}{(2b)^2} = 1.$$
Rearranging $b$ for $a$ in the previous equation, we obtain $b = a + \frac{1}{2}(h-w)$. Substituting into the latter equation, we see that $a$ must satisfy 
$$\frac{w^2}{(2a)^2} + \frac{h^2}{(2a + h - w)^2} = 1$$
which, when rearranged, is a quartic in $a$.
Note, if $h = w$ (i.e. the rectangle is a square), then $a = b = \frac{w}{2}\sqrt{2}$ so the ellipse is a circle with radius equal to the distance between the origin and a vertex of the square (as one would expect).
Example: If $h = 1$ and $w = 2$ we obtain
$$\frac{4}{(2a)^2} + \frac{1}{(2a-1)^2} = 1$$
which becomes 
$$4a^4-4a^3-4a^2+4a-1=0.$$
This only has two real roots, but the only positive one is $a = 1.2908$. From the first equation, we then see that $b = 0.7908$.
The result can be seen below (made using WolframAlpha).

A: HINT 
Let us put everything centered at (0,0) and since everything is symmetrical, we shall just look at the first quadrant.
So you look for a value of "x", smaller than "a", such that (a - x) be equal to (b - y). But you know that the upright corner of the rectangle is also along the ellipse. This means that   
y = b Sqrt[1 - (x/a)^2].  
Can you continue from here ?  
Happy New Year
A: This is an old question, but I found myself doing a bunch of math to reduce it down to my use case. In case it helps anyone else, here's the (Python 3) class method I ended up with:
    def _describeEllipse(self, widthIn, heightIn):
        """
        Adapted from
        https://math.stackexchange.com/a/623933
        """
        w, h = widthIn, heightIn
        k = 2 * h * w**3 - h**2 * w**2 - w**4
        coeffs = [16, 16 * (h - w), -8 * h * w, 4 * w**2 * (w - h), k]
        solutions = np.roots(coeffs)
        validSolutions = np.array([np.real(x) for x in solutions if np.isreal(x) and x > 0])
        assert len(validSolutions) > 0, "No physical solutions found for ellipse"
        bPool = validSolutions + .5 * (h - w)
        goodPairs = [(x, y) for x, y in zip(validSolutions, bPool) if y > 0]
        assert len(goodPairs) > 0, "No semimajor/semiminor physical combination found"
        # There really should only be one valid solution, but in case there isn't, 
        # return the first
        return goodPairs[0]

