Link to home
Create AccountLog in
Avatar of nike_golf
nike_golfFlag for Afghanistan

asked on

Distance between 2 points given LAT / LONG, most accurate..

I want to calculate the distance between 2 points given the LAT/LONG of each point.

I see there are a few solutions on the site;Since I know nothing about the math involved  I'm interested in the accuracies of these solutions and are there certain types that are better than others?

I'm interested in YARDS, so accuracy is a must...

Thanks.

NG,
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

the formula between 2 points (coordinates x1/y1 - x2/y2) is a simple math:
Sqr((Abs(X2 - X1) ^ 2) + (Abs(Y2 - Y1) ^ 2))

the "accuracy" is the one of the data types uses, ie precision of the Single data type:



Public Function DistanceBetween(ByVal X1 As Single, ByVal Y1 As Single, ByVal X2 As Single, ByVal Y2 As Single) As Single
  ' Calculate the distance between two points, given their X/Y coordinates.
  DistanceBetween = Sqr((Abs(X2 - X1) ^ 2) + (Abs(Y2 - Y1) ^ 2))
End Function

Open in new window

Since you posted that in three language zones, I take it that you need a solution for each of them (BTW, 'abs()' is not needed). In C/C++, that would be

#include <math.h>
 
double Distance(double x1, double y1, double x2, double y2) {
 
  return sqrt(pow(x1 - x2,2.0) + pow(y1 - y2,2.0));
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Agree to both post above. By the way. abs is not needed, that's true (negative squared always gives positive number).

If your coordinates are not integers (singles) it's definitely needed to use some floating point type like double. And does not matter what units you use, meters, or yards, or miles, etc.
Avatar of nike_golf

ASKER

Yes, these are positions taken from Google Earth.

I don't need a solution for all three languages just the best of the 3 if there is such a monster...

What is the accuracy of the Haversine formula? and does anyone have working Function that I can plug in and use?

Do I just convert the answer then if I want Miles or Yards?

Thanks,

>> What is the accuracy of the Haversine formula?

Pretty accurate. It assumes a spherical Earth (it's actually a geoid, but it's close to a sphere). You can make it slightly more accurate by using a different Earth radius for a different latitude.
For most applications, this formula is more than sufficient precision wise.


>> Do I just convert the answer then if I want Miles or Yards?

You can use whatever you want, as long as you use it consistently.