<s>[INST] How to calculate the size of any rectangle to fit into an ellipse? It's been a long time I do not review my math knowledge, please help me out.
I have an ellipse image with fix size, let's say it has the bounding rect with width=w1, height=h1, and I have any random image with random size let's say width=w2, height=h2, I need to make a thumbnail of this image so that it fits totally inside the ellipse. 
The center of the two images is the same, and it's in the center position.
Any thoughts on how to calculate the new w2' and h2'?
Thanks in advance,
 [/INST] For your ellipse centred on (0,0) the equation is:
$$
\frac{x^2}{w1^2}+\frac{y^2}{h1^2} = 1 \Rightarrow y^2 = h1^2 \cdot \left( 1 - \frac{x^2}{w1^2} \right)
$$
Now imagine a box centred on (0,0) of width $w2'$ and height $h2'$ where $\frac{h2'}{w2'} = \frac{h2}{w2}$.
Finally imagine a straight line passing through (0,0) and the top right and bottom left corners of the box.  The equation of this line will be :
$$y = \frac{h2}{w2} \cdot x$$
We can find $x$ such that $y$ is the same for both equations the note that $x = \frac{w2'}{2}$
Thus squaring our equation for the line and setting it equal to the ellipse we have
$$\begin{align}
\frac{h2^2}{w2^2} \cdot x^2 & = h1^2 \cdot \left( 1 - \frac{x^2}{w1^2} \right) \\
\frac{h2^2}{w2^2} \cdot x^2 & = h1^2  - \frac{h1^2 \cdot x^2}{w1^2} \\
x^2 \left( \frac{h2^2}{w2^2} + \frac{h1^2}{w1^2} \right) & = h1^2\\
x & = \sqrt{ \frac{h1^2}{\left( \frac{h2^2}{w2^2} + \frac{h1^2}{w1^2} \right) }}
\end{align}$$
Thus 
$$
w2' = 2 \cdot \sqrt{ \frac{h1^2}{\left( \frac{h2^2}{w2^2} + \frac{h1^2}{w1^2} \right) }}
$$
and 
$$
h2' = w2' \cdot \frac{h2}{w2}
$$
</s>