Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unwind angle to 360

Tags:

c++

angle

How can I unwind an angle to result in an angle in [0, 360)?
I can do this:

int unwind(int angle)
{
    while(angle < 0) angle += 360;
    while(angle >= 360) angle -= 360;
}

But I'm pretty sure there is a way to do this without loops. I also tried angle % 360 but that doesn't work for negative angles (-60 % 360 == -60).

like image 242
Dani Avatar asked Sep 03 '11 00:09

Dani


1 Answers

Try:

(360 + (angle % 360)) % 360

or:

(angle >= 0 ? 0 : 360) + angle % 360
like image 79
Lie Ryan Avatar answered Sep 18 '22 17:09

Lie Ryan