2011-03-23 36 views
0

什麼庫/標題/類相當於Java Math類?什麼是Java Math庫的Objective-C等價物?


背景&獎金問題:

我試圖端口此功能可以從Java的Objective-C。我應該重寫它,還是隻能複製和粘貼並重寫語法不同的部分? (換句話說,如果Java以Objective-C或C的形式運行,Java的行爲是否一樣?)

下面是Java中的怪物函數。它本質上是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; 
    } 
    } 
+1

選中此項。 http://stackoverflow.com/questions/5348159/how-do-i-find-cos10-6481-cos-inverse-of-math-function-in-objective-c/ – taskinoor 2011-03-23 05:15:34

回答

6

在很多Java的Math類基於標準的C math.h,所以你可以使用一個。

+0

很好,但似乎缺少度數弧度和弧度度。 – Moshe 2011-03-23 05:35:09

+0

是的。看到這裏:http://stackoverflow.com/questions/4765937/iphone-toradians-todegrees-compiler-problem – Thilo 2011-03-23 05:37:58

0

看起來像你正在重新實現現有的代碼。見MKMetersBetweenMapPoints()

+0

有趣的...我會研究它。 – Moshe 2011-03-23 13:33:41

相關問題