Sub MainStartTest
Dim lat1 As Double = 52.419242
Dim lon1 As Double = -1.770847
Dim lat2 As Double = 52.539881
Dim lon2 As Double = -1.898292
Log(CourseFromLonLatDegrees(lat1, lon1, lat2, lon2))
End Sub
Sub CourseFromLonLatDegrees(Lat1 As Double, Lon1 As Double, Lat2 As Double, Lon2 As Double) As Double
Dim DegreesToRadians As Double = cPI / 180
Lat1 = Lat1 * DegreesToRadians
Lon1 = Lon1 * DegreesToRadians
Lat2 = Lat2 * DegreesToRadians
Lon2 = Lon2 * DegreesToRadians
Return CourseFromLonLatRadians(Lat1, Lon1, Lat2, Lon2) / DegreesToRadians
End Sub
Sub CourseFromLonLatRadians(Lat1 As Double, Lon1 As Double, Lat2 As Double, Lon2 As Double) As Double
Dim tc1 As Double
'formula from: https://edwilliams.org/avform.htm#Crs
If (Cos(Lat1) < 0.0000001) Then 'within one metre of north or south pole
If (Lat1 > 0) Then
tc1 = cPI 'starting from north pole, thus heading south
Else
tc1= 0 'starting from south pole, thus heading north
End If
Else
'Lon1 and Lon2 reversed in subtraction due to formula convention of west = +ve
tc1 = ATan2(Sin(Lon2 - Lon1) * Cos(Lat2), Cos(Lat1) * Sin(Lat2) - Sin(Lat1) * Cos(Lat2) * Cos(Lon2 - Lon1))
'translate to compass angle ie 0 = north, positive through E, S, W
If tc1 < 0 Then
tc1 = tc1 + 2 * cPI
End If
End If
Return tc1
End Sub