logo VB.Net - Compass Points
Guide Contents
DigitalDan Home Page

Compass Bearing to Compass Point

Convert a compass bearing (e.g. 22.5 degrees) to a compass point (e.g. NNW) The input patameters are
Bearing = Double (-180.0 to 360.0)
compassPoints = Integer (4, 8, 16 or 32)
fullName = Boolean (can be omitted)
... True = points in full (North by North West)
... False = points abbreviated (NxNW)
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

Compass Point to Compass Bearing
Convert a Compass Point to a compass beariing in degrees (0.0 to 360.0) The function expects point names (or point abbreviations) to be in English (North, West ..) but could easily be adapted for other languages (Nord, Ouest ...) The input parameter is
compassPoint = Point Name (e.g. "North West") or abbreviation (e.g. NW)
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

DigitalDan.co.uk ... Hits = 33