Results 1 to 3 of 3

Thread: Need help calculating azimuth with Atan2

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    1

    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.

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Need help calculating azimuth with Atan2

    You're entering lat/long values in degrees, but the trig functions expect them in radians.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Need help calculating azimuth with Atan2

    Quote Originally Posted by jemidiah View Post
    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width