0
votes

I have two rectangles where one is a clipping for the other one. Now I want to rotate the bigger rectangle around the center of the clipping rectangle and adjust x/y values.

enter image description here

How can I calculate the new x/y values after rotation?

I actually just want to rotate the x/y of the bigger box around the center of the smaller box. So the x/y point of the bigger box is relative to the top/left point of the smaller box. I have the width and height of the smaller box so I can calculate x/y point of the big box relative to the center of small box. The angle to rotate is in degrees. The rotation can be any degree, for example 10.

3
What do you mean by " one is a clipping for the other one" ?Yunnosch
What is the significance of the inner rectangle? Does it ever make a difference from just rotating around the point c? When? How?Yunnosch
How far have you got yourself? Can you calculate the distance and angle of (x,y) to c? You do know the coordinates of c, don't you?Yunnosch
What is the angle by which you want to rotate? Looks like roughly 26.31 degrees. Is the angle to rotate by in degrees or in radians?Yunnosch
I actually just want to rotate the x/y of the bigger box around the center of the smaller box. so the x/y point of the bigger box is relative to the top/left point of the smaller box. I have the Width and height of the smaller box so i can calculate y/x big box relative to the center of small box. The angle to rotate is in degrees. The rotation can be any degree, for example 10.Adnan Mujkanovic

3 Answers

0
votes

If you want to rotate a given point P around a point C, which are defined in the same coordinate system you can use a simple rotation matrix. Calculate the P coordinates with respect to C (subtraction), then apply rotation with the matrix and go back to original coordinates by adding C again.

enter image description here

1
votes

You can do as follows:

  • determine the angle by which you want to rotate, make sure it suitable for the trigonometric functions (sin(), cos(), ...), i.e. right angle is usually Pi/2
  • in case of rotating counterclockwise, it is negative
  • determine the coordinates of c, as cx,cy
  • process each of the corners of the rectanlge, one by one, for a total of four
  • for each corner P, currently at coordinates px,py and to move to px2,py2
    • determine angle between current P and C, using atan2(py-cy, px-cx)
    • to get from degrees to radians (for use with trigonometry) calculate radians=(pi*degrees)/180.0
    • add the desired rotation angle to that current angle, to get newangle
    • determine the distance of current P to C, sqrt((px-cx)(px-cx) + (py-cy)(py-cy))
    • multiply the distance (which is not changing by rotation), with the appropriate trigonometric function
    • px2 = distance * cos(newangle)
    • py2 = distance * sin(newangle)
0
votes

All that matters is the coordinates of the rotation center and the angle.

The most compact formulation is by means of complex numbers (of which I hope you have some understanding; you actually don't need a complex data type, you can expand the formulas).

Let C be the center and α the angle. Then for any point P, the image Q is given by

Q = (P - C) cis(α) + C

where cis(α) = cos(α) + i sin(α).

The inverse rotation is simply given by

P = (Q - C) cis(-α) + C.