Jump to content
  • Advertisement
Sign in to follow this  
Josheir

C++ 2D Math Problem

Recommended Posts

In the following code:


 

Point p = a[1];  center of rotation

for (int i = 0; I<4; i++)
{
 	int x = a.x - p.x;
 	int y = a.y - p.y;
 	
   	a.x = y + p.x;
   	a.y = - x + p.y;
}

 

I am understanding that a 90 degree shift results in a change like:   

xNew = -y

yNew = x

 

Could someone please explain how the two additions and subtractions of the p.x and p.y works?

 

Thank you,

Josheir

Edited by Josheir

Share this post


Link to post
Share on other sites
Advertisement
xNew = -y
yNew = x

This rotates a point/vector around the origin by 90 degrees

The original code snippet does the same (similar -- they're mirrored, so if using the same coordinate system it would be the reverse direction), except it rotates about a point other than the origin, which in this case is the center of the rectangle. It's effectively the same as rotating around the origin, except it offsets by the center point's position in order to treat the center point as the origin.

 

EDIT: for clarity, the two subtractions move the rectangle points to a position relative to the world origin that is the same as their position relative to their center point. Then they are rotated around the origin, then moved back (the two additions) to be relative to the center point instead of the world origin.

Edited by cmac

Share this post


Link to post
Share on other sites

So, is this correct:  The first two p's subtract the offset to regard the point not being the origin.  Than the computation is done again and p's are added to re translate it back to the origin.

 

The array is four integers held to be a shape of a grid of 8 cells (two columns,)  It's not really a rectangle.

 

I was playing around with the values (mirrored or not:)

xNew = -y

yNew = x

and got it working luckily.  I find it confusing that the second set of additions uses the first set that subtracts.  I think I've got it now but was hoping someone could do a simple explanation if there is anything else to explain!

 

The way that I got it working was I drew a circle with p.y in the center and a.y on the circumference with the difference being what I assumed to be  the y.  Likewise for the x values.

 

Thank you,

Josheir

 

 

 

Edited by Josheir

Share this post


Link to post
Share on other sites

If you know how to rotate around the origin, you can rotate around any point P like this:

  • translate the whole picture so P becomes the origin,
  • rotate around the origin,
  • translate back so the origin becomes P

 

Does that help?

 

Share this post


Link to post
Share on other sites

Yes, all that helps; I'm pretty sure I understand it now.  I am a bit confused by the diagram though.  I'll go over my program again tomorrow but I do understand what you mean.  I need to translate this understandable concept into the understandability of the idea for a specific program.  Like I said, it's not really a rectangle, it's more complicated.

Thanks again,

Josheir

Share this post


Link to post
Share on other sites

Black dot = polygon center, red line = subtraction, green line = rotation, blue line = addition. As you can see, the point ends up at the desired point (90 degrees rotated around center).

Overall Alvaro's explanation was much more elegant and concise than mine, so just focus on that.

Share this post


Link to post
Share on other sites

The subtraction (red line) translates the picture so that the polygon center has become (0,0).

Edited by cmac

Share this post


Link to post
Share on other sites

So in the image, there is P and center. P is a distance from center. The red line is P - C, this puts P a distance from origin. Green line is a rotation around origin = Pr. Blue line is Pr + C, moves the rotated point back out to be around center, and is the final rotated point.  

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  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!