Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    82
    Rep Power
    2

    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.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Read this
    Just remember to turn your GPS degrees.minutes.seconds into radians before calling the math functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    82
    Rep Power
    2
    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...
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    82
    Rep Power
    2
    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.
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,111
    Rep Power
    1803
    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


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

    Join Date
    Dec 2012
    Posts
    82
    Rep Power
    2
    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)))) ;
    
        printf("%f", head);
    }

    Comments on this post

    • salem agrees : Good first effort - well done for trying
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    82
    Rep Power
    2
    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;
    
        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);
    }
  18. #10
  19. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,111
    Rep Power
    1803
    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;
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    82
    Rep Power
    2
    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.
  22. #12
  23. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,111
    Rep Power
    1803
    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.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    82
    Rep Power
    2
    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.
  26. #14
  27. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,171
    Rep Power
    2222
    Originally Posted by zedeneye1
    how many bits is a double..?
    http://en.wikipedia.org/wiki/IEEE-754
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

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

    and the ordinary float is probably 32 right?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo