How to calculate the size of any rectangle to fit into an ellipse?

2.2k Views Asked by At

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,

3

There are 3 best solutions below

1
On BEST ANSWER

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

1
On

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

2
On

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