3

I'm trying to figure out a formula... for how much a picture (rectangle) would have to be scaled up during a rotation (at any rotation amount) so that you don't see the edge of the picture in the square of the bounding box.

rotation problem graphic

If the bounding box is square it might be a simpler formula. But when the box is rectangle it seems like it gets harder to figure out. Although it might be a different formula if the width>height, than if the height>width. The graphic above shows 30 degrees which took 170%. I would think 45 degrees would be the worst case scenario then from there to 90 it would go down again.

To clarify, I am looking for a formula to calculate a scale factor to use given an angle a height and a width. We can assume the size and aspect of the bounding box to be equal to the size and aspect of the photo. So the input is Angle, Height, and Width.. the output is Scale Factor.

Or actually, maybe it can be figured out using the Angle and an Aspect Ratio.. where aspect ratio is width/height.

In practice this will be done in code.. so it doesn't have to be a single formula. It can be different formulas for different scenarios.. such as if the w>h or w=h or w

badweasel
  • 151

1 Answers1

4

The formula you want in this case is as follows: $$ k = \cos\theta+\frac{W}{H}\sin\theta $$ Where: $H$ is the height, $W$ is the width, $\theta$ is the angle of rotation (from $0$ to $90$ degrees), and $k$ is the scaling factor.

Interestingly, this means that the "worst case" (largest scaling factor) corresponds to $$ \theta = \tan^{-1}\left(\frac{W}{H}\right) $$


Proof:

Proof of formula


In fact, I overlooked what happens when $W<H$. In fact, we should have

$$ k = \begin{cases} \cos\theta+\frac{W}{H}\sin\theta & W\geq H\\ \cos\theta+\frac{H}{W}\sin\theta & W < H \end{cases} $$

For the second case, we could draw the above and still have overlap. The more mathematically concise (but computationally expensive) way to write this would be $$k = \cos\theta + \max\left\{\frac{W}{H},\frac{H}{W}\right\}\sin\theta$$

Ben Grossmann
  • 225,327
  • Would you like a diagram to explain the formula, or is this all you needed? – Ben Grossmann Jul 08 '13 at 01:47
  • Actually, I'm not totally sure about this. Have to check it over again – Ben Grossmann Jul 08 '13 at 01:52
  • This is the correct formula, sorry for the hiccup before. I can provide a diagram if you'd like it. – Ben Grossmann Jul 08 '13 at 02:02
  • Diagrams are always a good idea. – dfeuer Jul 08 '13 at 03:58
  • Thanks! This is exactly what I was looking for. It makes sense when you show it like that too. The diagram helped a lot to make it make sense. I see the two triangles that you have to add up to make H'. And the ratio between H and H' is the scale factor. I'm not a mathematician but I have to figure out a lot of formulas to write games. – badweasel Jul 08 '13 at 06:22
  • Is your answer correct when h>w? On stack overflow someone gave this answer:

    If w>h, the scaling factor is (w/h) sin(angle) + cos(angle).

    If h>w, the scaling factor is (h/w) sin(angle) + cos(angle).

    – badweasel Jul 08 '13 at 07:32
  • My formula should work as long as you consistently take $W$ to be the horizontal dimension and $H$ to be the vertical one, since in any case you should be able to make these triangles. I think the SO answer assumes that $W$ is always the longer dimension, which makes sense in its own way. – Ben Grossmann Jul 08 '13 at 11:24
  • Hmm on second thought the other guy might be right. When you scale it by my amount, you might still have some overlap with the rectangles. Probably best just to try and see which works. – Ben Grossmann Jul 08 '13 at 11:37
  • The other guy is right, and here's the sanity check: let's say $$W = .01 \quad H=100 \quad \theta = 90˚$$ Our scale factor should be really big, and certainly nowhere near $1$. – Ben Grossmann Jul 08 '13 at 11:47
  • Thanks for the edit. The max thing actually works easier in code than the if then also. So that's great. – badweasel Jul 08 '13 at 21:36