/* the table of sin() array from 0 to 45*/
short sin_table[] = {
,0
,175
,349
,523
,698
,872
,1045
,1219
,1392
,1564
,1736
,1908
,2079
,2250
,2419
,2588
,2756
,2924
,3090
,3256
,3420
,3584
,3746
,3907
,4067
,4226
,4384
,4540
,4695
,4848
,5000
,5150
,5299
,5446
,5592
,5736
,5878
,6018
,6157
,6293
,6428
,6561
,6691
,6820
,6947
,7071
};
/* the table of cos() array from 0 to 45*/
short cos_table[] = {
,10000
,9998
,9994
,9986
,9976
,9962
,9945
,9925
,9903
,9877
,9848
,9816
,9781
,9744
,9703
,9659
,9613
,9563
,9511
,9455
,9397
,9336
,9272
,9205
,9135
,9063
,8988
,8910
,8829
,8746
,8660
,8572
,8480
,8387
,8290
,8192
,8090
,7986
,7880
,7771
,7660
,7547
,7431
,7314
,7193
,7071
};
/* get the value of 10000 times of sin() */
short sin_q(unsigned short Angle)
{
if(Angle<=45) return sin_table[Angle];
else if(Angle>45&&Angle<=90) return cos_table[90-Angle];
else if(Angle>90&&Angle<=135) return cos_table[Angle-90];
else if(Angle>135&&Angle<=180) return sin_table[180-Angle];
else if(Angle>180&&Angle<=225) return -sin_table[Angle-180];
else if(Angle>225&&Angle<=270) return -cos_table[270-Angle];
else if(Angle>270&&Angle<=315) return -cos_table[Angle-270];
else if(Angle>315&&Angle<=360) return -sin_table[360-Angle];
return 0;
}
/* get the value of 10000 times of sin() */
short cos_q(unsigned short Angle)
{
if(Angle<=45) return cos_table[Angle];
else if(Angle>45&&Angle<=90) return sin_table[90-Angle];
else if(Angle>90&&Angle<=135) return -sin_table[Angle-90];
else if(Angle>135&&Angle<=180) return -cos_table[180-Angle];
else if(Angle>180&&Angle<=225) return -cos_table[Angle-180];
else if(Angle>225&&Angle<=270) return -sin_table[270-Angle];
else if(Angle>270&&Angle<=315) return sin_table[Angle-270];
else if(Angle>315&&Angle<=360) return cos_table[360-Angle];
return 0;
}
/*注意函数把结果放大了10000倍,函数虽然简单,因为消除了浮点计算,在需要频繁使用sin和cos,
但对精度要求不是太高时可以表现的很高效。曾利用上函数在一个没有图形函数的平台上实现画图功能,
达到了理想效果*/

本文介绍了一种通过预置的正弦和余弦值表格来高效计算角度对应的正弦和余弦值的方法。该方法避免了浮点运算,适用于对精度要求不高但需要频繁调用正余弦函数的场合。
4790

被折叠的 条评论
为什么被折叠?



