Calculating distance

Discussion in 'Code Samples & Tips' started by abastien, May 6, 2008.

  1. abastien

    abastien Member Licensed User

    Here is some sample code to calculate distance between two points:

    Code:
    Sub getDistanceKM(lat1, long1, lat2, long2)

       DEGREES_TO_RADIANS = (
    cPI / 180.0)
       EARTH_RADIUS = 
    6371.0
       
       rlat1 = DEGREES_TO_RADIANS * lat1
       rlong1 = DEGREES_TO_RADIANS * long1
       rlat2 = DEGREES_TO_RADIANS * lat2
       rlong2 = DEGREES_TO_RADIANS * long2
       
       
    '   There is no real reason To break this lot into 
       '   4 statements but I just feel it's a little more 
       '   readable.
       p1 = Cos(rlat1) * Cos(rlong1) * Cos(rlat2) * Cos(rlong2)
       p2 = 
    Cos(rlat1) * Sin(rlong1) * Cos(rlat2) * Sin(rlong2)
       p3 = 
    Sin(rlat1) * Sin(rlat2)

       ret = p1 + p2 + p3
       
    If ret = 1 Then Return 0
       
       ret = 
    ACos(ret)
       ret = ret * EARTH_RADIUS
       
       
    Return ret
       
    End Sub
    Can we add this to the gps.dll?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Generally speaking, libraries should only include functions that it isn't possible to implement from code.
     
  3. abastien

    abastien Member Licensed User

    I was thinking of the processing power. Would complex calculations not run faster in a library?

    Just a thought - not based on fact.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Calculations will run faster in a library but the difference should not be too significant.
     
  5. Bruno

    Bruno Member Licensed User

    Hi there,

    Just a little correction for your code

    " If ret >1 Then Return 0 "

    My tests give me sometime 1.0000000002 :)

    Bye,

    Bruno.
     
  6. MM2forever

    MM2forever Active Member Licensed User

    Am I right assuming that a decimal lat/lon input is required?
    I think the other (dddmm.mm) format is giving me false distances...
     
    Last edited: Jan 8, 2009
  7. klaus

    klaus Expert Licensed User

  8. MM2forever

    MM2forever Active Member Licensed User

    Thanks for the info klaus,
    I was already searching for the mathematical/physical/geographical basis of this routine, since for me, using existing routines is great, but i like to understand what im using :)

    edit://
    arent all functions of the gps lib (at least in the version i have which i guess is the latest/only one) replaceable by string operations that can be done with the b4p standart string functions set?
     
    Last edited: Jan 9, 2009
  9. Erel

    Erel Administrator Staff Member Licensed User

    Yes.
    Every statement has one or two exceptions ;)

    If I were to write the GPS library today I would have written it as a module instead of a library. That would have made it easier to enhance it and parse other NMEA sentences.
    The GPS library was written before Basic4ppc V6.50 and therefore modules weren't available at that time.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice