I can't help but feel like a noob once again, so I need to ask this before my head goes through the wall.

I'm working on a rotation algorithm. Basically, I have an angle (in rad) and a target angle (in rad) and I need to figure out whether the shortest rotation between the two is clockwise or counter-clockwise.

I've devised a bunch of algorithms, but all of them with complex exceptions to handle the wrap of the angle.

My circle is -Pi to Pi (where Pi and -Pi occupy the exact same position, which is the opposite to 0).

Anybody got a clue how to solve this 'elegantly'?

Thanks

-=- My Articles-=-Getting Gones Done - Method and tools on how to start a hobby project and get it Done!

Yes: Stop using angles. If you represent orientations by unit-length complex numbers, in order to rotate from z1 to z2, you need to multiply by z2/z1. Whether that rotation is achieved going clockwise or counterclockwise depends on the sign of the imaginary part of z2/z1.

EDIT: Oh, since z1 has length 1, you can compute z2/z1 as z2*conj(z1).

The point is, if possible, to not use angles in the first place. The need for trigonometry may just be the result of using the wrong tool, and the correct solution may not involve any trigonometry at all if done correctly to begin with. That's why Alvaro gave the advice to stop using angles.

You can do it with some if statements (and probably some abs()) if you can't fmod or fmodf in your language.

Write down some cases (try with angles from all 4 quadrants), work out the expected answer for each combination, and work out the pattern. You need to work out how to handle the "shortest angle crosses the discontinuity" case.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley