0
votes

I need to find the co-ordinates of rotated rectangle from the bounding box of the rotated rectangle and angle of rotation.

Below image will explains what I need to find?

enter image description here

I have to find the points P1,P2,P3,P4

where,

R1,R2,R3,R4,angle are given.

1
How is the rectangle to be rotated specified? 4 points? Origin point, width, height? Equations for the for edges' lines? Also, what point is the rotation around?uliwitness
The rotation is based on center. Actually initially I have a rectangle A, and rotated it to R, then found the bounding box of R. Now I have the data bounding box and angle. Actually the rotation is doing from some software and I have only the data bounding box and angle as the information from the software.Haris
How is the rectangle A specified?uliwitness
There is no unique solution. At least in the 45° case. In the example, the black rectangle could be longer but thinner. How do you want to handle this ambiguity?Nico Schertler
If the rectangle's position and dimension is known, why don't you just transform it to get the rotated corner positions? Am I right in thinking that rectangle A is the unrotated black one?Nico Schertler

1 Answers

1
votes

Consider the following unrotated rectangle:

A -------- B
|          |
D -------- C

There are two options how the bounding box may be defined. Either:

  • its width is defined by A and C and its height is defined by B and D or
  • its width is defined by B and D and its height is defined by A and C.

The bounding box' width and height (let's call them w_b and h_b) can be easily calculated from the given R.

In the following, I assume that the rectangle's center is located at the origin. This makes computations easier to follow. The center may be incorporated afterwards.

Given the unrotated rectangle's width w and height h, the positions are:

A = (-w/2, -h/2)
B = ( w/2, -h/2)
C = ( w/2,  h/2)
D = (-w/2,  h/2)

Rotating A about the origin with angle angle, we get A':

A'.x = -w/2 * cos angle - h/2 * sin angle
A'.y =  w/2 * sin angle - h/2 * cos angle

Similar equations can be calculated for the remaining points. In the following, I focus on the first option of the bounding box definition (the second one is similar, which I leave up to you).

Assuming that the bounding box' width is defined by A and C, we get:

w_b = | C'.x - A'.x |

Then:

w_b = | w * cos angle + h * sin angle |

Similar:

h_b = | w * sin angle + h * cos angle |

All variables except w and h are known. Thus, you can solve the linear system of equations to get them. Then, use these values to calculate the unrotated points and then the rotated points.

After calculating the solution for a case, you need to check if the assumptions still hold. I.e. if the bounding box is actually defined by the corners you assumed. Otherwise, this case has no solution.

Note that the LSE becomes under-determined if angle is 45° because sine and cosine are equal. Thus, you get one additional degree of freedom for choosing w and h.