# 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'?

• You mean ellipse instead of eclipse? Dec 17, 2013 at 11:15
• yes, sorry I was confused between the two words. Dec 17, 2013 at 11:16

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}$$

• hi, thanks for the excellent answer. Dec 25, 2013 at 12:43

Is this what you want?

Let us consider an ellipse $\frac{x^2}{a^2}+\frac{y^2}{b^2}=1$ where $a\gt 0,b\gt0$.

Then, if you cut it by a line $x=t$ where $0\lt t\lt a$, you'll get $b\sqrt{1-\frac{t^2}{a^2}}$ as the half of the height. This is because you can get $\frac{t^2}{a^2}+\frac{y^2}{b^2}=1$ and solve it.

This means that $w_2=2t, h_2=2b\sqrt{1-\frac{t^2}{a^2}}$ where $0\lt t\lt a$. You can get any size of rectangle by substituting a number for $t$.

• thanks, you give me some clues, however, it's not what I want. I need a programmatic way to resize any image of random size (w2,h2) to fit into the fixed ellipse region of bounding rect size (w1,h1). Dec 17, 2013 at 13:11

Let me start : we put the center of the ellipse at $(0, 0)$; the point on the right side is $(a, 0)$, the top point is $(0, b)$. Let us put a point at $x (0 < x < a)$; the corresponding point on the ellipse is given by the equation $\left(\frac{x}{a}\right)^2 + \left(\frac{y}{b}\right)^2 = 1$ ; then the top right corner of the rectangle has coordinates $\left(x , b \sqrt{1 - \left(\frac{x}{a}\right)^2}\right)$. So, the area of the fourth of the rectangle is given by $xy = bx\sqrt{1 - \left(\frac{x}{a}\right)^2}$ and this is what you want to maximize.

The maximum corresponds to a value of x such that the derivative is zero. Compute the derivative of the area; do not forget to simplify it. You should arrive to $x = \frac{a }{\sqrt{2}}$ and $y = \frac{b}{\sqrt{2}}$.

• Hi, thanks Claude, it's a good solution. though, it's not what I want, I have an image of random size, width=w2, height= h2. It needs to just fit into the ellipse without going out into the ellipse region. Your solution does not consider into the size of the image. Dec 17, 2013 at 13:08
• @HoangPham. I thought you wanted the maximum area of the rectangle fitting in the ellipse. So, what I gave you are the upper bounds in x and y directions you cannot exceed if you want to stay inside the ellipse. Dec 17, 2013 at 13:12