Reputation: 13025
For any given latitude and longitude there should exist two distances. Once is clockwise direction and other is anti-clockwise direction. That is how sometimes flights fly too.
Below javascript worked fine in calculating the distance. I'm not sure whether it is clockwise or anti-clockwise. (referring South Down and North up)
What would be the formula to find both directions?
var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var lat1 = lat1.toRad();
var lat2 = lat2.toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
Edit:
I want to calculate clockwise and anti-clockwise path between two points. Practically flights use clockwise and anti-clockwise paths to reach the same point. They don't just use the direct path for commercial and regulatory reasons.
I just want a formula to calculate clockwise and anti-clockwise path to the same point.
Distance wise, the shortest straight path and longest straight path.
Upvotes: 0
Views: 901
Reputation: 70122
If you read the description of the website where you cut and paste that code, you will find that it is the haversine forumla and that it provides the shortest distance.
The equation does not give you a simple clockwise or anticlockwise path, it is the most direct 'journey' from point to point.
Upvotes: 1