Distance Formula and code segment

This page contains an explanation of the formula and the source code used in the GC Calculator. It is probably only useful to those with a reasonable grasp of mathematics and/or programming.
The Java API is now available for purchase


Great Circle Menu
Map & Calculator
Alternate Map sizes
Formula
Java API
Spreadsheet
Perl script
What is a
Great Circle?
Help Us
Sponsorship Info
Statistics



 

If you arrived at this page looking to something to calculate distances then you probably really want my GC Calculator.

Skip to the source code if you don't want to read my waffle.

Sounds sad, but a Great Circle calculator is something I had been meaning to write for many years. I investigated, tested, and ultimately discarded many algorithms owing to them being inaccurate or insufficiently general purpose. In 1998 I stumbled across an accurate algorithm in a rather elderly mathematical text book entitled Spherical Geometry at my local library. Regrettably I do not have a note of the author's name. It is this algorithm that is encoded in the GC calculator and shown below.

Earth from Space, courtesy of NASA
Earth from Space, courtesy of NASA

The calculator is written in Java. It consists of over 4000 lines of code carefully crafted using only Java1.1, which should ensure compatibility with most browsers. I've had a few mails from people under the impression that's its Javascript. Well Javascript simply doesn't posses the graphical or interactive facilities that I believe makes this GC Calculator stand out from all the rest. Some say that the only resemblance between the two languages is in the name (I tend to agree).
A good way to insult a Java programmer is to suggest that his work is Javascript...

The Java API is now available for purchase

The formula is generic, and relates only to the earth by virtue of the constant EARTH_RADIUS. The formula assumes the earth is a perfect sphere which it is not.

Explanation of code

I am an IT professional, and not a mathematician. I will not therefore attempt to explain the mathematics involved, but merely the computer code I created from it.....

Degrees, minutes, and seconds of latitude and longitude are converted into a floating point Radian value. Most people are more familiar with Degrees, computers however use Radians;
180 degrees = (Pi) radians. is approximately 3.142

Northerlies are represented by positive values, Southerlies by negative ones. Similarly, longitudes east of the meridian are positive, westerlies negative. This happens to be the convention used by the aviation industry, in case you're interested. It doesn't actually matter which convention one uses so long as you're consistent..

All calculations are done using 64 bit floating point arithmetic ("double"), this is quite sufficiently accurate for the range of values relating to the earth, though possibly not for significantly larger bodies such as stars.

The functions Math.sin() and Math.cos() merely return the sine and cosine respectively of their arguments. Possibly less obvious is that Math.acos() returns the arc-cosine of it's argument.

Those unfamiliar with OOP may scratch their head wondering where the two co-ordinates come from as only one is passed into GreatCircleDistance(). Well the technical definition is that the first co-ordinate is actually a private instance variable defined within the LatLong class, and GreatCircleDistance() is a public method within that class. In essence the first co-ordinate is already visible to the method so doesn't need to be passed to it.

For C programmers
the variable is defined at the top of the source file prior to any function definition, thus making is visible to all functions within that source file ( primitive concept IMHO ).

For Fortran programmers
the nearest analogy is a named common block variable ( a vastly superior concept to C, again IMHO ).

For non-programmers
Value Represented by:
Latitude of first co-ordinate lat
Longitude of first co-ordinate lon
Latitude of second co-ordinate alt.lat
Longitude of second co-ordinate alt.lon

The code segment

N.B. This code will not byte-compile standalone.

// CONSTANTS USED INTERNALLY
final double DEGREES_TO_RADIANS = ( Math.PI/180.0 );
// Mean radius in KM final double EARTH_RADIUS = 6371.0;
/** Method to compute Great Circle distance between * two points. Please note that this algorithm * assumes the Earth to be a perfect sphere, whereas * in fact the equatorial radius is about 30Km * greater than the Polar. * * @param alt other point to compute distance to * @return The distance in Kilometres */ public double GreatCircleDistance(LatLong alt) { // There is no real reason to break this lot into // 4 statements but I just feel it's a little more // readable. double p1 = Math.cos(lat)*Math.cos(lon) *Math.cos(alt.lat)*Math.cos(alt.lon); double p2 = Math.cos(lat)*Math.sin(lon) *Math.cos(alt.lat)*Math.sin(alt.lon); double p3 = Math.sin(lat)*Math.sin(alt.lat); return(Math.acos(p1+p2+p3)*EARTH_RADIUS);
}

The Java API is now available for purchase

Great Circle visitor counter
total hits on all pages in section
(since 01/01/04)



Home Thai Guide   Great Circle Calculator WorldClock AMS Services Contact us