Haversine Formula for Rosetta

Post Reply
User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Haversine Formula for Rosetta

Post by GeorgeMcGinn »

Here is some code for the Rosetta project.

This function calculates the distance between two points using their Latitude and Longitude.

This is close to the ASP sample, but adapted for SmartBASIC.

George.

Code: Select all

'*** LAT/LONG For Venice, FL
Lat1=27.07
Lon1=-82.44

'*** LAT/LONG For Sarasota, FL
Lat2=27.40   
Lon2=-82.55

'*** Units: K=kilometers  M=miles  N=nautical miles
Unit$ = "M"	

Result=Distance(Lat1,Lon1,Lat2,Lon2,Unit$)

PRINT "The Distance From Venice, FL to Sarasota, FL in Miles is: "&Result

DEF Distance(Lat1,Lon1,Lat2,Lon2,Unit$)
    PI=3.14159265358979323846
    Radius=6378.137
    Lat1=(Lat1*PI/180)
    Lon1=(Lon1*PI/180)
    Lat2=(Lat2*PI/180)
    Lon2=(Lon2*PI/180)
    DLon=Lon1-Lon2
    Answer=ACOS(SIN(Lat1)*SIN(Lat2)+COS(Lat1)*COS(Lat2)*COS(DLon))*Radius

    IF UNIT$="M" THEN Answer=Answer*0.621371192
    IF UNIT$="N" THEN Answer=Answer*0.539956803

  RETURN Answer
ENDDEF
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

User avatar
sarossell
Posts: 195
Joined: Sat Nov 05, 2016 6:31 pm
My devices: iPad Mini 2, iPhone 5, MacBook Air, MacBook Pro
Flag: United States of America
Contact:

Re: Haversine Formula for Rosetta

Post by sarossell »

Wow. You made that look easy. :)
smart BASIC Rocks!

- Scott : San Diego, California

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Haversine Formula for Rosetta

Post by GeorgeMcGinn »

There are a lot of samples in other languages.

Only found 3 (including ASP, which is VBScript) in a BASIC-dialect.

Since I need a function to calculate where the nearest city is using current GPS and a lookup table, I needed to be able to do this calculation.

Since I got 10 examples to work off of from Rosetta and others, I figured I would give back as well.

Thanks – George.
sarossell wrote:Wow. You made that look easy. :)
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Haversine Formula for Rosetta

Post by Henko »

See also function "geo_distance()" in matlib (library section)

Post Reply