Alan
Alan

Reputation: 46833

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.

Application2 (reader) uses the center as the origin for rotation.

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.

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)

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. I'd like to shift the green rectangle onto the white one.

(0,0)

Image

Upvotes: 2

Views: 1740

Answers (1)

Daniel Gehriger
Daniel Gehriger

Reputation: 7468

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 (x1, y1).

  • Since application 1 rotates rectangles about the top left vertex, their rotated position coincides: (x1',y1') = (x1,y1)

  • Application 2 uses center coordinates, which relate to top-left point in application 1 as follows: (xc2,yc2) = (x1,y1) + (w/2,h/2)

  • To calculate the rotated top-left vertex in application 2, we apply the rotation matrix R:(x2',y2') = (xc2,yc2) - R (w/2,h/2)

  • So the translation vector T is (substituting equations above): enter image description here

Upvotes: 3

Related Questions