1. distance point to point revisited

-- 
-- distance.e 
-- 
include std/math.e 
 
-- Great Circle Method 
global function distance(atom LA1, atom LO1, atom LA2, atom LO2, integer miles = 1) 
-- given starting and destination lattitude and longitude coordinates in degrees 
-- returns distance between the two points (default: miles = 1, or km (miles = 0) 
-- South latitudes are negative / East longitudes are positive 
-- uses spherical law of cosines 
     
    atom D, DLO, R =  6371.02   -- mean earth radius in km                    
    DLO = deg2rad(LO2-LO1) 
    LA1 =  deg2rad(LA1) LO1 = deg2rad(LO1)-- initial location coordinates  
    LA2 =  deg2rad(LA2) LO2 = deg2rad(LO2)-- destination location coordinates 
     
    D = arccos( sin(LA1) * sin(LA2) + cos(LA1) * cos(LA2) * cos(DLO)) * R 
  
    if miles then 
        D = D / 1.609 -- convert km to miles 
    end if 
 
return D 
end function 
 
-- test 
? distance(41.3, 174.7, 37.0, 174.7)    -- Wellington-Auckland 
? distance(41.3, 174.7, 33.7, 151.3)    -- Wellington-Sydney 
 
atom LA1, LA2, LO1, LO2 
LA1 =  32.204287  LO1 =  82.322732  -- Lyons, GA -- starting location 
LA2 =  51.5074    LO2 =   0.1278    -- London, UK 
? distance(LA1,LO1, LA2, LO2) 
LA2 = 33.7490 LO2= 84.3880   -- Lyons, GA to Atlanta, GA 
? distance(LA1,LO1,LA2,LO2) 
LA2 = 32.8407 LO2 = 83.6324  -- Lyons, GA to Macon, GA 
? distance(LA1,LO1,LA2,LO2) 
LA2 = 32.2177 LO2 = 82.4135  -- Lyons, GA to Vidalia, GA 
? distance(LA1,LO1,LA2,LO2) 
LA2 = 32.0522 LO2 = 118.2437 -- Lyons, GA to Los Angeles, California 
? distance(LA1,LO1,LA2,LO2) 
 
new topic     » topic index » view message » categorize

2. Re: distance point to point revisited

This function seems to be a more accurate than the Great Circle Method. See: Haversine Formula and Calc-Distance

global function distance(atom LA1, atom LO1, atom LA2, atom LO2, integer miles = 1) 
-- Haversine Formula  
 
atom a, c, D, DLO,  DLA, R = 6371.02 -- mean earth radius in km 
 
    DLO =  deg2rad(LO2-LO1)  DLA = deg2rad(LA2-LA1) 
    LA1 =  deg2rad(LA1)  LO1 = deg2rad(LO1) -- initial location coordinates  
    LA2 =  deg2rad(LA2)  LO2 = deg2rad(LO2) -- destination location coordinates 
 
    a = power(sin(DLA/2),2)  + cos(LA1) * cos(LA2) * power(sin(DLO/2),2) 
    c = 2 * arcsin(min({1,sqrt(a)})) 
    D = R * c 
    
    if miles then 
        D/=1.609      -- convert km to miles   
    end if 
     
return D 
end function 
 
 
-- test 
-- the distance reading refers to the Haversine Formula 
 
LA1=32.204287 LO1=82.322732 -- Lyons, GA - starting location 
 
LA2=32.0835 LO2=81.0998   -- Savannah, GA USA  -- 72.0542 
? Great_Circle_Distance(LA1,LO1, LA2, LO2,u)   -- 72.12361789                  
? distance(LA1,LO1, LA2, LO2,u)                -- 72.04468674                    
         
LA2=21.3069 LO2=157.8583 -- Honolulu, USA      -- 4627 
? Great_Circle_Distance(LA1,LO1, LA2, LO2,u)   -- 4636 
? distance(LA1,LO1, LA2, LO2,u)                -- 4631                              
         
LA2=51.5074 LO2=0.1278 -- Lyons, GA to London, UK -- 4195 
? Great_Circle_Distance(LA1,LO1, LA2, LO2,u)      -- 4202                            
? distance(LA1,LO1, LA2, LO2,u)                   -- 4198                                        
         
LA2=-22.9068 LO2=43.1729-- Lyons, GA to Rio de Janero -- 4599 
? Great_Circle_Distance(LA1,LO1, LA2, LO2,u)          -- 4608.276053                                   
? distance(LA1,LO1, LA2, LO2,u)                       -- 4603.232816                                                       
 
 
new topic     » goto parent     » topic index » view message » categorize

3. Re: distance point to point revisited

Senator said...

but I didn't know how to translate min(1,sqrt(a)) to euphoria

OE has a min function, so at a guess it is just

min({1,sqrt(a)}) 
new topic     » goto parent     » topic index » view message » categorize

4. Re: distance point to point revisited

Thanks, Pete! smile

I edited the Haversine code in the previous post and included some sample run data.

The first number I got from a search engine The second number is the Great Circle code results The third number is the Haversine code results

The Haversine code appears to run 3-4 miles over the results obtained from the web for distances 4000+ miles

~0.0009

Regards, Ken

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu