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?
I have to find the points P1,P2,P3,P4
where,
R1,R2,R3,R4,angle
are given.
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?
I have to find the points P1,P2,P3,P4
where,
R1,R2,R3,R4,angle
are given.
Consider the following unrotated rectangle:
A -------- B
| |
D -------- C
There are two options how the bounding box may be defined. Either:
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
.