Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the Bounding Rectangle of Rotated Rectangle [duplicate]

I have rectangle with co-ordinates(x1,y1) and (x2,y2) and I have to rotate the rectangle an amount of θ about it centre using Rotation Matrix

 |  cosθ  sinθ |
 | -sinθ  cosθ |

I need to find the co-ordinates of bounding rectangle after rotation.

Before rotation

0,0
 |"""""""""""""""""""""""""""""""""""""""""""|
 |                                           |
 |  x1,y1                                    |
 |       |"""""""""""""|                     |
 |       |             |                     |
 |       |             |                     | 
 |       |             |                     |
 |       """""""""""""" x2,y2                |
 |                                           |
 |                                           |
  """"""""""""""""""""""""""""""""""""""""""" W,H

After rotation

 0,0
     |"""""""""""""""""""""""""""""""""""""""""""|
     |           ?,?                             |
     |            |""""/\"""""|                  |      
     |            |   /   \   |                  |
     |            |  /      \ |                  |
     |            | /        /|                  |
     |            |/        / |                  |
     |            |\       /  |                  |
     |            |  \    /   |                  |
     |            |    \ /    |                  |
     |             """""""""""  ?,?              |
     |                                           |
     |                                           |
      """"""""""""""""""""""""""""""""""""""""""" W,H

Is there any general equation for finding the co-ordinates of bounding rectangle?.

Thanks....

Haris.

like image 317
Haris Avatar asked Nov 07 '13 07:11

Haris


People also ask

How do you rotate bounding boxes?

Rotating bounding boxes are represented using coordinates of the center point (x, y), width w, height h, and θ. Rotation Angle (θ) is the horizontal axis (the X-axis) counterclockwise to deal with the rectangle on the edge of the first Angle. (b) Normalization strategy for the representation of rotating bounding box.

What is a rectangle rotated?

A rectangle is an example of a shape with rotation symmetry. A rectangle can be rotated about its center and it will look exactly the same and be in the same location. The only difference is the location of the named points. A rectangle has half-turn symmetry, and therefore is order 2.

How do you find the coordinates of the rotation of a rectangle?

See the Wikipedia article on rotation. The essence is this: (1) If c is the center point, then the corners are c + (L/2,W/2), +/- etc., where L and W are the length & width of the rectangle. (2) Translate the rectangle so that center c is at the origin, by subtracting c from all four corners.


2 Answers

Just mark all Fi angles on your drawing, and you can see that

Old_Width = X2_Old - X1_Old, Old_Height = Y2_Old - Y1_Old
New_Height = Old_Width * Abs(Sin(Fi)) + Old_Height * Abs(Cos(Fi))
New_Width = Old_Width * Abs(Cos(Fi)) + Old_Height * Abs(Sin(Fi))
X1_New = X1_Old - (New_Width - OldWidth) / 2 = 
         (X1_Old + X2_Old - New_Width) / 2

enter image description here

Delphi test:

procedure TForm1.DrawRotatedRectWithFrame(X0, Y0, X1, Y1: Integer; Fi: Double);
var
  P: array[0..3] of TPoint;
  CX, CY, WX, WY, NW, NH : Integer;
  CF, SF: Double;
begin
  CX := (X0 + X1) div 2;  //Center point
  CY := (Y0 + Y1) div 2;
  WX := (X1 - X0) div 2;  //Half-width
  WY := (Y1 - Y0) div 2;
  SinCos(Fi, SF, CF);
  //calculate vertices of rotated rectangle
  P[0] := Point(Round(CX  -WX*CF + WY * SF), Round(CY - WX * SF - WY * CF));
  P[1] := Point(Round(CX  +WX*CF + WY * SF), Round(CY + WX * SF - WY * CF));
  P[2] := Point(Round(CX  +WX*CF - WY * SF), Round(CY + WX * SF + WY * CF));
  P[3] := Point(Round(CX  -WX*CF - WY * SF), Round(CY - WX * SF + WY * CF));
  Canvas.Polygon(P); //draw rotated rectangle

  Canvas.Rectangle(CX - 2, CY - 2, CX + 3, CY + 3); //mark center point
  NH := Round(Abs(WX * SF) + Abs(WY * CF));  //boundrect half-height
  NW := Round(Abs(WX * CF) + Abs(WY * SF));  //boundrect half-width
  Canvas.Brush.Style := bsClear;
  Canvas.Rectangle(CX - NW, CY - NH, CX + NW, CY + NH); //draw bound rectangle
end;

Output example:

enter image description here

like image 108
MBo Avatar answered Oct 15 '22 11:10

MBo


Point (x1, y1) rotates to (x1 cos θ - y1 sin θ, x1 sin θ + y1 cos θ), while point (x2, y2) rotates to (x2 cos θ - y2 sin θ, x2 sin θ + y2 cos θ). The other two points can be calculated accordingly.

The coordinates of the bounding reactangle are (x3, y3) and (x4, y4), where x3 is the smallest of all new x coordinates, y3 the smallest of all new y coordinates, x4 the greatest of all new x coordinates and y4 the greatest of all new y coordinates.

Which of the corners produces the smallest x (and so on) depends on your angle or rotation. For angles from 0° to 90°, x3 will come from (x1, y1), so x3 = x1 cos θ - y1 sin θ. For angles from 90° to 180°, it will come from (x2, y1), and so on. So either you decide which points to use based on your angle of rotation, or you just take the smallest and greatest of all x's and y's.


But I think you should probably ask this on https://math.stackexchange.com/

like image 41
SQB Avatar answered Oct 15 '22 11:10

SQB