[Math] Geometrical calculation to determine size difference between two rectangles when rotating one

geometryrectanglesrotations

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?

Best Answer

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

Related Question