Sign in to follow this  
tiberiu11

Latitude Longitude Distance calculation problem

Recommended Posts

Guys, I am asking for you help again. This time I am experiencing a problem when trying to calculate the distance between two locations(I have the latitude and longitude for each of the locations). I tried coding a simple MS-DOS program in C++, but I do not know how to make the appropriate conversions.Here is the sourcecode:double a1 = 450000; //first latitudedouble b1 = 230000; /first longitudedouble a2 = 455000; //second latitudedouble b2 = 230000; //second latitudedouble dist;double r = 3443.9; /radius of Earthcout << a1;cout << 'n';cout << b1;cout << 'n';cout << a2;cout << 'n';cout << b2;cout << 'n';/*a1 = a1 * pi / 180;b1 = b1 * pi / 180;b2 = b2 * pi / 180;*/dist = acos(cos(a1)*cos(b1)*cos(a2)*cos(b2) + cos(a1)*sin(b1)*cos(a2)*sin(b2) + sin(a1)*sin(a2)) * r;dist = dist * 180 / pi; My problem is that I do not get the appropriate answer(50.0895 nautical miles) I always get a different bigger answer. I used this website to help me out with determing the distance, and the sourcecode:http://jan.ucc.nau.edu/~cvm/latlongdist.html I also have a code that converts decimal degrees to deg:min:sec , but I do not know if I should use it. It is really not that important for me to use it, since I just want this code to work, and to correctly calculate the distance between these coordinates. I thank you very much for your help in advance. This really means a lot to me, and my project.Yours Truly,Tiberiu Brasov

Share this post


Link to post
Share on other sites
Help AVSIM continue to serve you!
Please donate today!

I use this formula:...#include #ifndef PI #define PI 3.1415926535897932384626433832795 //circle number#endif#define HALFEARTH 10800 //half earth circumference in nautic miles#define RADIANS_TO_DEGREE_FACTOR (180.0/PI)#define DEG_SIN( val ) sin( ( val )/RADIANS_TO_DEGREE_FACTOR) //sinus in degrees#define DEG_COS( val ) cos( ( val )/RADIANS_TO_DEGREE_FACTOR) //cosine in degrees#define DEG_TAN( val ) tan( ( val )/RADIANS_TO_DEGREE_FACTOR) //tangens in degrees#define DEG_ASIN( val ) RADIANS_TO_DEGREE_FACTOR * asin(( val )) //arcsinus in degrees#define DEG_ACOS( val ) RADIANS_TO_DEGREE_FACTOR * acos(( val )) //arccosine in degrees#define DEG_ATAN( val ) RADIANS_TO_DEGREE_FACTOR * atan(( val )) //arctangens in degrees#define DEG_ATAN2( val1 , val2 ) RADIANS_TO_DEGREE_FACTOR * atan2( val1, val2 ) //atan2 in degrees#define RADIANS_TO_MINUTE_FACTOR (10800.0/PI)#define RADIANS_TO_MINUTE( val ) (RADIANS_TO_MINUTE_FACTOR *( val ))#define MIN_SIN( val ) (sin( ( val )/RADIANS_TO_MINUTE_FACTOR))#define MIN_COS( val ) (cos( ( val )/RADIANS_TO_MINUTE_FACTOR))#define MIN_TAN( val ) (tan( ( val )/RADIANS_TO_MINUTE_FACTOR))#define MIN_ASIN( val ) (RADIANS_TO_MINUTE_FACTOR * asin(( val )))#define MIN_ACOS( val ) (RADIANS_TO_MINUTE_FACTOR * acos(( val )))#define MIN_ATAN( val ) (RADIANS_TO_MINUTE_FACTOR * atan(( val )))#define MIN_ATAN2( val1 , val2 ) (RADIANS_TO_MINUTE_FACTOR * atan2( ( val1) , ( val2 ) ))/* #if (_MSC_VER >= 1300) && (WINVER < 0x0500) //VC7 or later, building with pre-VC7 runtime libraries extern "C" long _ftol( double ); //defined by VC6 C libs extern "C" long _ftol2( double dblSource ) { return _ftol( dblSource ); } #endif*///GREAT CIRCLE// _ // | // To| |InboundCourse// ---------------------------------------+----// | / | |// | / | / beta// | / -|- // | / .// | / .// | / .// | / .// DeltaTheta / Dist .// | / .// | / .// | / .// | alpha/ .// |-----/ .// | / .// |// .// -----+------------DeltaPhi---------->.// |From// |// |//double GrtCrcDist(double *pdblInboundCourse, double dblFromLat,double dblFromLong, double dblToLat,double dblToLong)// input (lat,lon) in degrees// output dist in minutes (==nm) course in degrees{ double dblDist=0,dblCourse=180,dblDeltaTheta=0,dblDeltaPhi=0; dblDeltaPhi=(dblToLong-dblFromLong)*0.5; dblDeltaTheta=(dblToLat-dblFromLat)*0.5; dblDist= DEG_SIN(dblDeltaTheta)*DEG_SIN(dblDeltaTheta)+ DEG_COS(dblFromLat)*DEG_COS(dblToLat)*DEG_SIN(dblDeltaPhi)*DEG_SIN(dblDeltaPhi); if(dblDist>0) { dblDist=sqrt(dblDist); if(dblDist<1) dblDist=2*MIN_ASIN(dblDist); else dblDist=HALFEARTH; dblCourse=DEG_ACOS( (DEG_SIN(dblFromLat)-DEG_SIN(dblToLat)*MIN_COS(dblDist))/ (DEG_COS(dblToLat)*MIN_SIN(dblDist)) ); } if(DEG_SIN(2.0*dblDeltaPhi)>0) dblCourse=360-dblCourse; if(pdblInboundCourse) *pdblInboundCourse=dblCourse; return dblDist;} ...it might be helpful. It does not use the cos-calculus of spheric triangles directly, but a little transformation to sin sin(x/2)=sqrt((1-cos(x))/2)this more accurate for short distances. Please remind the definition of nm, it is an arc minute. So only recalculate angles to minutes and you'll get nm.Arne Bartels

Share this post


Link to post
Share on other sites

I use this for great circle:void NV_LLALLA2GC(PGPSLLA from, PGPSLLA to, PGPSRhumb r){ double d, c; double dlat, dlon, dlon2; dlon2 = (from->lon) - (to->lon); dlat = sin(((from->lat) - (to->lat)) * 0.5); dlon = sin( dlon2 * 0.5); d = 2.0 * asin(sqrt( (dlat * dlat) + (cos(from->lat) * cos(to->lat) * (dlon * dlon)) ) ); c = (atan2( sin(dlon2) * cos(to->lat) , cos(from->lat) * sin(to->lat) - sin(from->lat) * cos(to->lat) * cos(dlon) ) ); r->d = RAD2NM(d); r->tc = c;}all angles in Radians. r->d is distance and t->tc is true course, converted using:#define RAD2NM® ((double) (® * (3437.7467707849975) ) )Hope this helps!

Share this post


Link to post
Share on other sites

Arne and Jean, Thank you very much for your help. I do not know what I would have done without you. I am still experiencing a problem while calculating the distance. I get the answer for both of the codes 2400. I use these values:From lat = 450000From long = 230000To lat = 455000To long = 230000 My questions are: What should I do to 2400 in order to get the distance between the latitudes in a nm format? Is 2400 considered as minutes, which need to be converted to nm? I am stuck, and any help is greatly appreciated.Sincerely,Tibeiru Brasov

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this