• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Trigonometry: Rotating a point

## Recommended Posts

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.

##### Share on other sites
Something like this, maybe?

  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!

##### Share on other sites
Fruny    1658
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...

##### Share on other sites
Qatal    127
well, here we go.

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

##### Share on other sites
LilBudyWizer    491
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.

##### Share on other sites
So what you''re all telling me is that I don''t have to convert theta from degrees to radians first of all?

##### Share on other sites
Fruny    1658
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...