今天搞J2ME里面计算经纬度,没想到J2ME这么多限制,连几个很简单的三角函数也不给提供完全,还要直接写.没办法,自己动手好了.留个代码,方便以后参考学习吧.
package
com.ken.math;
/**
* 通过计算公式实现反正切、反正弦、反余弦<br>
* Tan(X)=Sin(X)/Cos(x) <br>
* ArcSin(X)=ArcTan(X/Sqrt(1-Sqr(X)))<br>
* ArcCos(X)=ArcTan(Sqrt(1-Sqr(X))/X)<br>
*
* @author <a href="mailto:xzknet@gmail.com">Ken_xu</a>
* @version 1.0 Copyright 2008-5-4 下午04:09:04
*/
public class SuperMath {
/**
* 反正切<br>
* radian为正切值,这个函数的值域为-π/2~π/2
*
* @param radian
* 弧度为单位的角度
* @return 该角度所对应的反正切
* @author Ken_xu
*/
public static double actTan(double radian) {
System.out.println(radian);
double retval = 0d;
if (Math.abs(radian) <= 1.0d) {
retval = radian / (1.0D + 0.28d * sqr(radian));
} else {
retval = -radian / (sqr(radian) + 0.28d);
if (radian < -1.0d) {
retval = retval - Math.PI / 2;
} else {
retval = retval + Math.PI / 2;
}
}
return retval;
}
/**
* 反正切<br>
* 正切值为y/x,因此该函数求的是y/x所对应的角,这个函数的值域为-π~π
*
* @param y
* @param x
* @return 该角度所对应的反正切
* @author Ken_xu
*/
public static double actTan(double y, double x) {
if (y == 0.0d && x == 0.0d)
return 0.0d;
if (x > 0.0d)
return actTan(y / x);
if (x < 0.0F)
if (y < 0.0d)
return (-(Math.PI - actTan(y / x)));
else
return (Math.PI - actTan(-y / x));
return y >= 0.0F ? Math.PI / 2 : Math.PI / 2;
}
/**
* 反正弦
*
* @param radian
* 弧度为单位的角度
* @return 该角度所对应的反正弦
* @author Ken_xu
*/
public static double actSin(double radian) {
// ArcSin(X)=ArcTan(X/Sqrt(1-Sqr(X)))
double retval = actTan(radian / Math.sqrt(1 - sqr(radian)));
return retval;
}
/**
* 反余弦
*
* @param radian
* 弧度为单位的角度
* @return 该角度所对应的反余弦
* @author Ken_xu
*/
public static double actCos(double radian) {
// ArcCos(X)=ArcTan(Sqrt(1-Sqr(X))/X)
double retval = actTan(Math.sqrt(1 - sqr(radian)) / radian);
return retval;
}
/**
* 平方函数
*
* @param x
* @return 平方值( x * x )
* @author Ken_xu
*/
public static double sqr(double x) {
return x * x;
}
}
/**
* 通过计算公式实现反正切、反正弦、反余弦<br>
* Tan(X)=Sin(X)/Cos(x) <br>
* ArcSin(X)=ArcTan(X/Sqrt(1-Sqr(X)))<br>
* ArcCos(X)=ArcTan(Sqrt(1-Sqr(X))/X)<br>
*
* @author <a href="mailto:xzknet@gmail.com">Ken_xu</a>
* @version 1.0 Copyright 2008-5-4 下午04:09:04
*/
public class SuperMath {
/**
* 反正切<br>
* radian为正切值,这个函数的值域为-π/2~π/2
*
* @param radian
* 弧度为单位的角度
* @return 该角度所对应的反正切
* @author Ken_xu
*/
public static double actTan(double radian) {
System.out.println(radian);
double retval = 0d;
if (Math.abs(radian) <= 1.0d) {
retval = radian / (1.0D + 0.28d * sqr(radian));
} else {
retval = -radian / (sqr(radian) + 0.28d);
if (radian < -1.0d) {
retval = retval - Math.PI / 2;
} else {
retval = retval + Math.PI / 2;
}
}
return retval;
}
/**
* 反正切<br>
* 正切值为y/x,因此该函数求的是y/x所对应的角,这个函数的值域为-π~π
*
* @param y
* @param x
* @return 该角度所对应的反正切
* @author Ken_xu
*/
public static double actTan(double y, double x) {
if (y == 0.0d && x == 0.0d)
return 0.0d;
if (x > 0.0d)
return actTan(y / x);
if (x < 0.0F)
if (y < 0.0d)
return (-(Math.PI - actTan(y / x)));
else
return (Math.PI - actTan(-y / x));
return y >= 0.0F ? Math.PI / 2 : Math.PI / 2;
}
/**
* 反正弦
*
* @param radian
* 弧度为单位的角度
* @return 该角度所对应的反正弦
* @author Ken_xu
*/
public static double actSin(double radian) {
// ArcSin(X)=ArcTan(X/Sqrt(1-Sqr(X)))
double retval = actTan(radian / Math.sqrt(1 - sqr(radian)));
return retval;
}
/**
* 反余弦
*
* @param radian
* 弧度为单位的角度
* @return 该角度所对应的反余弦
* @author Ken_xu
*/
public static double actCos(double radian) {
// ArcCos(X)=ArcTan(Sqrt(1-Sqr(X))/X)
double retval = actTan(Math.sqrt(1 - sqr(radian)) / radian);
return retval;
}
/**
* 平方函数
*
* @param x
* @return 平方值( x * x )
* @author Ken_xu
*/
public static double sqr(double x) {
return x * x;
}
}