Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Issue to calculate distance between 2 towns ( gps coordinates )

edited July 2013 in Questions Posts: 257

The results are false. I don't know why. Perhaps because of codea rounded float. thanks for your answers.

Here is my source code :

-- GPS

function setup()
    Villes()
    update=0
end

function draw()
    if update==0 then aff() end
end

function aff()

    print("---------------------")

    -- Mémoire Utilisée 
    kb = math.floor(gcinfo() / 10) * 10
    val = tostring(kb).." kb"
    print(val)
    collectgarbage()

    -- Numéros des 2 villes
    vil1=2 ; vil2=3
    nomvil1=vil[vil1][1] ; nomvil2=vil[vil2][1]

    -- Calcul de la distance entre 2 points GPS

    lat1=vil[vil1][2] ; lon1=vil[vil1][3]
    lat2=vil[vil2][2] ; lon2=vil[vil2][3]

    print(vil1.." "..nomvil1.." "..lat1.." "..lon1) ; print(vil2.." "..nomvil2.." "..lat2.." "..lon2)

    rad=math.rad
    sin=math.sin ; cos=math.cos
    acos=math.acos ; asin=math.asin
    sqrt=math.sqrt ; pow=math.pow

    arc1=acos(sin(rad(lat1))*sin(rad(lon1))+cos(rad(lat1))*cos(rad(lon1))*cos(rad(lat2-lon2)))
    dist1=arc1*6366
    arc2=2*asin(sqrt(pow(sin((lat1-lat2)/2),2)+cos(lat1)*cos(lat2)*pow(sin((lon1-lon2)/2),2)))
    dist2=arc2*6366

    print("Arc 1 = "..arc1)
    print("Distance 1 = "..dist1.." km")
    print("Arc 2 = "..arc2)
    print("Distance 2 = "..dist2.." km")

    update=1

end

Comments

  • edited July 2013 Posts: 257
    function Villes()
    
    -- Coordonnées GPS des villes
    -- triées d'haut en bas
    -- puis de gauche à droite
    
    -- Format degré et 6 décimales
    -- Au lieu degré minute seconde
    
    
    -- Ville Latitude Longitude
    
    -- Latitude Nord Sud ( Equateur 0° )
    -- Longitude Est Ouest ( Méridien de Greenwich 0° )
    
    -- 5° par 5°
    
    vil={}
    
    -- Nord de la France
    
    vil[1]={"CALAIS",50.951210,1.858686}
    vil[2]={"PARIS",48.856614,2.352221}
    vil[3]={"NANCY",48.692054,6.184416}
    vil[4]={"METZ",49.119666,6.176905}
    vil[5]={"RENNES",48.113475,-1,675707}
    vil[6]={"LE MANS",48.006110,0,199556}
    
    -- Milieu de la France
    
    vil[500]={"ANGERS",47.474419,-0.563166}
    vil[501]={"TOURS",47.394144,0.684840}
    vil[502]={"DIJON",47.322047,5.041479}
    vil[503]={"NANTES",47.218371,-1.55}
    vil[504]={"POITIERS"}
    vil[505]={"LA ROCHELLE"}
    vil[506]={"LIMOGES"}
    
    -- Sud de la France
    
    vil[1001]={"LYON"}
    vil[1002]={"GRENOBLE"}
    vil[1003]={"BORDEAUX",44.837789,-0,579179}
    vil[1004]={"AGEN"}
    vil[1005]={"TOULOUSE"}
    vil[1006]={"MONTPELLIER"}
    vil[1007]={"AIX EN PROVENCE"}
    vil[1008]={"FREJUS"}
    vil[1009]={"CANNES"}
    vil[1010]={"NICE"}
    vil[1011]={"MONACO"}
    vil[1012]={"MARSEILLE",43.296482,5,369779}
    
    
    -- Autres Villes
    vil[5000]={"MONTMARTRE",48.872555,2.399054}
    vil[5001]={"USHUAIA",-54.814511,-68.307753}
    
    end
    
    
  • IgnatzIgnatz Mod
    edited July 2013 Posts: 5,396

    (Deleted)

  • Posts: 1,595

    How do we get round the language barrier? Can't help otherwise

  • Posts: 257

    Sorry for french language in source code.
    Villes function is data function containing gps coordinates ( latitude and longitude )
    it works but dist1 or dist2 give false result,
    i don't think it's because the formulas

  • Jmv38Jmv38 Mod
    Posts: 3,295

    @hpsoft you say 'result are false'. But what is the'right result' you expect? And what do you get insead?

  • Posts: 1,595

    If you use triple tidles

    Code
    

    It makes it more readable as I can't currently see a mistake seeming as half of it is in a different formatting, but from what I've been able to pull out so far is there is an extra bracket in dist2 I think that might be causing trouble?

  • Posts: 22

    First formula, switched around a few latitudes and longitudes

    arc1=acos(sin(rad(lat1))*sin(rad(lat2))+cos(rad(lat1))*cos(rad(lat2))*cos(rad(lon2-lon1)))
    dist1=arc1*6366
    

    Second formula, added rad() to everything

    arc2=2*asin(sqrt(pow(sin(rad(lat1-lat2)/2),2)+cos(rad(lat1))*cos(rad(lat2))*pow(sin(rad(lon1-lon2)/2),2)))
    dist2=arc2*6366
    

    These return 281.xx km for me which appears to be the correct distance between Paris and Nancy.

  • Posts: 257

    for example

    Paris to Nancy should give : 281,71 km ( note that 1 km = 0.621371192 miles )

    but the program give : 6551 km for first formula
    and 1017 km for second formula

    it's seems i have used triple tidles in the first source code

  • Posts: 257

    fine and fast answer.
    first farmula give me 281,165 and second 281,169
    very good
    thanks for your answers

  • Posts: 257

    http://www.01net.com/editorial/555908/astuce-excel-calculez-la-distance-entre-deux-points-de-la-terre/

    with vil1=5000 ; vil2=5001 ( Montmartre to Ushuaia )

    Good Result with both formulas too -> 13262,5684 km It's fine !!!

  • dave1707dave1707 Mod
    Posts: 7,604

    @hpsoft I thought I'd dig up one of my old programs and try your coordinates. I came up with about the same answers. To use this, enter the latitude and longitude in the input area at the same time seperated by a comma, then press RETURN ( see example coordinates below input area ). Don't forget to include the minus sign when needed. If you just press RETURN for city 1 without entering anything, it will keep the default coordinates or the last coordinates entered. In this example, New Youk City. The default can be changed to whatever city you want when the program starts.


    displayMode(FULLSCREEN) supportedOrientations(PORTRAIT) function setup()     lat1,lon1,lat2,lon2=40.7142,-74.0064,0,0     city=1     d1,d2=0,0 end function draw()     background(40,40,50)     if not isKeyboardShowing() then         showKeyboard()     end     if keyboardBuffer() ~= nil then         str=keyboardBuffer()     end     fill(255)     textMode(CORNER)     text("Latitude of city 1..  "..lat1,200,900)     text("Longitude of city 1..  "..lon1,200,870)     text("Latitude of city 2..  "..lat2,200,800)     text("Longitude of city 2..  "..lon2,200,770)     text("Miles between the 2 cities..  "..d1,200,700)     text("Kilometers between the 2 cities..  "..d2,200,670)     text("Enter latitude , longitude of city "..city,200,500)     text("then press RETURN",200,470)        text("example for NYC   40.7142,-74.0064",200,350)     rectMode(CENTER)     textMode(CENTER)     rect(300,430,200,50)     fill(0)     text(str,300,430) end function keyboard(k)     if k==RETURN then         hideKeyboard()         lat,lon=string.match(str,"([+-]?%d*%.*%d*),([+-]?%d*%.*%d*)")         if city==1 then             if lat~="" and lat~=nil then                 lat1=lat or 0                 lon1=lon or 0             end             d1,d2,lat2,lon2=0,0,0,0             city=2         elseif city==2 then             lat2=lat or 0             lon2=lon or 0             city=1             dlon = math.rad(lon2 - lon1)             dlat = math.rad(lat2 - lat1)              v=(math.sin(dlat/2))^2 +                 math.cos(math.rad(lat1)) *                  math.cos(math.rad(lat2)) *                  (math.sin(dlon/2))^2              c=2*math.atan2(math.sqrt(v),math.sqrt(1-v) )              d1=3961 * c             d2=6366 * c         end     end   end
Sign In or Register to comment.