Math 使用边界框坐标计算旋转矩形变换
我有一个红色的容器旋转了Math 使用边界框坐标计算旋转矩形变换,math,rotation,geometry,bounding-box,coordinate-transformation,Math,Rotation,Geometry,Bounding Box,Coordinate Transformation,我有一个红色的容器旋转了-13度,在这个容器内有一个粉红色的正方形也旋转了-13度 仅使用下面的这些信息,我试图找到相对于原点(顶部,左侧)(0,0) 相对变换坐标是我需要在父对象内部进行的平移量。边界框就是包含旋转的大小(它是屏幕截图上的黑框) 粉红广场 size before rotation height : 398 width : 398 size after rotation height : 477 width : 477 Bounding box x : 179 y : 23
-13度
,在这个容器内有一个粉红色的正方形也旋转了-13度
仅使用下面的这些信息,我试图找到相对于原点(顶部,左侧)(0,0)
相对变换坐标是我需要在父对象内部进行的平移量。边界框就是包含旋转的大小(它是屏幕截图上的黑框)
粉红广场
size before rotation
height : 398
width : 398
size after rotation
height : 477
width : 477
Bounding box
x : 179
y : 230
Relative transform to parent
x : 0
y : 49
Rotation
-13 deg
红色容器
size before rotation
height : 632
width : 447
size after rotation
height : 716
width : 577
Bounding box
x : 179
y : 182.28
Relative transform to parent
x : 279
y : 182
Rotation
-13 deg
这就是我试图做的
yCoordinate = pink.relativeTransform.y + redContainer.boundingBox.y
xCoordinate = pink.relativeTransform.x + redContainer.boundingBox.x
我成功地获得了Y坐标,但我无法获得x坐标。我担心这将适用于所有角度。如果将变换表示为矩阵,您将很容易得到答案(请注意,我将使用“变换”一词来表示整个变换,包括旋转,而不仅仅是偏移向量)。顺便说一句,您的图像显示正向旋转(在数学意义上),因此我假设它实际上是
+13°
为了获得角度φ和偏移向量(tx,ty)旋转的变换矩阵,我们可以采用以下形式:
/ cos(phi) -sin(phi) tx \
T = | sin(phi) cos(phi) ty |
\ 0 0 1 /
因此,红色矩形相对于原点的变换为:
/ 0.974 -0.225 279 \
TRed = | 0.225 0.974 182 |
\ 0 0 1 /
粉色正方形相对于红色矩形的变换将是(相对于父矩形没有旋转,只是平移):
为了得到粉色正方形相对于原点的变换,我们只需将两个矩阵相乘:
/ 0.974 0.225 267.977 \
TRed * TPink = | 0.225 0.974 229.744 |
\ 0 0 1 /
我们可以看到,第一部分的旋转与TRed
中的旋转相同,即旋转13°。翻译(您正在寻找的向量)是(267.977229.744)
一般而言,该转换向量为:
/ cos(phi) * tPinkX - sin(phi) * tPinkY + tRedX \
\ sin(phi) * tPinkX + cos(phi) * tPinkY + tRedY /
/ cos(phi) * tPinkX - sin(phi) * tPinkY + tRedX \
\ sin(phi) * tPinkX + cos(phi) * tPinkY + tRedY /