# How to rotate a rectangle?

I can easily rotate sprites using the Draw method. However I would like to simply rotate a Rectangle (the structure) so that I can get the coordinate of the four corners of the rectangle. How can I do that?

-

You can't rotate a Microsoft.Xna.Framework.Rectangle. It is by nature an axis aligned structure only.

What do you hope to do with the corners that you want to find? Maybe we could suggest a good way to accomplish your goal.

-

You can make for all 4 points something like this:

``````public static Vector2 RotateAround(this Vector2 source, Vector2 target, float radians)
{
Vector2 fromAround = source - target;
float postRotationX = (float)(fromAround.X * Math.Cos(radians) - fromAround.Y * Math.Sin(radians));
float postRotationY = (float)(fromAround.X * Math.Sin(radians) - fromAround.Y * Math.Cos(radians));
return target + new Vector2(postRotationX, postRotationY);
}
``````
-
Pretty sure that rotation should have a `+` before the `Y * cos` term. – Andrew Russell Jun 17 '11 at 1:32

If you want to get the coordinates of the rotated corners, you have to transform the coordinates from the non-rotated space to the rotated space. In other words, you have to multiply them by a matrix. I'm not sure the exact xna syntax, but it should be something like this:

``````float angle = 60;
Matrix rotate = Matrix.CreateRotationZ(angle);
Vector2 transformedPoint = Vector2.Transform(unTransformedPoint, rotate);
``````

This will rotate the rectangle about it's top left corner. I'm not sure if that's what you want or not, but you can easily change this by adding a translate transform before the rotate transform to change the rotation origin.

-