3

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

2 Answers2

4

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.

TaW
  • 53,122
  • 8
  • 69
  • 111
0

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);
Quergo
  • 888
  • 1
  • 8
  • 21
  • Still an orthogonal Rect: `transformed = {-164.142,-15.858,200,200}` – r2d2 Apr 18 '20 at 20:34
  • transformed = {X, Y, Width, Height} gives you no information about orthogonality. Check the corner points TopLeft and BottomRight instead. Width and Height are not changed by a rotation. – Quergo Apr 19 '20 at 07:09
  • 1
    The OP asked for the rotation of the corner points. The accepted answer of TaW is the right solution. As I tested `original.Transform(m)` will change the width and height. It modifies orginal to `{-70.711,20,141.421,141.421}`. It will stay orthogonal to the coordinate sytem, with TopLeft.Y = TopRight.Y. I guess it's the surrounding rectangle of the transformed rectangle the OP expected. – r2d2 Apr 20 '20 at 12:37
  • You are right. Changed the code sample to transformation of corner points. This seems to do the work. – Quergo Apr 20 '20 at 20:57