I''m trying to rotate a 2d vector by an angle. So my equation would be y = sin(a + b), x = cos(a + b).. I think.

Using those fun identities I came up with this:
y = y * cos(angle) + x * sin(angle)
x = x * cos(angle) - y * sin(angle)
So I wrote this simple little program:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float angle;
float x, y;
cout << "Enter an x y value: ";
cin >> x >> y;
cout << "Enter a rotation angle: ";
cin >> angle;
cout << "New x and y: " << (x * cos(angle) - y * sin(angle)) << " " << (y * cos(angle) + x * sin(angle)) << endl;
return 0;
}
So I expect when I enter 1, 1 as the vector, and rotate by 180 degrees, I should get -1, -1. But instead, I get these results:
Enter an x y value: 1 1
Enter a rotation angle: 180
New x and y: 0.202693 -1.39961
That''s WAY off!! What could I be doing wrong? Is the error in my formula? In the program? Rounding errors? Those are some pretty darn big rounding errors if so. Any ideas?
aut viam inveniam aut faciam
MoonStar Projects