Smallest angle between two vectors?

2k Views Asked by At

I have a robot and I am going to turn it clockwise (negative degrees) or counterclockwise (positive degrees). If I turn the robot -270 degrees, that is the same as turning +90 degrees. Is there a way I can calculate the smallest angle in terms of magnitude to take every time? Is there a function that will take an input of degrees and returns the smallest (in terms of magnitude) angle to get to that target heading. So, if I input -200, the function returns +160.

Also, the modulus cannot be used because the input degrees can be a decimal and not a whole integer.

Here is what I have so far, I does not work for some reason:

1

There are 1 best solutions below

3
On BEST ANSWER

EDIT: try small_angle = acos(cos(angle))*sign(sin(angle))

Even if it works, you can spend less processing with an if-else...

EDIT2: On Python, for example, it may be more elegant to do small_angle=atan2(sin(angle),cos(angle))

Notice that atan2 is different from atan.