Normalise orientation between 0 and 360

前端 未结 9 1632
不知归路
不知归路 2021-01-30 10:58

I\'m working on a simple rotate routine which normalizes an objects rotation between 0 and 360 degrees. My C# code seems to be working but I\'m not entirely happy with it. Can a

9条回答
  •  梦谈多话
    2021-01-30 11:51

    Add any multiple of 360 degrees between which your possible input values could be (to take it above zero), and just take the remaining with %, like this

    angle = 382;
    normalized_angle = (angle+3600) %360;
    //result = 22
    

    The case above can take input angles down to -3600. You can add any number (multiple of 360) crazily high that would make the input value positive first.

    Usually during an animation, your previous frame/step value would probably be already normalized by the previous step, so you'll be good to go by just adding 360:

    normalized_angle = (angle+360) %360;
    

提交回复
热议问题