Here's an implementation of the atan version from http://en.wikipedia.org/wiki/Great-circle_distance ("special case of the Vincenty formula") which is "accurate for all distances":
/**
* Mean earth radius in Kilometers (KM) as defined in WGS84
*/
public static final double EARTH_RADIUS_KM = 6371.0087714;
/**
* Mean earth radius in US Miles, converted from Kilometers
*/
public static final double EARTH_RADIUS_MI = EARTH_RADIUS_KM * 0.621371192;
/**
* Mean earth radius in Nautical Miles, converted from Kilometers
*/
public static final double EARTH_RADIUS_NM = EARTH_RADIUS_KM * 0.539956803;
/**
* Calculates the distance between two points on a sphere using a "special
* case of the Vincenty formula" for accuracy. This uses more trigonometric
* calls than other formulas, but is more accurate for all distances.
* <p/>
*
* For more details, see <a href=
* "https://en.wikipedia.org/wiki/Great-circle_distance">https://en.wikipedia.org/wiki/Great-circle_distance</a>.
*
* @param startLat
* The starting point's latitude
* @param startLon
* The starting point's longitude
* @param endLat
* The ending point's latitude
* @param endLon
* The ending point's longitude
* @param radius
* The radius of the spherical earth to be used
*
* @return The distance between the points for the given radius
*/
public static double greatCircleDistance( double startLat, double startLon, double endLat, double endLon, double radius )
{
// convert to radians
double lonDelta = Math.toRadians( startLon - endLon );
double lat1 = Math.toRadians( startLat );
double lat2 = Math.toRadians( endLat );
// compute repeatedly used values
double cos1 = Math.cos( lat1 );
double sin1 = Math.sin( lat1 );
double cos2 = Math.cos( lat2 );
double sin2 = Math.sin( lat2 );
double cosDelta = Math.cos( lonDelta );
double top1 = cos2 * Math.sin( lonDelta );
top1 *= top1; // squared
double top2 = (cos1 * sin2) - (sin1 * cos2 * cosDelta);
top2 *= top2; // squared
double top = Math.sqrt( top1 + top2 );
double bottom = (sin1 * sin2) + (cos1 * cos2 * cosDelta);
double rad = Math.atan( top / bottom );
return radius * rad;
}
Choosing different values for the radius of the earth sets the units desired. This will work for any sphere of any radius, so distances on Mars could be calculated using a radius of 3389.5 km as long as you know the lat/lon of the starting and ending points.