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


Andrea Richards

I've searched all the links for rectangle rotation, but nothing seems to apply to my problem. I have a RectangleF structure that I want to input into a rotation matrix. Then use the resulting RectangleF to pass to other functions.

The reason for wanting to use a matrix is ​​because I might also perform translation, then scale, and then pass the resulting rectangle to other functions like

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 the screen or anything else. I only need these values, but all the examples I read use the graphics class for transforming and drawing, which is not what I want.

thank you very much

Tawa

The System.Drawing.Rectanglestructure is always orthogonal and cannot be rotated. You can only rotate its corner points.

Here is an example of doing this using 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 that contains Pointor PointFcontains all the points you're interested in , here the four corners; you can Matrixthen transform those points based on various things you ask for, rotation around a point is one of them. Others include zoom , cut and pan .

The result is as expected:

enter image description here

If you need this functionality repeatedly, you need to create a function that converts a Rectangle to a Point[] and back.

Note that the latter is not actually possible as mentioned above, since it Rectangleis always orthogonal, i.e. cannot be rotated , so you have to look for the corners. Or switch to the class Rectfrom the namespace System.WindowsQuergo shows in its post .

Related


How to rotate rectangle in Libgdx?

Kevin Bryan I rotated the sprite 90 degrees and wanted to do the same with the rectangle to make it work for collisions, but that rotate()method doesn't work for rectangles. Here is what I did: treeSpr=new Sprite(new Texture(Gdx.files.internal("tree.png")));

How to rotate rectangle in Libgdx?

Kevin Bryan I rotated the sprite 90 degrees and wanted to do the same with the rectangle to make it work for collisions, but that rotate()method doesn't work for rectangles. This is what I did: treeSpr=new Sprite(new Texture(Gdx.files.internal("tree.png")));

How to rotate rectangle in Libgdx?

Kevin Bryan I rotated the sprite 90 degrees and wanted to do the same with the rectangle to make it work for collisions, but that rotate()method doesn't work for rectangles. Here is what I did: treeSpr=new Sprite(new Texture(Gdx.files.internal("tree.png")));

How to rotate rectangle in Libgdx?

Kevin Bryan I rotated the sprite 90 degrees and wanted to do the same with the rectangle to make it work for collisions, but that rotate()method doesn't work for rectangles. This is what I did: treeSpr=new Sprite(new Texture(Gdx.files.internal("tree.png")));

How to rotate a rectangle?

Joe Hot 200 I'm trying to rotate a (Java class) Rectanglecreated using Rectangle rect = new Rectangle(x, y, width, height); I can't seem to find anything in the API. If I google "Java rotate rectangle" I can only find threads that tell me how to rotate the dr

How to rotate rectangle in Libgdx?

Kevin Bryan I rotated the sprite 90 degrees and wanted to do the same with the rectangle to make it work for collisions, but that rotate()method doesn't work for rectangles. Here is what I did: treeSpr=new Sprite(new Texture(Gdx.files.internal("tree.png")));

How to rotate a rectangle?

Joe Hot 200 I'm trying to rotate a (Java class) Rectanglecreated using Rectangle rect = new Rectangle(x, y, width, height); I can't seem to find anything in the API. If I google "Java rotate rectangle" I can only find threads that tell me how to rotate the dr

Can't rotate rectangle in canvas

Rajib I am new to HTML5 canvas. I'm trying to build a game area using an HTML canvas, where someone can drive around a car inside the Canvas area, like if I press the left arrow key, the car will turn to the left, and if the right arrow key is pressed, the car

What is this blue rectangle thing for? How can I get rid of it?

Harim Ahmed I'm new to Android Studio and don't know what I'm doing. Just took a course on Udemy and it doesn't have that weird blue square, so how do I get rid of it? Thunder King | This is called a blueprint view. The first 3 buttons of the Layout Editing ta