RosettaCodeData/Task/Haversine-formula/FreeBASIC/haversine-formula.basic

37 lines
1.2 KiB
Plaintext

' version 09-10-2016
' compile with: fbc -s console
' Nashville International Airport (BNA) in Nashville, TN, USA,
' N 36°07.2', W 86°40.2' (36.12, -86.67)
' Los Angeles International Airport (LAX) in Los Angeles, CA, USA,
' N 33°56.4', W 118°24.0' (33.94, -118.40).
' 6372.8 km is an approximation of the radius of the average circumference
#Define Pi Atn(1) * 4 ' define Pi = 3.1415..
#Define deg2rad Pi / 180 ' define deg to rad 0.01745..
#Define earth_radius 6372.8 ' earth radius in km.
Function Haversine(lat1 As Double, long1 As Double, lat2 As Double, _
long2 As Double , radius As Double) As Double
Dim As Double d_long = deg2rad * (long1 - long2)
Dim As Double theta1 = deg2rad * lat1
Dim As Double theta2 = deg2rad * lat2
Dim As Double dx = Cos(d_long) * Cos(theta1) - Cos(theta2)
Dim As Double dy = Sin(d_long) * Cos(theta1)
Dim As Double dz = Sin(theta1) - Sin(theta2)
Return Asin(Sqr(dx*dx + dy*dy + dz*dz) / 2) * radius * 2
End Function
Print
Print " Haversine distance between BNA and LAX = "; _
Haversine(36.12, -86.67, 33.94, -118.4, earth_radius); " km."
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End