How to wrap around a range

后端 未结 7 836
夕颜
夕颜 2020-12-05 04:39

Angles in my program are expressed in 0 to 2pi. I want a way to add two angles and have it wrap around the 2pi to 0 if the result is higher than 2pi. Or if I subtracted an a

7条回答
  •  粉色の甜心
    2020-12-05 05:18

    You can use something like this:

    while (angle > 2pi)
        angle -= 2pi;
    
    while (angle < 0)
        angle += 2pi;
    

    Basically, you have to change the angle by 2pi until you are assured that it doesn't exceed 2pi or fall below 0.

提交回复
热议问题