Trigonometry: Rotating a point
This should be an easy one for you guys. I''ve found some formulas for doing this, but I must be doing something wrong, since my code never works the way it should. Lets pretend I have this class:
typedef class _POINT2DF
{
float x, y;
void rotateAbout(float theta, float aboutX, aboutY)
{
//WORK, YOU STUPID CODE!
}
}POINT2DF, *POINT2DFp;
Now the question is, what should I be putting in the rotateAbout() function, to be able to rotate the x y members of the point about (around) another point (aboutX, aboutY)? Thanks in advance.
Something like this, maybe?
I''m assuming that theta means angle (in degrees). Anyway, it should work - I may have the negative signs all mixed up...
Hope this helps
---------------
#define TRUE 0
#define FALSE 1
//MUAHAHAHAHAAAA!
void rotateAbout(float theta, float aboutX, float aboutY){ x = aboutX + -cosf(theta); y = aboutY + sinf(theta);}
I''m assuming that theta means angle (in degrees). Anyway, it should work - I may have the negative signs all mixed up...
Hope this helps

---------------
#define TRUE 0
#define FALSE 1
//MUAHAHAHAHAAAA!
Substract the coordinates of the point you are rotating around
Rotate by the appropriate angle ( x=x*cos(a)-y*sin(a), y=x*sin(a)+y*cos(a))
Add the coordinates of the point you are rotating around
[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]
Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
Rotate by the appropriate angle ( x=x*cos(a)-y*sin(a), y=x*sin(a)+y*cos(a))
Add the coordinates of the point you are rotating around
[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]
Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
well, here we go.
works good for me.
die or be died...i think
void rotateAbout(float theta, float aboutX, float aboutY){x = x - aboutX + cosf(theta); y = y - aboutY + sinf(theta);}
works good for me.
die or be died...i think
die or be died...i think
No, you need to use the trig identities for angle addition. Conceptually you start with a point (r*cos(alpha),r*sin(alpha)) and are transforming it to (r*cos(alpha+theta),r*sin(alpha+theta)). r*cos(alpha+theta) = r*cos(alpha)*cos(theta)-r*sin(alpha)*sin(theta) = x*cos(theta)-y*sin(alpha) since x=r*cos(alpha) and y=r*sin(alpha). It works similarly for the y component except you use the identity for sine. To rotate around a point you have to subtract before the rotation and add it back after the rotation. So x''=(x-cx)*cos(theta)-(y-cy)*sin(alpha)+cx.
Keys to success: Ability, ambition and opportunity.
So what you''re all telling me is that I don''t have to convert theta from degrees to radians first of all?
Of course you do. The documentation clearly states that sin() and cos() need their arguments in radians.
[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]
Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]
Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement