Andrea Richards
Andrea Richards

Reputation: 63

How can I rotate a rectangle using a matrix and get the modified rectangle?

I have searched all the links for rectangle rotation, but nothing seems to apply to my problem. I have a RectangleF structure and wish to feed it into a rotation matrix. Then use resulting RectangleF to pass into some other function.

The reason for wanting to use a matrix is because I may also want to perform a translation, and then perhaps a scale afterwards and pass the resultant rectangle into some other function, e.g.

RectangleF original = new RectangleF(0,0, 100, 100);
Matrix m = new Matrix();
m.Rotate(35.0f);
m.Translate(10, 20);

....   (what do I do here ?)

RectangleF modified = (How/where do I get the result?)

SomeOtherFunction(modified);

How can I achieve this ?

I don't want to draw this rectangle on a screen or anything else. I just need the values, but all examples I have read use the graphics class to do the transform and draw which is not what I want.

Many thanks

Upvotes: 3

Views: 3759

Answers (2)

TaW
TaW

Reputation: 54433

The System.Drawing.Rectangle structure is always orthogonal and cannot have a rotation. You can only rotate its corner points.

Here is an example of doing this with a Matrix:

Matrix M = new Matrix();

// just a rectangle for testing..
Rectangle R = panel1.ClientRectangle;
R.Inflate(-33,-33);

// create an array of all corner points:
var p = new PointF[] {
    R.Location,
    new PointF(R.Right, R.Top),
    new PointF(R.Right, R.Bottom),
    new PointF(R.Left, R.Bottom) };

// rotate by 15° around the center point:
M.RotateAt(15, new PointF(R.X + R.Width / 2, R.Top + R.Height / 2));
M.TransformPoints(p);

// just a quick (and dirty!) test:
using (Graphics g = panel1.CreateGraphics())
{
    g.DrawRectangle(Pens.LightBlue, R);
    g.DrawPolygon(Pens.DarkGoldenrod, p );
}

The trick is to create an array of Point or PointF containg all points you are interested in, here the four corners; the Matrix can then transform those points according to all sorts of things you asked for, rotation around a point being one of them. Others include scaling, shearing and translating..

The result, as expected:

enter image description here

If you need this repeatedly you will want to create function to convert Rectangle to Point[] and back.

Note, as pointed out above, that the latter is not really possible, as Rectangle will always be orthogonal, i.e. cannot be rotated, so you will have to go for the corner points. Or switch to the Rect class from the System.Windows namespace as Quergo shows in his post.

Upvotes: 5

Quergo
Quergo

Reputation: 928

Use Rect if you can/want use System.Windows namespace. Rect is also always orthogonal but you can apply a rotate transform to its corner points. It is same procedure as using System.Drawing namespace.

var rect = new Rect(0, 0, 100, 100);
Point[] cornerPoints = { rect.TopLeft, rect.TopRight, rect.BottomRight, rect.BottomLeft };

var m = new Matrix();

//define rotation around rect center
m.RotateAt(45.0, rect.X + rect.Width / 2.0, rect.Y + rect.Height / 2.0);

//transform corner points
m.Transform(cornerPoints);

Upvotes: 0

Related Questions