Java 数学库的 Objective-C 等效项是什么?
哪个库/头文件/类相当于 Java Math 类?
背景&额外问题:
我正在尝试将此函数从 Java 移植到 Objective-C。我应该重写它,还是可以复制粘贴并仅重写语法不同的部分? (换句话说,如果 Java 作为 Objective-C 或 C 运行,其行为是否会相同?)
这是 Java 中的怪物函数。它本质上是Vincinty 公式:
private double vincentyFormula(GeoLocation location, int formula) {
double a = 6378137;
double b = 6356752.3142;
double f = 1 / 298.257223563; // WGS-84 ellipsiod
double L = Math.toRadians(location.getLongitude() - getLongitude());
double U1 = Math
.atan((1 - f) * Math.tan(Math.toRadians(getLatitude())));
double U2 = Math.atan((1 - f)
* Math.tan(Math.toRadians(location.getLatitude())));
double sinU1 = Math.sin(U1), cosU1 = Math.cos(U1);
double sinU2 = Math.sin(U2), cosU2 = Math.cos(U2);
double lambda = L;
double lambdaP = 2 * Math.PI;
double iterLimit = 20;
double sinLambda = 0;
double cosLambda = 0;
double sinSigma = 0;
double cosSigma = 0;
double sigma = 0;
double sinAlpha = 0;
double cosSqAlpha = 0;
double cos2SigmaM = 0;
double C;
while (Math.abs(lambda - lambdaP) > 1e-12 && --iterLimit > 0) {
sinLambda = Math.sin(lambda);
cosLambda = Math.cos(lambda);
sinSigma = Math.sqrt((cosU2 * sinLambda) * (cosU2 * sinLambda)
+ (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda)
* (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda));
if (sinSigma == 0)
return 0; // co-incident points
cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * cosLambda;
sigma = Math.atan2(sinSigma, cosSigma);
sinAlpha = cosU1 * cosU2 * sinLambda / sinSigma;
cosSqAlpha = 1 - sinAlpha * sinAlpha;
cos2SigmaM = cosSigma - 2 * sinU1 * sinU2 / cosSqAlpha;
if (Double.isNaN(cos2SigmaM))
cos2SigmaM = 0; // equatorial line: cosSqAlpha=0 (ß6)
C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
lambdaP = lambda;
lambda = L
+ (1 - C)
* f
* sinAlpha
* (sigma + C
* sinSigma
* (cos2SigmaM + C * cosSigma
* (-1 + 2 * cos2SigmaM * cos2SigmaM)));
}
if (iterLimit == 0)
return Double.NaN; // formula failed to converge
double uSq = cosSqAlpha * (a * a - b * b) / (b * b);
double A = 1 + uSq / 16384
* (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
double B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
double deltaSigma = B
* sinSigma
* (cos2SigmaM + B
/ 4
* (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) - B
/ 6 * cos2SigmaM
* (-3 + 4 * sinSigma * sinSigma)
* (-3 + 4 * cos2SigmaM * cos2SigmaM)));
double distance = b * A * (sigma - deltaSigma);
// initial bearing
double fwdAz = Math.toDegrees(Math.atan2(cosU2 * sinLambda, cosU1
* sinU2 - sinU1 * cosU2 * cosLambda));
// final bearing
double revAz = Math.toDegrees(Math.atan2(cosU1 * sinLambda, -sinU1
* cosU2 + cosU1 * sinU2 * cosLambda));
if (formula == DISTANCE) {
return distance;
} else if (formula == INITIAL_BEARING) {
return fwdAz;
} else if (formula == FINAL_BEARING) {
return revAz;
} else { // should never happpen
return Double.NaN;
}
}
What library/header/class is equivalent to the Java Math
class?
Background & bonus question:
I'm trying to port this function to Objective-C from Java. Should I rewrite it, or can I copy and paste and rewrite only the parts that are syntactically different? (In other words, will the Java behave the same way if it were run as Objective-C or C?)
Here's the monster function in Java. It essentially is the Vincinty Formula:
private double vincentyFormula(GeoLocation location, int formula) {
double a = 6378137;
double b = 6356752.3142;
double f = 1 / 298.257223563; // WGS-84 ellipsiod
double L = Math.toRadians(location.getLongitude() - getLongitude());
double U1 = Math
.atan((1 - f) * Math.tan(Math.toRadians(getLatitude())));
double U2 = Math.atan((1 - f)
* Math.tan(Math.toRadians(location.getLatitude())));
double sinU1 = Math.sin(U1), cosU1 = Math.cos(U1);
double sinU2 = Math.sin(U2), cosU2 = Math.cos(U2);
double lambda = L;
double lambdaP = 2 * Math.PI;
double iterLimit = 20;
double sinLambda = 0;
double cosLambda = 0;
double sinSigma = 0;
double cosSigma = 0;
double sigma = 0;
double sinAlpha = 0;
double cosSqAlpha = 0;
double cos2SigmaM = 0;
double C;
while (Math.abs(lambda - lambdaP) > 1e-12 && --iterLimit > 0) {
sinLambda = Math.sin(lambda);
cosLambda = Math.cos(lambda);
sinSigma = Math.sqrt((cosU2 * sinLambda) * (cosU2 * sinLambda)
+ (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda)
* (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda));
if (sinSigma == 0)
return 0; // co-incident points
cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * cosLambda;
sigma = Math.atan2(sinSigma, cosSigma);
sinAlpha = cosU1 * cosU2 * sinLambda / sinSigma;
cosSqAlpha = 1 - sinAlpha * sinAlpha;
cos2SigmaM = cosSigma - 2 * sinU1 * sinU2 / cosSqAlpha;
if (Double.isNaN(cos2SigmaM))
cos2SigmaM = 0; // equatorial line: cosSqAlpha=0 (ß6)
C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
lambdaP = lambda;
lambda = L
+ (1 - C)
* f
* sinAlpha
* (sigma + C
* sinSigma
* (cos2SigmaM + C * cosSigma
* (-1 + 2 * cos2SigmaM * cos2SigmaM)));
}
if (iterLimit == 0)
return Double.NaN; // formula failed to converge
double uSq = cosSqAlpha * (a * a - b * b) / (b * b);
double A = 1 + uSq / 16384
* (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
double B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
double deltaSigma = B
* sinSigma
* (cos2SigmaM + B
/ 4
* (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) - B
/ 6 * cos2SigmaM
* (-3 + 4 * sinSigma * sinSigma)
* (-3 + 4 * cos2SigmaM * cos2SigmaM)));
double distance = b * A * (sigma - deltaSigma);
// initial bearing
double fwdAz = Math.toDegrees(Math.atan2(cosU2 * sinLambda, cosU1
* sinU2 - sinU1 * cosU2 * cosLambda));
// final bearing
double revAz = Math.toDegrees(Math.atan2(cosU1 * sinLambda, -sinU1
* cosU2 + cosU1 * sinU2 * cosLambda));
if (formula == DISTANCE) {
return distance;
} else if (formula == INITIAL_BEARING) {
return fwdAz;
} else if (formula == FINAL_BEARING) {
return revAz;
} else { // should never happpen
return Double.NaN;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java 的 Math 类很大程度上基于标准 C math.h,因此您可以使用那个。
Java's Math class in very much based on the standard C math.h, so you could use that one.
看起来您正在重新实现现有代码。请参阅
MKMetersBetweenMapPoints()
。Looks like you're reimplementing existing code. See
MKMetersBetweenMapPoints()
.