Finding bounding rectangle after rotation

213 Views Asked by At

fig depicting problem

I have two rectangles inner rectangle(green) and the outer one (red).

OuterRectangle will be calculated by adding some offset to inner rectangle as shown below.

OuterLeft = innerLeft - 100; OuterTop = innerTop - 100; OuterRight = innerRight + 100; OuterBottom = innerBottom + 100;

But this will not work once the inner rectangle is rotated . Please let me know how can i calculate outer rectangle based on inner rectangle after rotation by some angle.

Thanks in advance

1

There are 1 best solutions below

5
On

If you knew the rotation angle, you could simply apply the same rotation to the outer rectangle. So I assume that you only have to corner points of the (rotated) inner rectangle.

Let $p_0, p_1, p_2, p_3$ be the points of the inner rectangle, ordered counterclockwise (starting at the upper left corner, in this case). Then you can compute the vectors describing the edges

  • $e_0 = p_1 - p_0$
  • $e_1 = p_2 - p_1$
  • $e_2 = p_3 - p_2$
  • $e_3 = p_0 - p_3$

and the corresponding directions, by normalizing these vectors

$d_i = e_i / |e_i|$ for $i = 0...3$

The corners of the outer rectangle can then be computed based on the original points and these direction vectors. Let $s_t, s_l, s_b, s_r$ denote the offset of the outer rectangle at the top, left, bottom and right side, respectively (in your case, they are all 100).

  • $q_0 = p_0 - s_t d_0 + s_l d_3$
  • $q_1 = p_1 - s_l d_1 + s_b d_0$
  • $q_2 = p_2 - s_b d_2 + s_r d_1$
  • $q_3 = p_3 - s_r d_3 + s_t d_2$

(This is not really related to trigonometry, by the way)