Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scale a rotated rectangle to always fit another rectangle

the background of my question is the following. I have a picture and a crop rectangle which describes how the picture should be cropped to produce the resulting picture. The crop rectangle is always smaller or at maximum the size of the picture. Now it should be possible to rotate the crop rectangle. This means that when rotating the crop regtanle inside the picture, the crop must be scaled in order that its extends does not exceed the photo.

Can anybode help me with a formula of how to compute the scale of the crop rectanlge based on the axis aligned photo regtancle?

My first attempt was to compute a axis aligned bounding box of the crop rectanlge and than make this fit it the photo rectangle. But somehow i get stuck with this approach,

Edited: One more think to note: - The crop rectangle can have other dimension and another center point inside the surrounding rectangle. This means the crop rectangle can be much smaller but for example is located at the lower left bound of the picture rectangle. So when rotating the smaller crop it will also exceed its limits

Thanks in advance Sebastian

like image 978
TosKen Avatar asked Nov 15 '25 21:11

TosKen


1 Answers

When you rotate an axis-aligned rectangle of width w and height h by an angle φ, the width and height of the rotated rectangle's axis-aligned bounding box are:

W = w·|cos φ| + h·|sin φ|
H = w·|sin φ| + h·|cos φ|

(The notation |x| denotes an absolute value.) This is the bounding box of the rotated crop rectangle which you can scale to fit the original rectangle of width wo and height ho with the factor

a = min(wo / W, ho / H)

if a is less than 1, the rotated crop rectangle fits inside the original rectangle and you don't have to scale. Otherwise, reduce the crop rectangle to the scaled dimensions

W′ = a·W
H′ = a·H
like image 134
M Oehm Avatar answered Nov 17 '25 19:11

M Oehm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!