简体   繁体   中英

Unwind angle to 360

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 ).

Try:

(360 + (angle % 360)) % 360

or:

(angle >= 0 ? 0 : 360) + angle % 360

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM