当前位置:编程学习 > JSP >>

J2ME中查表法使用三角函数

答案: 

CLDC和MIDP都没有提供三角函数,而且CLDC1.0中也没有浮点数,所以我们的选择是查表。使用8位定点数的sin和cos表。下面是wtk自带demo中的代码,只提供了有限的几个角度,实际使用时根据需要细化角度值。

// sines of angles 0, 10, 20, 30, 40, 50, 60, 70, 80, 90,    all *256
    private static final int[] SINES =
        { 0, 44, 88, 128, 165, 196, 222, 241, 252, 256 };


    // angle is in degrees/10, i.e. 0..36 for full circle
    private static int sineTimes256(int angle)
    {
        angle %= 36;    // 360 degrees
        if (angle <= 9)          // 0..90 degrees
        {
            return SINES[angle];
        }
        else if (angle <= 18)    // 90..180 degrees
        {
            return SINES[18-angle];
        }
        else if (angle <= 27)    // 180..270 degrees
        {
            return -SINES[angle-18];
        }
        else                     // 270..360 degrees
        {
            return -SINES[36-angle];
        }
    }


    // angle is in degrees/10, i.e. 0..36 for full circle
    private static int cosineTimes256(int angle)
    {
        return sineTimes256(angle + 9);     // i.e. add 90 degrees
    }


上一个:如何使用J2ME中的线程
下一个:J2ME技术入门之一——J2ME程序的开发过程

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,