Find the Bounding Rectangle of Rotated Rectangle [

2019-02-11 00:03发布

This question already has an answer here:

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.

2条回答
走好不送
2楼-- · 2019-02-11 00:15

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/

查看更多
手持菜刀,她持情操
3楼-- · 2019-02-11 00:23

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

查看更多
登录 后发表回答