Need help calculating azimuth with Atan2
I have been pulling my hair out I've been so frustrated with this one! I need to calculate the Azimuth or true north bearing between two sets of geographic coordinates in Microsoft Access. I found the following code by doing a google search but its not giving me correct results! Half the time its accurate and the other half it gives me a weird random angle. Heres the code:
public function fAzimuth(lat1 as double, lat2 as double, lon1 as double, lon2 as double) as double
dim Arctan as double
dim x as double
dim y as double
x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) *cos(lon2 - lon1)
y = sin(lon2 - lon1) * cos(lat2)
Arctan = excel.application.Atan2(x, y)
If Arctan < 0 and lon1 > lon2 then
fAzimuth = (excel.application.degrees(Arctan) + 360) mod 360
else
fAzimuth = 360 - ((excel.application.degrees(Arctan) + 360) mod 360
end if
end function
Here is an example of it giving me a weird random angle:
?fAzimuth(36.07553, 36.1414, 66.83867, 66.90865)
359
It says the angle is 359 degrees but when you plot both points, it's obvious that the angle should be closer to 45 degrees or 315 degrees if its going counter clockwise. I have no idea how it came up with this angle!
Any help will be greatly appreciated.
Re: Need help calculating azimuth with Atan2
You're entering lat/long values in degrees, but the trig functions expect them in radians.
Re: Need help calculating azimuth with Atan2
Quote:
Originally Posted by
jemidiah
You're entering lat/long values in degrees, but the trig functions expect them in radians.
Some extensions that might help:
Code:
Imports System.Runtime.CompilerServices
Module DegreeRadianConversions
<Extension()>
Public Function ToRadians(ByVal degrees As Double) As Double
Return degrees * Math.PI / 180
End Function
<Extension()>
Public Function ToDegrees(ByVal radians As Double) As Double
Return radians * 180 / Math.PI
End Function
<Extension()>
Public Function ToGrads(ByVal radians As Double) As Double
Return radians * 200 / Math.PI
End Function
<Extension()>
Public Function ToTurns(ByVal radians As Double) As Double
Return radians / 2 / Math.PI
End Function
End Module
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim deg As Double = 360
Dim rad As Double
Dim grad As Double
Dim turn As Double
rad = deg.ToRadians
turn = rad.ToTurns
grad = rad.ToGrads
End Sub
End Class