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
}
雖然這是一個簡單的實現,但確非常實用,從效率來說,要比模擬浮點計算的效率高很多。但是現在項目中需要使用一個帶有開三次方的計算公式,這時這個東西就幫不上忙了。^-^
因此,在非CLDC1.1以上的開發環境中,需要自己模擬實現這種功能并不簡單,但也并不是沒有辦法,具體實現請關注作者的后續原創文章。(這篇不是原創,來自網絡)
(出處:http://www.49028c.com)
新聞熱點
疑難解答