Rectangle problem

Started by
8 comments, last by EvilNando 14 years, 7 months ago
Ive been trying to get a formula for finding some points of a rectangle no matter its current rotation. Ive managed to get it working by using trigonometry functions but this only works if the rectangle rotates along its top left corner Im have no idea how to accomplish this if the rectangle has to rotate around its center Here is an image of the problem So given this rectangle we always know: its upper left coordinate (P) its width and height (w & h) its rotation angle (Ө) How can I get the coordinates of points A and B? is it even possible or I have to rely on transformation matrices as my only solution? thank you
Advertisement
It's actually not that hard.

A = P + h*(sin(theta),cos(theta))
B = A + w*(cos(theta),-sin(theta))

EDIT: I didn't understand the part about rotating around the center. Didn't you say that part of what you know is P? Or do you mean "what P was before a rotation around the center"?
ah really?

Im curious how you get to that answer , could you explain that (if its not too much of a hassle)

Thanks
Quote:Original post by EvilNando
ah really?

Im curious how you get to that answer , could you explain that (if its not too much of a hassle)


It is a bit of a hassle. If you know what sine and cosine mean and if you think about it for a little while, you can probably understand it on your own. If you try for a couple of hours and don't get it, I'll try to explain. But it's much better if you put some effort.
I thought that was a given, sorry.

Coordinate system is the same like in a monitor

the upper left is (0,0)

I understand the trigonometry functions the problem I was having is that I cannot find a way to project a right triangle that would help me figure out the coordinates (thing that I was able to do with the rectangle when it was rotating along its top left corner)

Quote:Original post by alvaro

EDIT: I didn't understand the part about rotating around the center. Didn't you say that part of what you know is P? Or do you mean "what P was before a rotation around the center"?


awww I think you got me

I need to check out the program Im writing and draw a quick test and see where is P right now , since Im using a primitive drawing library I think P is still the UNROTATED top left position

need to confirm this

thanks for the heads up
Quote:Original post by EvilNando
Ive been trying to get a formula for finding some points of a rectangle no matter its current rotation.

Ive managed to get it working by using trigonometry functions but this only works if the rectangle rotates along its top left corner

Im have no idea how to accomplish this if the rectangle has to rotate around its center

Here is an image of the problem



So given this rectangle we always know:

its upper left coordinate (P)

its width and height (w & h)

its rotation angle (Ө)

How can I get the coordinates of points A and B? is it even possible or I have to rely on transformation matrices as my only solution?

thank you

Firstly, you didn't sketch your x and y axes meaning that your problem is undefined. Of course we'll all probably make the same assumptions, but that doesn't mean that your problem is well defined.
We can define a system of coordinates whose y axis has the center of the rectangle as its origin, is parallel to the vector AP( A is the origin of the vector AP ). The x axis is the perpendicular vector to this y axis, parallel to the vector AB and passing through the center of the rectangle. Now let's define another system of coordinates. Its y axis is parallel to the vertical lines of your drawing and oriented upward and its x axis is parallel to the horizontal ones and oriented forward.( sorry for any mistake, English is not my mother tongue )
We don't care about its origin, but we know that it has an origin. Let's call it the reference frame and the former one, the local frame.
Well, we can see that the x axis of the local frame makes an angle absolute_value( theta ) with the x axis of the reference frame and so do the y axis of the local frame with the y axis of the one of the reference frame.
Now, we know that if a frame F1 is the rotation of another frame F2( in 2D, the rotation is around the z axis ), then the components of vector v in frame F1( v1 ), knowing its components in frame F2( v2 ) and the angle of rotation( angle ) are:

v1 = v0 + M * v2

M being a matrix whose coefficients are:
M11 = cos( angle )
M12 = sin( angle )
M21 = -sin( angle )
M22 = cos( angle )

and v0, the components of the origin of the system of coordinates F2, in the frame F1.

Let's substitue:
F1 : reference frame
F2 : local frame

Here the angle of rotation is -absolute_value( theta )
Why minus? Because "we go clockwise from local x axis to reference x axis".

The components of A in the local frame are:
A1x = -w/2
A1y = -h/2

So its components in the reference frame will be:
A2 = O + M * A1

O: center of the rectangle. Coordinates expressed in the reference frame

=>A2x = Ox + cos( -absolute_value( theta ) ) * ( -w/2 ) + sin( -absolute_value( theta ) ) * ( -h/2 )
<=>A2x = Ox - cos( absolute_value( theta ) ) * w/2 + sin( absolute_value( theta ) ) * ( h/2 )

=>A2y = Oy - sin( -absolute_value( theta ) ) * ( -w/2 ) + cos( -absolute_value( theta ) ) * ( -h/2 )
<=>A2y = Oy - sin( absolute_value( theta ) ) * w/2 - cos( absolute_value( theta ) ) * h/2

[Edited by - johnstanp on September 9, 2009 7:45:41 AM]
Of course, it can be done with simple trigonometry, but when you do it that way, it's a hassle when you are not focussed.
aww my head hurts!

so basically what youre doing applation a rotation matrix first then adding the rectangle dimensions?

Im going to re-read this slooowly and see what I can come up with

Solved

thank you for explaining things ! LOL the only thing I could really get from all of that is that I needed matrices to get correct results!

but thank you!

the solution in 3 lines of code:

point_a = point_a - rectangle_position;
point_a = Vector2.Transform(point_a,Matrix.CreateRotationZ(rectangle_rotation));
point_a += rectangle_position;


This topic is closed to new replies.

Advertisement