how do I modify a rotated rectangle

Started by
2 comments, last by EvilNando 13 years, 4 months ago
Im trying to modify a rectangle's height while maintaining its position

its pretty easy to do this while the rectangle is not rotated but Im having lots of trouble when I have to deal with rotated rectangles


example of what I want to do
[SOURCE]    (X,Y)_______   ________	 |     |   |      |	 |     |   |      |	 |     |   |      |	 -------   |      |	           |      |	 before    |      |	           |      |	           --------			   	           after			   note: the top-left coordinate remains the same after the transformation process[\SOURCE]
Advertisement
For situations like this, the optimal solution is usually to work with a local-space copy of the geometry rather than a world-space copy.

However, if you need to modify a world-space copy of the rectangle, one way to do it is as follows. Convert to rectangle to a position (which can be a corner if you want), an orthonormal basis (two mutually perpendicular unit-length vectors), and a height and width. You can derive this information from the vertices by subtracting the appropriate vertices from each other and computing the lengths of the edges.

Once you have the rectangle in this form, you can adjust the width and height as desired, and then rebuild the vertices from the modified data.

There are other ways it can be done that might be more appropriate to the context, but they'll likely be at least somewhat similar to what I described above.
If we are only dealing with 90 degree rotations then it is not that hard.

If you want to do, rotations of smaller then that, well it gets a bit more complicated.

So for nowI am assuming 90 degree.

Just swap the width and height. That is all ya need to do. And then draw as before.

theTroll
Ive managed to get it to local coords

how do I return it to world coords?

[SOURCE]//go local Matrix transform_matrix = Matrix.CreateTranslation(new Vector3(-origin.X, -origin.Y, 0)) * Matrix.CreateRotationZ(-rotation);Vector2 aux_origin = Vector2.Transform(new Vector2(origin.X, origin.Y), transform_matrix);// DO SOME LOCAL TRANSFORMS HERE...//return to worldtransform_matrix = Matrix.CreateTranslation(new Vector3(origin.X, origin.Y, 0)) * Matrix.CreateRotationZ(rotation);aux_origin = Vector2.Transform(aux_origin, transform_matrix);[/SOURCE]

This topic is closed to new replies.

Advertisement