December 24th, 2012, 02:50 AM

A program to calculate heading between two gps coordinates?!!!
someone find me a piece of code where I input 4 floats which are latitude and longitudes of 2 points on the surface of the earth:
A(a1,a2) and B(b1,b2)
and the function/program returns me a heading from A to B.
I've been googling alot for this, and haven't found anything useful yet.
And I'm talking strictly of C.
no C++, C# etc...just C.
December 24th, 2012, 03:07 AM

Read this
Just remember to turn your GPS degrees.minutes.seconds into radians before calling the math functions.
December 24th, 2012, 03:13 AM

I'm pretty much a beginner level "programmer"...
cant do anything myself.
So if you could get me the code...
Most of the code I cant understand.
Also, I had read all of those already...
December 24th, 2012, 03:31 AM

> I'm pretty much a beginner level "programmer"...
Guess how you become an advanced programmer.
> cant do anything myself.
Awwww  do you want your bib changing again?
Is that "can't" or "won't"?
This problem is way beyond the "hello world" example at the beginning of the book/course, so you must have learnt a few things along the way. Given the date, it looks like a nice end of semester assignment.
Look, you have a website which has pseudocode AND also provides you with test code to check against. It doesn't get any better than that, unless you want to be totally spoonfed.
So
1. Read the page again (and this time, spend more than 5 minutes on it FFS)
2. Write some code
3. Type in an example coordinate pair on the website
4. Pass those same coordinates into your code
5. Compare results.
When you've got code of your own, then feel free to post it here and we'll gladly help you (we like people who show self motivation and initiative).
But if you just intend to roll around from one forum to another with "google for me" or "gimmetehcodez", then you're just going to end up failing at some point. You may get lucky occasionally, but failure is inevitable.
December 24th, 2012, 03:53 AM

Originally Posted by salem
> I'm pretty much a beginner level "programmer"...
Guess how you become an advanced programmer.
> cant do anything myself.
Awwww  do you want your bib changing again?
Is that "can't" or "won't"?
This problem is way beyond the "hello world" example at the beginning of the book/course, so you must have learnt a few things along the way. Given the date, it looks like a nice end of semester assignment.
Look, you have a website which has pseudocode AND also provides you with test code to check against. It doesn't get any better than that, unless you want to be totally spoonfed.
So
1. Read the page again (and this time, spend more than 5 minutes on it FFS)
2. Write some code
3. Type in an example coordinate pair on the website
4. Pass those same coordinates into your code
5. Compare results.
When you've got code of your own, then feel free to post it here and we'll gladly help you (we like people who show self motivation and initiative).
But if you just intend to roll around from one forum to another with "google for me" or "gimmetehcodez", then you're just going to end up failing at some point. You may get lucky occasionally, but failure is inevitable.
I would like to be spoon fed, cuz I'm not studying programming at university etc , I'm just learning it as a hobby...And one day I was just thinking how headings could be calculated...
But don't spoon feed me yet. Let me try it on my own first, like you said...I'll contact you in a few days if I'm unable to do it on my own...
Thanks.
December 24th, 2012, 09:23 AM

A number of formulae and Javascript code are presented here:http://www.movabletype.co.uk/scripts/latlong.html.
The Javascript is relatively easy to port to C code. Note that the C math library trig functions work in radians. To convert from degrees to radians: r = d * pi / 180
[edit]I see I have moreorless repeated Salem's answer!
Last edited by clifford; December 24th, 2012 at 09:27 AM.
December 24th, 2012, 11:58 AM

the formula is :
Code:
θ = atan2( sin(Δλ).cos(φ2), cos(φ1).sin(φ2) − sin(φ1).cos(φ2).cos(Δλ) )
where φ is latitude, λ is longitude
So what am I doing wrong here(the following code) that it does not seem to work?!!
Code:
#include <math.h>
int main()
{
float head, lon1, lon2, lat1, lat2;
float londif;
londif=lon2lon1;
printf("enter lat1, lon1, lat2, lon2\n");
scanf("%f, %f, %f, %f", &lat1, &lon1, &lat2, &lon2);
head = atan2((sin(londif)*cos(lat2)),((cos(lat1)*sin(lat2))(sin(lat1)*cos(lat2)*cos(londif)))) ;
printf("%f", head);
}
Comments on this post
December 24th, 2012, 12:35 PM

> londif=lon2lon1;
You need to do this AFTER you have input the data, not before.
> scanf("%f, %f, %f, %f", &lat1, &lon1, &lat2, &lon2);
Do you actually type in commas when you input the data, as in
1, 2, 3, 4
If you typed in
1 2 3 4
then remove the commas from the format string.
> sin(londif)
See the previous comments.
Did you convert say 50.5 degrees into radians?
If you typed in radians, then that's fine, but it's hard work to ask for radians from the user.
December 24th, 2012, 02:01 PM

Originally Posted by salem
> londif=lon2lon1;
You need to do this AFTER you have input the data, not before.
> scanf("%f, %f, %f, %f", &lat1, &lon1, &lat2, &lon2);
Do you actually type in commas when you input the data, as in
1, 2, 3, 4
If you typed in
1 2 3 4
then remove the commas from the format string.
> sin(londif)
See the previous comments.
Did you convert say 50.5 degrees into radians?
If you typed in radians, then that's fine, but it's hard work to ask for radians from the user.
Try it now, I've finished the code. Enter values in degrees with comma in between and compare with this. I know its slightly off, cuz the earth is not a perfect sphere, its oblate spheroid. :
Code:
#include <math.h>
int main()
{
float a, b, c, d;
float head, lon1, lon2, lat1, lat2;
float londif;
float finalans, finalans2;
printf("enter lat1, lon1, lat2, lon2\n");
scanf("%f, %f, %f, %f", &a, &b, &c, &d);
lat1=(a*3.14159)/180;
lon1=(b*3.14159)/180;
lat2=(c*3.14159)/180;
lon2=(d*3.14159)/180;
londif=lon2lon1;
head=atan2((sin(londif)*cos(lat2)),((cos(lat1)*sin(lat2))(sin(lat1)*cos(lat2)*cos(londif)))) ;
finalans=(head*180)/3.14159;
finalans2=0;
if(finalans<=0)
{
finalans2=finalans+360;
printf("%f", finalans2);
}
else
printf("%f", finalans);
}
December 24th, 2012, 03:36 PM

Originally Posted by zedeneye1
Try it now, I've finished the code.
I strongly suggest that you create a function of the form:
Code:
double bearing( double lat1, double lon1, double lat2, double lat2 ) ;
to enhance the reusability of your code. I also suggest that you use a double precision value for PI that is good for fifteen significant figures rather than just six for a float. The standard library is defined for double in any case. There are few circumstances where it is a good idea to use single precision floating point.
Use:
#define PI 3.1415926535897931
#define PI_DEGREES 180
rather than relying on repeated magic numbers. So for example:
Code:
lat1=(a*PI)/PI_DEGREES;
December 25th, 2012, 01:06 AM

Originally Posted by clifford
I strongly suggest that you create a function of the form:
Code:
double bearing( double lat1, double lon1, double lat2, double lat2 ) ;
to enhance the reusability of your code. I also suggest that you use a double precision value for PI that is good for fifteen significant figures rather than just six for a float. The standard library is defined for double in any case. There are few circumstances where it is a good idea to use single precision floating point.
Use:
#define PI 3.1415926535897931
#define PI_DEGREES 180
rather than relying on repeated magic numbers. So for example:
Code:
lat1=(a*PI)/PI_DEGREES;
Thats a good idea, except the spherical method is not accurate enough to make use of all those significant numbers...
Now I'm going to try to get the vincenty's formula to work which uses a oblate spheroid model(most accurate) instead of a round spherical model...
Right now even if I use the doubles, there will be ±0.5 degree error in some cases, so its pointless.
December 31st, 2012, 04:21 AM

Originally Posted by zedeneye1
the spherical method is not accurate enough to make use of all those significant numbers...
...
Right now even if I use the doubles, there will be ±0.5 degree error in some cases, so its pointless.
The error is moreorless proportional of the distance between the points  for short distances you will benefit from doubles. Otherwise the resolution as well as the accuracy suffers. Try both float and double for distances a few meters apart and see what happens.
December 31st, 2012, 07:10 AM

Originally Posted by clifford
The error is moreorless proportional of the distance between the points  for short distances you will benefit from doubles. Otherwise the resolution as well as the accuracy suffers. Try both float and double for distances a few meters apart and see what happens.
how many bits is a double..?
wouldn't a "long long" be better...?
And I just try for two coordinates which were at a distance of about 20 meters...the result is accurate.
December 31st, 2012, 07:12 AM

December 31st, 2012, 07:20 AM

Originally Posted by dwise1_aol
its 64 bits...?
and the ordinary float is probably 32 right?