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:
Someone much clevered than me has posted this in javascript: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
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?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 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.
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
Not really but the required output seems to be logical. :-) Thanks!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks