# Geometrical calculation to determine size difference between two rectangles when rotating one

I've asked a programming question on StackOverflow here which should give you a good understanding why I'm trying to do this. I'm asking it here because it's now down entirely to the mathematics of the calculation.

I've got two rectangles: a "crop" rect and an "image" rect that are on top of each other. I've designed it so that either both widths or both heights of each will start out the same (e.g. if they are landscape rectangles, regardless of width, the height of both will be the same).

When rotating the image rect, the crop rect stays the same size, but I need to mathematically calculate how much to enlarge the image rect so that it always completely fills the crop rect. (The .gif in my linked question illustrates this rather well).

Known values:

• The size and coordinates of each rect before rotation
• The size of the bounding rect that the rotated image rect fits into
• The angle of rotation in radians

How, based on the degree of rotation (radians), and the size of each rectangle, can I calculate the difference in scale required to enlarge the image rect to fit entirely inside the crop rect when rotating it?

I'll assume that the crop rectangle is centred inside the image rectangle, which is I think what you intend. I'll also work just with widths, heights and angles in the hope that you can calculate the coordinate points from that.

In the image, the crop rectangle is black and the image rectangle is red. The notation is:

\begin{eqnarray*} w_c &=& \text{width of crop rectangle} \\ h_c &=& \text{height of crop rectangle} \\ w_i &=& \text{width of image rectangle} \\ h_i &=& \text{height of image rectangle} \\ \alpha &=& \text{angle of clockwise rotation of the image.} \end{eqnarray*}

Instead of thinking of the image rotating clockwise, think of the crop rotating anti-clockwise. Then we want the "effective" width and height of the crop after rotation, denoted by $w_c^{'}$ and $h_c^{'}$ respectively.

From the image we see:

\begin{eqnarray*} w_c^{'} &=& w_1 + w_2 \\ &=& w_c\cos\alpha + h_c\sin\alpha. \\ && \\ h_c^{'} &=& h_1 + h_2 \\ &=& w_c\sin\alpha + h_c\cos\alpha. \\ \end{eqnarray*}

Now we compare $w_c^{'}, h_c^{'}$ with $w_i, h_i$. Say, for example, $w_c^{'} = 1.2\times w_i$ and $h_c^{'} = 1.4\times h_i$. Then, since $1.4 \gt 1.2$, we need to increase the size of the image by a factor of $1.4$. That is, with a zoom percentage of $140\%$.

Our required zoom factor is then:

$$\text{Zoom }\% = \max\left\{\dfrac{w_c^{'}}{w_i}, \dfrac{h_c^{'}}{h_i}\right\} \times 100\%$$

• Lookin pretty promising Mick, thanks! Going to work on translating this into code over the next couple days and see if I can get it working. – brandonscript Dec 18 '14 at 4:04
• @remus Great. Let us know how it goes. – Mick A Dec 18 '14 at 8:56
• A note for anyone finding this: remember to "absolute" the angle! – thomasb Feb 26 '15 at 14:14
• Also, this calculation doesn't straight work for angles > 90° (wich is logical, but if you're a programmer like me, remember to check for that, not like me) – thomasb Feb 26 '15 at 14:21