Since atan2 returns values in the range -π ... +π (that is, -180° ... +180°), to normalise the result to a compass bearing (in the range 0° ... 360°, with -ve values transformed into the range 180° ... 360°), convert to degrees and then use (θ+360) % 360, where % is modulo.
Above para i found through the net.i'm using VB6 and the my codes are like this
brng=(θ+360) Mod 360
(where 0= - 68) I'm getting the answer 292°,but expected answer is 248°.
am i committing a mistake..? or am I missing something.? Please help me.
Update:
I'll further explain my question,
Dim b As Double
Dim x As Double
Dim y As Double
Dim Dlat As Double
Dim DLon As Double
Private Const pi As Double = 3.14159265358979
Public Function Atn2(ByVal y As Double, ByVal x As Double) As Double
On Error GoTo DivideError
Atn2 = Atn(y / x)
If (x < 0) Then
If (y < 0) Then Atn2 = Atn2 - vbPI Else Atn2 = Atn2 + vbPI
End If
Exit Function
DivideError:
If Abs(y) > Abs(x) Then 'Must be an overflow
If y > 0 Then Atn2 = vbPI / 2 Else Atn2 = -vbPI / 2
Else
Atn2 = 0 'Must be an underflow
End If
Resume Next
End Function
Public Sub AFAMP()
lat1 = Val(Text1.Text) * pi / 180 'conveting to radians
lat2 = Val(Text2.Text) * pi / 180
Long1 = Val(Text3.Text) * pi / 180
Long2 = Val(Text4.Text) * pi / 180
Dlat = (lat1 - lat2)
DLon = (Long1 - Long2)
y = Math.Sin(DLon) * Math.Cos(lat2)
x = Math.Cos(lat1) * Math.Sin(lat2) - Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(DLon)
b = Atn2(y, x) * (180 / pi)
Text5.Text = (b +360) mod 360
End Sub
coordinates are as below in decimal degrees
lat1 = Val(7.337361) * pi / 180
lat2 = Val(7.000667) * pi / 180
Long1 = Val(81.626198) * pi / 180
Long2 = Val(80.773737) * pi / 180
I have checked with an online coordinate calculator, the answer is 248. In my case it is 292 degrees. Can anyone please help me? I'm stuck.
-68 + 360 = 292You seem to be going the wrong way around. - Deanna