0
votes

I have two rectangles: red and green. For each of them, I have the following information:

  • Center point (x and y coords).
  • Rotation angle
  • Width and height

The rectangles will always be moving in positive coordinates. Edit: No coordinate can ever be negative: the rectangles are always located in positive coordinates. Therefore, the center will never be (0,0).

Problem

I have an starting position. To simplify the example, let's say that my red and green rectangles are located as follows:

enter image description here

Now, I rotate the red rectangle using an angle phi which goes between 0º and 90º. However, the green rectangles needs to rotate and keep its position regarding the red rectangle. The green rectangle is not only rotating but also moving.

Let see an image (please excuse the sketch-quality):

enter image description here

My Question:

How can I obtain the new center coords for the green rectangle?

1

1 Answers

1
votes

Rotation is about some point (rx, ry).

Edit: As comment says, rotation center (rx, ry) is red center. Formula remains the same.

If green center was at (gx, gy), then after rotation it has coordinates

 gx' = rx + (gx - rx) * Cos(Phi) - (gy - ry) * Sin(Phi)
 gy' = ry + (gx - rx) * Sin(Phi) + (gy - ry) * Cos(Phi)