Here
是一组用于快速近似三角函数的低级技巧。C中有一些我很难理解的示例代码,但是这些技术在Java中同样容易实现。
这是我在Java中的ViSQL和ATAN2的等价实现。
我可以为其他trig函数做类似的工作,但我没有发现这是必要的,因为分析显示只有sqrt和atan/atan2是主要的瓶颈。
public class FastTrig
{
/** Fast approximation of 1.0 / sqrt(x).
* See http://www.beyond3d.com/content/articles/8/
* @param x Positive value to estimate inverse of square root of
* @return Approximately 1.0 / sqrt(x)
**/
public static double
invSqrt(double x)
{
double xhalf = 0.5 * x;
long i = Double.doubleToRawLongBits(x);
i = 0x5FE6EB50C7B537AAL - (i>>1);
x = Double.longBitsToDouble(i);
x = x * (1.5 - xhalf*x*x);
return x;
}
/** Approximation of arctangent.
* Slightly faster and substant