开发者

Unwind angle to 360

开发者 https://www.devze.com 2023-04-02 07:56 出处:网络
How can I unwind an angle to 开发者_开发百科result in an angle in [0, 360)? I can do this: int unwind(int 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).


Try:

(360 + (angle % 360)) % 360

or:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号