I'm trying to create a member function called 'rotated' in class 'Point'. However when I try to rotate (1,0) by 180 degrees , I get the output as 0.00555556,-4.35155e-17 instead of (-1,0).
To maintain compatibility with all the other trig functions, I'd define the angle in rotated() to be in radians, not degrees. If someone wants to rotate by degrees, let them (the caller) do the conversion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Rotate "this" by "angle" radians and return the result
Point rotated (constdouble angle) {
Point r;
r.x = (x)*cos(angle) - (y)*sin(angle);
r.y = (x)*sin(angle) + (y)*cos(angle) ;
return r;
}
...
int main()
{
// example, replace with your tests
Point a(1,0);
Point rota;
rota=a.rotated(90*M_PI/180);
cout<<rota.x<<","<<rota.y<<'\n';
return 0;
}