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 widthaand heightb - a box
box2(red) with a fixed aspect ratio, it can be scaled
I need:
- the angle by which
box2must 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
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.