Rotate rectangle around center keeping minimal distance to a baseline

944 Views Asked by At

I have a rectangle defined by it's width (w) and height (h) and a base line (B). How can I calculate the offset (x) of the rotation point for any given angle (α = 0°-360°) so the rectangle always touches the baseline? I can calculate each offset using trigonometry but I'm unable to find a function. rectangle rotation

2

There are 2 best solutions below

1
On BEST ANSWER

How about this algorithm:

You can represent an Axis-Aligned-Bounding-Box (AABB) using two points $min$ and $max$. The center of the box is the average of the two points. Lets enclose your original rectangle in the AABB. For a given angle $\alpha$ you just rotate the AABB points and then calculate a new AABB with points $min'$, $max'$ that tightly enclose the rotated one. That amounts to compute the min and max coordinates independently.

The offset in the y-axis is just $\|min.y - min'.y\|$ or $\|max.y - max'.y\|$.

3
On

For $0^\circ < \alpha < 90^\circ$, its pretty straightforward. In the first figure, join the bottom right corner (call $P$) to centre of rectangle (call $O$). Rotation occurs about this point. And the line $CO$ we just drew, let the angle it makes from baseline be $\theta$. Here $\theta = \arcsin \left(\tfrac{h}{\sqrt {w^2+h^2}}\right) + \alpha$.

The angle distance of $O$ from baseline $B$ is thus given as $OP \sin (\theta)$ that is $\tfrac{\sqrt{h^2+w^2}}{2} \sin \left(\arcsin\left(\tfrac{h}{\sqrt{h^2+w^2}}+\alpha\right)\right)$