+ Reply to Thread
Results 1 to 3 of 3

Thread: Translate from javascript to VBA

  1. #1
    Forum Contributor
    Join Date
    06-16-2011
    Location
    London
    MS-Off Ver
    Excel XP
    Posts
    189

    Translate from javascript to VBA

    This one is beyond me.

    I'm trying to work out the compass bearing of a direct (rhumb line, constant bearing) route between two points on a sphere using latitude and longitude in radians. I've found a formula online but don't understand a word of it:

    Formula:	Δφ = ln(tan(lat2/2+π/4)/tan(lat1/2+π/4))	 [= the ‘stretched’ latitude difference]
    if E:W line,	q = cos(lat1)	 
    otherwise,	q = Δlat/Δφ	 
     	d = √(Δlat² + q².Δlon²).R	[pythagoras]
     	θ = atan2(Δlon, Δφ)	 
     	where ln is natural log, Δlon is taking shortest route (<180º), and R is the earth’s radius
    Someone much clevered than me has posted this in javascript:

    
    var dPhi = Math.log(Math.tan(lat2/2+Math.PI/4)/Math.tan(lat1/2+Math.PI/4));
    var q = (!isNaN(dLat/dPhi)) ? dLat/dPhi : Math.cos(lat1);  // E-W line gives dPhi=0
    
    // if dLon over 180° take shorter rhumb across 180° meridian:
    if (Math.abs(dLon) > Math.PI) {
      dLon = dLon>0 ? -(2*Math.PI-dLon) : (2*Math.PI+dLon);
    }
    var d = Math.sqrt(dLat*dLat + q*q*dLon*dLon) * R;
    var brng = Math.atan2(dLon, dPhi);
    And I dont speak java. I vaguely get the first line but don't understand the var q = line at all. Does anyone speak Java in here?

    And yes, I realise I've put a formula in [ code ] tags but it may as well be in code.....a 64bit encrypted code.
    Last edited by swoop99; 01-31-2012 at 11:03 AM.

  2. #2
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Translate from javascript to VBA

    I'm not going to even try to understand the maths, but the js above can be written as the below, hope it makes more sense to you than me!
    Dim pi As Double
    
    Sub JStoVBA()
    Dim dPhi As Double
    Dim brng As Double
    Dim dLon As Double
    Dim dLat As Double
    Dim q As Double
    Dim R As Double
    Dim d As Double
    Dim lat2
    Dim lat1
    
    
    pi = 4 * Atn(1)
    
    'Formula
    dPhi = Log(Tan(lat2 / 2 + pi / 4) / Tan(lat1 / 2 + pi / 4))
    
    'IF E:W Line
    On Error Resume Next
    If dLat / dPhi Then
        q = Cos(lat1)
    Else
    'OtherWise
        q=dLat/dPhi
    End If
    Err.Clear
    
    If Abs(dLon) > pi Then
        If dLon > 0 Then
            dLon = -(2 * pi - dLon)
        Else
            dLon = (2 * pi + dLon)
        End If
    End If
    
    d = Sqr(dLat * dLat + q * q * dLon * dLon) * R
    
    brng = Atan2(dLon, dPhi)
    
            
    
    End Sub
    
    Function Atan2(y As Double, x As Double) As Double
    
    If x > 0 Then
        Atan2 = Atn(y / x)
      ElseIf x < 0 Then
        Atan2 = Sgn(y) * (pi - Atn(Abs(y / x)))
      ElseIf y = 0 Then
        Atan2 = 0
      Else
        Atan2 = Sgn(y) * pi / 2
      End If
    End Function
    Last edited by Kyle123; 01-31-2012 at 04:26 AM.
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  3. #3
    Forum Contributor
    Join Date
    06-16-2011
    Location
    London
    MS-Off Ver
    Excel XP
    Posts
    189

    Re: Translate from javascript to VBA

    Not really but the required output seems to be logical. :-) Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0