简体   繁体   English

平移矩形的坐标

[英]Translating a rectangle's coordinates

I have two applications, one that creates a rectangle, and output's it's left,top,width,height and rotation angle, and another that draws the rectangle to the screen.我有两个应用程序,一个创建矩形,输出它的左、上、宽、高和旋转角度,另一个将矩形绘制到屏幕上。

Application1 (writer) uses the top, left as the origin for rotation. Application1(编写器)使用左上角作为旋转原点。

Application2 (reader) uses the center as the origin for rotation. Application2(阅读器)使用中心作为旋转的原点。

I'm trying to get a deltaX, and deltaY so that I can draw the rectangle, rotate it about the origina, and use a built in translate method, to shift the rectangle into position.我正在尝试获取 deltaX 和 deltaY,以便我可以绘制矩形,围绕原点旋转它,并使用内置的平移方法将矩形移动到位。

I know that a points rotation can be calculated as follows:我知道点旋转可以计算如下:

x' = x*cos(theta) - y*sin(theta) y' = x+sin(theta) + y*cos(theta) x' = x*cos(theta) - y*sin(theta) y' = x+sin(theta) + y*cos(theta)

But, with that, I can't seem to figure out the actual delta values needed for the translate method.但是,有了这个,我似乎无法弄清楚翻译方法所需的实际增量值。

In the image below, the white rectangle is drawn using top/left as the rotation point, while the green rectangle is drawn using center as the rotation point.在下图中,白色矩形是使用 top/left 作为旋转点绘制的,而绿色矩形是使用 center 作为旋转点绘制的。 I'd like to shift the green rectangle onto the white one.我想将绿色矩形移到白色矩形上。

(0,0) (0,0)

图片

You have to translate by:您必须通过以下方式翻译:

(Tx, Ty) = (-w/2 (1-cos theta) - h/2 sin theta), -h/2 (1-cos theta) - w/2 sin theta)

Development:发展:

  • In application 1, the top left vertex of a rectangle shall be在应用程序 1 中,矩形的左上顶点应为(x1, y1). .

  • Since application 1 rotates rectangles about the top left vertex, their rotated position coincides:由于应用程序 1 围绕左上顶点旋转矩形,因此它们的旋转位置重合:(x1',y1') = (x1,y1)

  • Application 2 uses center coordinates, which relate to top-left point in application 1 as follows:应用程序 2 使用中心坐标,它与应用程序 1 中的左上角点相关,如下所示: (xc2,yc2) = (x1,y1) + (w/2,h/2)

  • To calculate the rotated top-left vertex in application 2, we apply the rotation matrix R :为了在应用程序 2 中计算旋转后的左上角顶点,我们应用旋转矩阵R (x2',y2') = (xc2,yc2) - R (w/2,h/2)

  • So the translation vector T is (substituting equations above):所以平移向量T是(代入上面的等式): 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM