Guide Contents
|
DigitalDan Home Page
|
Private Shared Function Bearing_To_Compass(bearing As Double, compassPoints As Integer, Optional fullName As Boolean = False) As String
Dim ListPoints As New List(Of String)
Dim ret As String
ListPoints.AddRange({"N", "NxE", "NNE", "NExN", "NE", "NExE", "ENE", "ExN"})
ListPoints.AddRange({"E", "ExS", "ESE", "SExE", "SE", "SExS", "SSE", "SxE"})
ListPoints.AddRange({"S", "SxW", "SSW", "SWxS", "SW", "SWxW", "WSW", "WxS"})
ListPoints.AddRange({"W", "WxN", "WNW", "NWxN", "NW", "NWxN", "NNW", "NxW"})
Select Case compassPoints
Case 4, 8, 16, 32
Case Else : compassPoints = 32
End Select
Dim offset As Double = 180 / compassPoints
Dim temp1 As Double = bearing + offset
While temp1 < 0 : temp1 += 360.0 : End While
temp1 = temp1 Mod 360
Dim temp2 As Double = 360 / compassPoints
Dim cPoint As Integer = CInt(Math.Floor(temp1 / temp2))
cPoint = cPoint Mod 128
cPoint *= 32 \ compassPoints
ret = ListPoints(cPoint)
If Not fullName Then Return ret
ret = ret.Replace("N", "North ").Replace("W", "West ")
ret = ret.Replace("S", "South ").Replace("E", "East ")
ret = ret.Replace("x", "By ").Trim()
Return ret
End Function
Private Shared Function Compass_To_Bearing(compassPoint As String) As Double
' convert compassPoint to the standardised short form
compassPoint = compassPoint.ToUpper
compassPoint = compassPoint.Replace("WES", "W")
compassPoint = compassPoint.Replace("WE", "W")
compassPoint = compassPoint.Replace("EAS", "E")
compassPoint = compassPoint.Replace("B", "X")
Dim regex As New System.Text.RegularExpressions.Regex("[^NWSEX]")
compassPoint = regex.Replace(compassPoint, "")
' Get list of possiblew compass points
Dim ListPoints As New List(Of String)
ListPoints.AddRange({"N", "NXE", "NNE", "NEXN", "NE", "NEXE", "ENE", "EXN"})
ListPoints.AddRange({"E", "EXS", "ESE", "SEXE", "SE", "SEXS", "SSE", "SXE"})
ListPoints.AddRange({"S", "SXW", "SSW", "SWXS", "SW", "SWXW", "WSW", "WXS"})
ListPoints.AddRange({"W", "WXN", "WNW", "NWXN", "NW", "NWXN", "NNW", "NXW"})
' Get point number
Dim pointNumber As Integer = ListPoints.IndexOf(compassPoint)
If pointNumber < 0 Then Return -1
' -1 used to indicate invalid compass point name entered
Return Math.Round(pointNumber * 360 / 32, 6)
End Function