gpt4 book ai didi

math - 找到旋转矩形的边界矩形

转载 作者:行者123 更新时间:2023-12-04 18:43:20 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Calculate Bounding box coordinates from a rotated rectangle

(11 个回答)


7年前关闭。




我有带有坐标(x1,y1)和(x2,y2)的矩形,我必须使用旋转矩阵将矩形围绕其中心旋转θ量

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

我需要在旋转后找到边界矩形的坐标。

旋转前
0,0
|"""""""""""""""""""""""""""""""""""""""""""|
| |
| x1,y1 |
| |"""""""""""""| |
| | | |
| | | |
| | | |
| """""""""""""" x2,y2 |
| |
| |
""""""""""""""""""""""""""""""""""""""""""" W,H

旋转后
 0,0
|"""""""""""""""""""""""""""""""""""""""""""|
| ?,? |
| |""""/\"""""| |
| | / \ | |
| | / \ | |
| | / /| |
| |/ / | |
| |\ / | |
| | \ / | |
| | \ / | |
| """"""""""" ?,? |
| |
| |
""""""""""""""""""""""""""""""""""""""""""" W,H

是否有任何通用方程可以找到边界矩形的坐标?。

谢谢....

哈里斯。

最佳答案

只需在绘图上标记所有 Fi 角度,您就可以看到

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

德尔福测试:
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;

输出示例:

enter image description here

关于math - 找到旋转矩形的边界矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19830477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com