Sign in to follow this  
EvilNando

how do I modify a rotated rectangle

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 world
transform_matrix = Matrix.CreateTranslation(new Vector3(origin.X, origin.Y, 0)) * Matrix.CreateRotationZ(rotation);

aux_origin = Vector2.Transform(aux_origin, transform_matrix);

[/SOURCE]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this