我的数学很烂,感觉这应该很简单,但我希望它是优雅的,而不是一个黑客。
我有一个360度的角度,需要计算到一个255度的旋转自由度.人输入角度,准确输出到一个255度的圆。
int north = 90;
float outputNorth = north/360f * 255f; //Results: 90 angle-> 63 angle
但它需要是。
int north = 90;
float outputNorth = north/xf* yf; //Results: 90 angle -> 128 angle
问题是255度的旋转自由度的坐标有点怪异 不知道怎么翻译。
Should be the following:
//90 -> 128
//0 -> 192
//270 -> 0
//180 -> 64
你的数学有一些四舍五入的问题。我会使用不同的映射,其中360度将是。256 -> 0
而不是 255
. 否则你的8位值会有相同的角度。0
存储两次,很可能会引起其他问题。
在这里使用 线性插值:
int i; // angle [deg int]
int o; // 0..255 [8bit uint]
o=(192-((i<<8)/360))&255;
i=((192-o)*360)>>8;
这里的数值。
i o
0 192
90 128
180 64
270 0
360 192
价值: i
可以是任何甚至负的或大于360度的转换仍将工作... ...
float outputNorth = (north+90)/360f * 255f;
是我要找的答案... ... -.-