algorithmgeometryglm-math

How can I find the largest axis-aligned rectangle of a given aspect ratio that can fit inside a rotated rectangle?


Given a rotated rectangle, how can I find the largest axis-aligned rectangle of a given aspect ratio that can fit within the original rectangle? For example, I'd want to find the largest rectangle of aspect ratio 4:3 that can fit in another arbitrary rectangle.

I know the position, width, height and rotation of the enclosing rectangle. I know the width and height aspect ratio of the inscribed rectangle. I need to figure out the position, width and height of the inscribed rectangle.

Here's an example of what I'm looking for:

enter image description here

If it helps, I'm using the GLM C++ library.

Edit: not sure I understand why this should be closed. There's plenty of questions on geometry on stackoverflow. For example:

  1. Given aspect ratio of a rectangle, find maximum scale and angle to fit it inside another rectangle
  2. The largest aligned rectangular with fixed aspect ratio in an arbitrary convex polygon?
  3. Calculating smallest rotated rectangle that bounds another and maintains aspect

Solution

  • Let θ be the acute angle shown.

    enter image description here

    Project the axis-aligned rectangle (with sides w and aw, where a is the aspect ratio) onto the directions of the sides of the rotated rectangle. Then, to fit, you need, simultaneously,

    enter image description here

    Hence, your desired width is limited to

    enter image description here