Question

"I'm working with J2ME in eclipse, and need to use the method acos() belongs to the Math package, the question is that the package available for J2ME Math is not that function (limitations of mobiles), then I want an algorithm or method alternative to calculate the cosine.

I need the "acos" to calculate the following formula:

long2 = cood[i].getLongitud();
lat2 = cood[i].getLatitud();
dlong = (long1 - long2); 
c = dlong * degtorad;
dvalue = (Math.sin(a) * Math.sin(b))+(Math.cos(a) * Math.cos(b)*Math.cos(c)); 

dd = Math.acos(dvalue) * radtodeg; 

km = (dd * 111.302);

This function allows me from two geographical coordinates to calculate the distance in kilometers between them. If there is an alternative method to calculate this distance (where the cosine is not used), I also would be useful.

Any help on this?

Was it helpful?

Solution

public static double acos(double a)
{
    final double epsilon=1.0E-7; 
    double x=a;
    do {
        x-=(Math.sin(x)-a)/Math.cos(x);
    }
    while (Math.abs(Math.sin(x)-a)>epsilon);

    // returned angle is in radians
    return -1*(x-Math.PI/2);
}

OTHER TIPS

You could try to implement the arccosine as shown here.

If you have the Location API available you can use the distance method from the Coordinates class. From the Coordinates API:

public float distance(Coordinates to)

Calculates the geodetic distance between the two points according to the ellipsoid model of WGS84. Altitude is neglected from calculations. The implementation shall calculate this as exactly as it can. However, it is required that the result is within 0.35% of the correct result.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top