DSP

查表法实现三角函数

2019-07-13 18:18发布

 开发新游戏时,由于MTK不支持浮点运算,所以没有自带的三角函数,而游戏中这些是很重要的函数,会经常用到,所以自己参考相关文章,利用软件查表法,重新写了这些函数,我们看代码。正弦函数与余弦函数求出来的值,在与相关数值相乘后,必须除以4096才能是正确的值 int DSIN_TABLE[91] = {0,71,142,214,285,356,428,499,570,640,711,781,851,921,990,1060,1128,1197,1265,1333,1400,1467,1534,1600,1665,1731,1795,1859,1922,1985,2047,2109,2170,2230,2290,2349,2407,2465,2521,2577,2632,2687,2740,2793,2845,2896,2946,2995,3043,3091,3137,3183,3227,3271,3313,3355,3395,3435,3473,3510,3547,3582,3616,3649,3681,3712,3741,3770,3797,3823,3848,3872,3895,3917,3937,3956,3974,3991,4006,4020,4033,4045,4056,4065,4073,4080,4086,4090,4093,4095,4096};
int DCOS_TABLE[91] = {4096,4095,4093,4090,4086,4080,4073,4065,4056,4045,4033,4020,4006,3991,3974,3956,3937,3917,3895,3872,3848,3823,3797,3770,3741,3712,3681,3649,3616,3582,3547,3510,3473,3435,3395,3355,3313,3271,3227,3183,3137,3091,3043,2995,2946,2896,2845,2793,2740,2687,2632,2577,2521,2465,2407,2349,2290,2230,2170,2109,2048,1985,1922,1859,1795,1731,1666,1600,1534,1467,1400,1333,1265,1197,1129,1060,990,921,851,781,711,640,570,499,428,357,285,214,143,71,0};
long int DTAN_TABLE[91] = {0,71,143,214,286,358,430,502,575,648,722,796,870,945,1021,1097,1174,1252,1330,1410,1490,1572,1654,1738,1823,1909,1997,2086,2177,2270,2364,2461,2559,2659,2762,2868,2975,3086,3200,3316,3436,3560,3687,3819,3955,4095,4241,4392,4548,4711,4881,5058,5242,5435,5637,5849,6072,6307,6554,6816,7094,7389,7703,8038,8397,8783,9199,9649,10137,10670,11253,11895,12605,13396,14283,15285,16427,17740,19268,21070,23227,25858,29141,33355,38965,46809,58562,78132,117240,234442,250875938};
#define DSIN(x) (DSIN_TABLE[x])/*不会大于90°*/
#define DCOS(x) (DCOS_TABLE[x])/*不会大于90°*/
#define pi 3.14156
int MF_DSIN(x)
{
    if(x >= 0 && x <= 90)
        return(DSIN(x));
    else if(x > 90 && x <= 180)
        return(DCOS(x - 90));
    else if(x > 180 && x <= 270)
        return( - DSIN(x - 180));
    else if(x >270 && x <= 360)
        return( - DCOS(x - 270));
}
int MF_DCOS(x)
{
   if( x >= 0 && x <= 90)
        return(DCOS(x));
   else if(x >90 && x <=180)
        return( - DSIN(x - 90));
   else if(x > 180 && x <= 270)
        return( - DCOS(x - 180));
   else if(x > 270 && x <= 360)
        return(DSIN(x - 270)); }
int d_atan(double value)
{
    /**//*----------------------------------------------------------------*/
    /**//* Local Variables                                                */
    /**//*----------------------------------------------------------------*/
    long int low = 0, up = 90, spec = 45, binary = 1, sitaValue = 0;
    long int sita;
    sita = value * 4096;     /**//*----------------------------------------------------------------*/
    /**//* Code Body                                                      */
    /**//*----------------------------------------------------------------*/
    while (binary)
    {
        if (sita > DTAN_TABLE[spec])
        {
            up = up;
            low = spec;
        }
        else if (sita < DTAN_TABLE[spec])
        {
            up = spec;
            low = low;
        }
        else if (sita == DTAN_TABLE[spec])
        {
            sitaValue = spec;
            binary = 0;
        }
        if ((up - low) == 1)
        {
            sitaValue = low;
            binary = 0;
        }
        else
        {
            spec = ((up - low) >> 1) + low;
        }     }
    return sitaValue;
}
int MF_DATAN(double x1,double y1,double x2,double y2) /**//*(x1,y1)为敌机坐标,(x2,y2)为玩家飞机坐标*/
{
   if( x1 < x2 && y1 == y2)
   {
        return  0;
   }
   else if( x1 == x2 && y1 < y2)
   {
        return 90;
   }
   else if( x1 > x2 && y1 == y2)
   {
        return 180;
   }
   else if ( x1 == x2 && y1 > y2)
   {
        return 270;
   }
   else if( x1 > x2 && y1 > y2)
   {
        return (180 + d_atan((x1 - x2)/(y1 - y2)));
   }
   else if( x1 > x2 && y1 < y2)
   {
        return ( 90 + d_atan( (x1 - x2)/(y2 - y1)));
   }
   else if( x1 < x2 && y1 < y2)
   {
        return (d_atan((x2 - x1)/(y2 - y1)));
   }
   else if(x1 < x2 && y1 > y2)
   {
        return( 270 + d_atan((x2 - x1)/(y1 - y2)));
   } } http://blog.csdn.net/KyosukeNo1/archive/2006/05/28/759445.aspx