### Thread: A program to calculate heading between two gps co-ordinates?!!!

Page 1 of 2 12 Last
1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
108
Rep Power
6

#### A program to calculate heading between two gps co-ordinates?!!!

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.
Just remember to turn your GPS degrees.minutes.seconds into radians before calling the math functions.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
108
Rep Power
6
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.

4. > 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 pseudo-code AND also provides you with test code to check against. It doesn't get any better than that, unless you want to be totally spoon-fed.

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.
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
108
Rep Power
6
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 pseudo-code AND also provides you with test code to check against. It doesn't get any better than that, unless you want to be totally spoon-fed.

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.
6. A number of formulae and Javascript code are presented here:http://www.movable-type.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

I see I have more-or-less repeated Salem's answer!
Last edited by clifford; December 24th, 2012 at 08:27 AM.
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
108
Rep Power
6
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=lon2-lon1;
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)))) ;

}```

#### Comments on this post

• salem agrees : Good first effort - well done for trying
8. > londif=lon2-lon1;
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.
9. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
108
Rep Power
6
Originally Posted by salem
> londif=lon2-lon1;
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=lon2-lon1;

finalans2=0;
if(finalans<=0)
{
finalans2=finalans+360;
printf("%f", finalans2);
}
else
printf("%f", finalans);
}```
10. 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;`
11. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
108
Rep Power
6
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.
12. 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 more-or-less 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.
13. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
108
Rep Power
6
Originally Posted by clifford
The error is more-or-less 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 co-ordinates which were at a distance of about 20 meters...the result is accurate.
14. Originally Posted by zedeneye1
how many bits is a double..?
http://en.wikipedia.org/wiki/IEEE-754
15. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
108
Rep Power
6
Originally Posted by dwise1_aol
its 64 bits...?

and the ordinary float is probably 32 right?
Page 1 of 2 12 Last