Calculate the dimensions of a rotated rectangle inside a bounding box

trigonometry

Rotated rectangle inside a bounding box

I'm trying to figure out if it's possible to calculate the width & height of the gray rectangle if I only know the width & height of the dotted red rectangle and the angle of the rotation.

I've tried substituting values for the width and height and using $x \cos(\alpha) + y \sin(\alpha)$ and attempting to figure out a scaling factor, but I'm not really getting anywhere.

Best Answer

$$x\cos\alpha+y\sin\alpha=w\\x\sin\alpha+y\cos\alpha=h$$ Multiply first equation by $\cos\alpha$, the second by $\sin\alpha$ and subtract: $$x(\cos^2\alpha-\sin^2\alpha)=w\cos\alpha-h\sin\alpha$$or $$x=\frac{w\cos\alpha-h\sin\alpha}{\cos^2\alpha-\sin^2\alpha}$$ Similarly, multiply the first equation by $\sin\alpha$, the second by $\cos\alpha$ and subtract: $$y(\sin^2\alpha-\cos^2\alpha)=w\sin\alpha-h\cos\alpha\\y=\frac{w\sin\alpha-h\cos\alpha}{\sin^2\alpha-\cos^2\alpha}$$ Notice that the denominator might be $0$ for $\alpha=45^\circ$. Then you must have the numerator equal to $0$ as well, or $w=h$, so you have a square inside a square $x=y$ and $$2x\frac{\sqrt 2}2=w$$ or $$x=w\frac{\sqrt 2}2$$

Related Question