弄清楚度数的方向

Figuring out direction of degrees

我可能想多了,但是,假设你有以下变量;

如何判断度数的方向?即 0->359 为顺时针,359->0 为逆时针。

这里有几个例子:

Starting at: 357 degrees
Ending at: 337 degrees
Incremented by: 20
Direction: Counterclockwise

Starting at: 10 degrees
Ending at: 350 degrees
Incremented by: 20
Direction: Counterclockwise

Starting at: 357 degrees
Ending at: 17 degrees
Incremented by: 20
Direction: Clockwise

你如何尽可能安全地确定方向?一种快速的肮脏解决方案是使用模块,但是,如果增加的数字偏离 1-2 度会怎样?

理想的解决方案 将是一种检测最接近增量数字的方向的方法。如果这没有意义,这里有一个例子:

Starting at: 17 degrees
Ending at: ~347-357 degrees
Incremented by: ~20-30
Direction: Counterclockwise

它不会是顺时针的,因为这个差异大约是 ~330-340。

如果不明白请告诉我,我会尽力改写。

计算 CW 和 CCW 的两个变体

E1 = (Start + Increment) %% 360
E2 = (Start + 360 - Increment) %% 360

并检查哪个变体与结束角度的绝对差异较小。

S:0; E:20; 

if I=20
CW: 0+20=20
CCW: 0 + 360-20=340
E is closer to CW result

if I=340
CW: 0+340=340
CCW: 0 + 360 - 340 = 20
E is closer to CCW result