Normalise orientation between 0 and 360

前端 未结 9 1601
不知归路
不知归路 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;
    
    0 讨论(0)
  • 2021-01-30 11:56

    formula for re-orienting circular values i.e to keep angle between 0 and 359 is:

    angle + Math.ceil( -angle / 360 ) * 360
    

    generalized formula for shifting angle orientation can be:

    angle + Math.ceil( (-angle+shift) / 360 ) * 360
    

    in which value of shift represent circular shift for e.g I want values in -179 to 180 then it can be represented as: angle + Math.ceil( (-angle-179) / 360 ) * 360

    0 讨论(0)
  • 2021-01-30 11:57

    I sort of quickly mocked this up in AS3, but should work (you may need += on the angle)

    private Number clampAngle(Number angle)
    {
        return (angle % 360) + (angle < 0 ? 360 : 0);
    }
    
    0 讨论(0)
提交回复
热议问题