Another way for 2D Rotation?

Started by
29 comments, last by nerco666 14 years, 1 month ago
I completed my one method for 2D Rotation, but I was wondering if there is a better or shorter method for 2D Rotation. The shortest I've found is cos(theta)*x - sin(theta)*y; sin(theta)*x + cos(theta)*y; I just want to know if there is a shorter way to acomplish this. Thanks! N
Advertisement
If rotating by a specific angle is what you want to do then not really, but you can precalculate those trig functions and put them in a matrix and then rotation becomes a single matrix multiplication. It makes code a bit more readable and runs faster if you frequently rotate by the same angles(since you don't calculate the sin/cos again). Then if you use a 3x3 matrix you can put translation into matrices as well and string those together with rotations,scaling,skewing, etc, all into a single local to world space matrix for each object. Mathematically speaking it's equivalent, but storing and manipulating all your transformations as matrices makes things appear cleaner and simpler.

For specific problems angles can be avoided altogether in favor of vector solutions, but I'd need more information about why you're rotating to give any more useful advice.
The rotation is absolutely fine and there is no shorter way.

Why would you want to have a shorter way? Efficiency? Don't temper with early optimization. It will only slow down your programming progress. Computers (and also Smartphones) are fast enough for such computations.

Cheers
-- blog: www.fysx.org
If you are familiar with complex numbers, 2D rotation can also be expressed as a the unit-length complex number r=cos(alpha)+i*sin(alpha). In order to apply the rotation to a point, write the point as a a complex number z=x+i*y and simply compute z*r.

Another formula for r is exp(i*alpha), but don't worry about it if you don't understand it.

In the end, the operations are exactly the same, but thinking about it in terms of complex numbers helps me remember the formula better, and can make the code cleaner.

Thanks for your replies, but could I ask for a basic example of using complex numbers in rotations?


"r=cos(alpha)+i*sin(alpha)"
Would I apply that formula for both X and Y drawing?

Example:

d[0] = a[0] - b[0];
d[1] = a[1] - b[1];

float r[2];

r[0] = d[0] * (cos(c) + I * sin(c));
r[1] = d[1] * (cos(c) + I * sin(c));

I don't think I am understanding this very well..But would you give me a brief explanation?

Thanks,

N
#include <iostream>#include <complex>#include <cmath>std::complex<double> const I(0.0,1.0);double const PI=std::atan(1.0)*4.0;int main() {  std::complex<double> z(10.0,0.0);  std::complex<double> r = std::exp(PI/4*I);    std::cout << "Original point: " << z << '\n';  std::cout << "Rotated point: " << (z*r) << '\n';}


EDIT: If you still need an explanation, ask again.
Hey thank you for your help, but I still think I would like an explanation.

Thanks,

N
I googled this because I had no clue how complex number multiplication worked. Here's a short primer on it. You only need to read up to the red equation, really.

However, if you simplify that, you basically get the very same equations that you had in your first post. Which makes sense, I guess.
If you think of complex numbers in their polar form, multiplication multiplies the moduli and adds up the arguments. Therefore, multiplying z by a complex number r with modulus 1 will result in a complex number with the same modulus as z and an argument that is that of z plus that of r. That's precisely the rotation you want.

You can verify this easily:
z = x + yir = cos(alpha) + sin(alpha)*iz*r = (x + yi) * (cos(alpha) + sin(alpha)*i) = x*cos(alpha) - y*sin(alpha) + (x*sin(alpha) + y*cos(alpha))*i


You can see that the real and imaginary parts of the result are precisely what you posted at the top of the thread.

Euler's identity says that exp(i*alpha) = cos(alpha) + sin(alpha)*i, which my code exploits. Of course, the implementation will probably use cos() and sin() under the hood, so it's not going to be any faster. I find it a little cleaner, though.

Thank you guys for your help.

Btw assuming "yi" is the difference between the 2 points on the y axis should i negate it since "i = negative exponent"

so a.y - - b.y = a.y + b.y..

Thanks!

[Edited by - nerco666 on March 15, 2010 8:35:17 PM]

This topic is closed to new replies.

Advertisement