3
$\begingroup$

Edit: You can calculate the bounding box for a rotated rectangle. If you change the rotation of the rectangle, the aspect ratio of the bounding box changes too.

I have a rectangle with a fixed aspect ratio. I need to find the angle that my rectangle must be rotated by, to make it's bounding box have a specific aspect ratio.

example:
I have an rectangle with an aspect ratio of 16:9. By how many degrees do I need to rotate it, to make it's bounding box have an aspect ratio of 4:3?

Origial post:

I've had this problem for the last few days, but wasn't able to come up with an solution.

I have:

  • a box box1 (blue) with fixed width a and height b
  • a box box2 (red) with a fixed aspect ratio, it can be scaled

I need:

  • the angle by which box2 must be rotated

My problem:
I want to put box2 diagonally inside box1. box2 should be as big as possible.

I'm having problems, finding the right angle for box2. My first idea was to calculate the angle of the diagonal of box1. I would then use this angle to rotate box2. As you can see, this only works if box1 is a square:

example 1 (working)
example 2 (not working)

As you can see in example 2, the red box doesn't have the right angle.

I would like to make box2 as big as possible while keeping its aspect ratio. This means, that all four corners of box2 will touch the sides of box1, which would also be forcing it to be 'diagonally centered' (? if that is propery english).

I was unable to find a solution for this problem. The best post I found was this, but I don't know if and how I can deduce the solution to my problem from it.

Thank you in advance

$\endgroup$
5
  • $\begingroup$ I suggest breaking the problem down into two sub-problems. First, given a fixed point on, say, the bottom edge of box 1, find the maximal box 2 that will fit. Once you have that, you can compute the area or side length of the inscribed box as a function of this point and solve a fairly standard optimization problem for it. $\endgroup$
    – amd
    Jun 19, 2018 at 22:46
  • 1
    $\begingroup$ I think similar right triangles are the key to this problem. The largest scaled version of box2 will touch box1 on all four sides, with the degenerate case of both rectangles coinciding when aspect ratios allow. $\endgroup$
    – hardmath
    Jun 19, 2018 at 22:52
  • $\begingroup$ In a similar vein, first solve the problem of finding the largest inscribed box for a fixed rotation angle, then optimize. $\endgroup$
    – amd
    Jun 19, 2018 at 22:53
  • $\begingroup$ this is related, but I assume that $a,b$ are fixed, so it's wrong to assume $a=b$, i.e. the bounding box is a square. $\endgroup$
    – John Glenn
    Jun 20, 2018 at 4:24
  • $\begingroup$ To get the result I want, I need to rotate my rectangle by a certain number of degrees, so that the bounding box of it has a specific aspect ratio. I think I didn't explain my problem well enough, so I edited my question. $\endgroup$ Jun 20, 2018 at 6:33

1 Answer 1

0
$\begingroup$

enter image description here

The inside box is skewed which makes it a more difficult geometry problem but the centers of the two are at the same point.

$(cd)^2 = (a-x)^2 + (b-y)^2$ and so

$d^2 = \frac{(a-x)^2 + (b-y)^2}{c^2}$………….(1)

$y^2 = d^2 – x^2$………………………….(2)

$d^2+ (cd)^2 = (a-2x)^2 + b^2$

$d^2(c^2+1) = (a-2x)^2 + b^2$

$d^2 = \frac{(a-2x)^2 + b^2}{(c^2+1)}$…………(3)

Substituting…..

$d^2 = \frac{(a-x)^2 + (b-\sqrt{(d^2-x^2)})^2}{c^2}$ gets rid of the y term

$\frac{(a-2x)^2 + b^2}{c^2+1} = \frac{(a-x)^2 + (b-\sqrt{(\frac{(a-2x)^2 + b^2}{(c^2+1)}–x^2}))^2}{c^2}$ gets rid of the d terms.

For $a = 9, b = 6$ and $c = 5$,

$117-18x-12\sqrt{\frac{4x^2-36x+117}{26}-x^2}-24\frac{4x^2-36x+117}{26} = 0$

then $x = .875, y = 1.625, d = 1.8456$ and $cd = 9.2280$

Edit: I corrected some errors and have verified the result.

To solve $117-18x-12\sqrt{\frac{4x^2-36x+117}{26}-x^2}-24\frac{4x^2-36x+117}{26} = 0$

Using Newton's method to solve for $x$...............

$x_1 = x_0 - \frac{f(x_0)}{f'(x_0)}$

Then $x_{n+1} = x_n - \frac{f(n)}{f'(n)}$

$f(x) = 117-18x-12\sqrt{\frac{4x^2-36x+117}{26}-x^2}-24\frac{4x^2-36x+117}{26}$

and $f'(x) = -18 - \frac{(192x-864)}{26}-\frac{\frac{48x-216}{26}-12x}{\sqrt{\frac{4x^2-36x+117}{26}-x^2}}$

$x_0 = 1$

$x_1 = 1 - \frac{2.4617}{20.1016} = .8775$

$x_2 = .8775 - \frac{.0484}{19.3607} = .8750$

$x = .875$

Let me try to explain further but before I do, I did find an error in the derivative. The good news about this is it converges on a solution much faster. There are 3 equations with 3 unknowns $x, y$ and $d$. We are given $a, b$ and $c$ so we use the equation $\frac{(a-2x)^2 + b^2}{c^2+1} = \frac{(a-x)^2 + (b-\sqrt{(\frac{(a-2x)^2 + b^2}{(c^2+1)}–x^2}))^2}{c^2}$ to solve for $x$, then $y$ and $d$.

$a = 9, b = 6$, and $c = 5$ so......

$\frac{(9-2x)^2 + 6^2}{5^2+1} = \frac{(9-x)^2 + (6-\sqrt{(\frac{(9-2x)^2 + 6^2}{(5^2+1)}–x^2}))^2}{5^2}$

$\frac{4x^2 - 36x + 117}{26} = \frac{(x^2 - 18x + 81) + (6-\sqrt{(\frac{(4x^2 - 36x + 117)}{(26)}–x^2}))^2}{25}$

$25\frac{(4x^2 - 36x + 117)}{26} = (x^2 - 18x + 81) + (6-\sqrt{(\frac{(4x^2 - 36x + 117)}{(26)}–x^2}))^2$

$25\frac{(4x^2 - 36x + 117)}{26} = x^2 - 18x + 81 + 36-12\sqrt{(\frac{(4x^2 - 36x + 117)}{(26)}–x^2}) + \frac{4x^2-36x+117}{26} - x^2$

$25\frac{(4x^2 - 36x + 117)}{26} = - 18x + 117 - 12\sqrt{(\frac{(4x^2 - 36x + 117)}{(26)}–x^2}) + \frac{4x^2-36x+117}{26}$

$24\frac{(4x^2 - 36x + 117)}{26} = - 18x + 117 - 12\sqrt{(\frac{(4x^2 - 36x + 117)}{(26)}–x^2})$

$0 = - 18x + 117 - 12\sqrt{(\frac{(4x^2 - 36x + 117)}{(26)}–x^2})-24\frac{(4x^2 - 36x + 117)}{26}$

Hence $117-18x-12\sqrt{\frac{4x^2-36x+117}{26}-x^2}-24\frac{4x^2-36x+117}{26} = 0$

This just goes through my calculations to get to the Newton's method formula for this particular problem with actual values of $a, b$ and $c$.

Really, not knowing values for $a, b$ and $c$ beforehand, you should work with the equation

$\frac{(a-2x)^2 + b^2}{c^2+1} = \frac{(a-x)^2 + (b-\sqrt{(\frac{(a-2x)^2 + b^2}{(c^2+1)}–x^2}))^2}{c^2}$

$c^2((a-2x)^2 + b^2) = (c^2+1)((a-x)^2 + (b-\sqrt{(\frac{(a-2x)^2 + b^2}{(c^2+1)}–x^2}))^2$

Which will reduce to...............

$(a^2 + b^2) -2ax-2b\sqrt{\frac{(a-2x)^2 + b^2}{c^2 + 1}-x^2}-(c^2 - 1)\frac{(a-2x)^2 + b^2}{c^2 + 1} = 0$

Then for Newton's method.....

$$f(x) = (a^2 + b^2) -2ax-2b\sqrt{\frac{(a-2x)^2 + b^2}{c^2 + 1}-x^2}-(c^2 - 1)\frac{(a-2x)^2 + b^2}{c^2 + 1}$$

$$f'(x) = -2a + \frac{4(c^2-1)(a-2x)}{c^2+1}-\frac{\frac{-4b(a-2x)}{c^2+1}-2bx}{\sqrt{\frac{(a-2x)^2 + b^2}{c^2 + 1}-x^2}}$$

Where $a$ and $b \ (a > b)$ are the length and width of the outer rectangle and c is the aspect ratio $(\frac{\text{length}}{\text{width}})$ of the inner rectangle.

$\endgroup$
6
  • $\begingroup$ Thank you for your answer. This looks like the solution. The only thing I'm struggling with is solving $x$ or $y$. Could you please give me a hint on how to do this? Maybe I'm missing something obvious but when trying to solve one of them, my approach gets overly complicated. $\endgroup$ Jun 21, 2018 at 9:57
  • $\begingroup$ x doesn't appear to be separable in the equation so It takes Newton's method to solve. $x_1 = x_0 - \frac{f(x_0)}{f'(x_0)}$ and then $x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}$ etc, etc. I took a short cut and entered the equation into a graphing calculator and found the value of x at the x intercept where f(x) = 0. $\endgroup$
    – Phil H
    Jun 21, 2018 at 13:50
  • $\begingroup$ I added the solution for solving x using Newton's method to my answer. I don't know whether this is out of the scope of your math topic but this problem is complex and requires knowing the derivative of the function to solve for x. $\endgroup$
    – Phil H
    Jun 21, 2018 at 17:59
  • $\begingroup$ Sorry I took so long, I've finally found the time to get back to this problem. I'm still struggling to understand how you solved x y d cd. In the end I want to use this in a script. You should be able to pass a b c to a function. Each function should return one of the searched values ($x$/$y$/...). I understand your solution, however I'm unable to come up with a general formular for my script. I tried to convert your formular (the one before you inserted the values), so that I would be able to insert passed values and then solve via Newton's method, but I'm unable to find the solution. $\endgroup$ Jul 4, 2018 at 15:37
  • $\begingroup$ @uniquestring I did some more work and have f(x) and f'(x) in terms of a, b and c. (see latest revision) I did add in how I determined this and checked out that these new equations work. I did find an error in the original derivative f'(x) which now makes it converge on a solution much sooner. Let me know if you have further questions. $\endgroup$
    – Phil H
    Jul 4, 2018 at 21:14

You must log in to answer this question.

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