Reputation: 47
One way to do this is:
Convert original vector to polar co-ord
Rotate by set amount
Convert back to cartesian
Is there a cleaner method, or one that doesnt have to do these conversions
Upvotes: 0
Views: 75
Reputation: 2681
Yes, multiply x and y by sin and cos of the angle. This is how I do in C++:
class Rotate2D
{
public:
float cosA, sinA;
Rotate2D(const float radRot, const float scale=1.0f)
: cosA(scale*cos(radRot)), sinA(scale*sin(radRot)){};
template<typename T>
inline Point2DF map(const T &pt) const
{
return Point2DF(cosA*pt.cx() - sinA*pt.cy(),
sinA*pt.cx() + cosA*pt.cy());
}
};
This code rotates and optionally scales in 2D. In 3D is about the same thing. Scaling comes for almost free, so there is almost no reasons to not have it.
However, I'd highly recommend for you to use Quaternion library to rotate 3D points.
Upvotes: 1
Reputation: 9481
Of course there is. Look at the rotation matrices https://en.wikipedia.org/wiki/Rotation_matrix.
For two dimensions the matrix is very simply it is just
| cos A - sin A |
R = | |
| sin A cos A |
Where A is the angle by which you want to rotate your vector
Once you composed a matrix like that multiply it by your vector and you will have the vector rotated by the amount 'A'
For two dimension use the one at the beginning of the article, for higher dimension google is your friend.
With a bit of tweaking you can extend this technique to scaling, moving (translating) and shear transformations.
Note that the multiplying two dimensional vector by 2x2 matrix results into the same operations you have to outlined in your method. It is just much cleaner way to thing about things. And it makes it much easier when number of dimensions goes beyond 2.
Upvotes: 1