这是一个简单的算法:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int rotate_diff(int a, int b, bool * clockwise);
int main(void) {
int degrees_rotated, a, b;
bool clockwise;
a = 4040;
b = 1060;
degrees_rotated = rotate_diff(a, b, &clockwise);
printf("A = %d, B = %d, rotation = %d degrees, direction = %s\n",
a, b, degrees_rotated,
(clockwise ? "clockwise" : "counter-clockwise"));
return EXIT_SUCCESS;
}
int rotate_diff(int a, int b, bool * clockwise) {
static const int min = 2000;
if ( a <= min ) {
a += 4096;
}
if ( b <= min ) {
b += 4096;
}
int degrees_rotated = b - a;
if ( degrees_rotated > 0 ) {
*clockwise = false;
} else {
degrees_rotated = -degrees_rotated;
*clockwise = true;
}
return degrees_rotated * 360 / 4096;
}
请注意,这会给您行进的度数,而不是行进的距离,因为您没有告诉我们轴的尺寸是多少。要获得经过的距离,显然将周长乘以经过的度数除以 360。如果您的点 0 到 4095 是某种已知单位,则只需跳过上述算法中的度数转换,并相应地更改变量名称。