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?


1 Answer 1


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.

enter image description here

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\%$$

  • $\begingroup$ 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. $\endgroup$ Dec 18, 2014 at 4:04
  • $\begingroup$ @remus Great. Let us know how it goes. $\endgroup$
    – Mick A
    Dec 18, 2014 at 8:56
  • $\begingroup$ A note for anyone finding this: remember to "absolute" the angle! $\endgroup$
    – thomasb
    Feb 26, 2015 at 14:14
  • $\begingroup$ 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) $\endgroup$
    – thomasb
    Feb 26, 2015 at 14:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .