How to convert a degree to a compass direction in VB.NET

1 Answer

0 votes
Imports System

' 0° → North
' 45° → North-East
' 90° → East
' 135° → South-East
' 180° → South
' 225° → South-West
' 270° → West
' 315° → North-West
'

Module CompassDirection

    Function DegreesToDirection(degrees As Double) As String
        ' Normalize degrees to (0, 360)
        degrees = (degrees Mod 360 + 360) Mod 360

        ' Define compass directions
        Dim directions() As String = {
            "North", "North-East", "East", "South-East",
            "South", "South-West", "West", "North-West"
        }

        ' Each direction covers 45 degrees
        Dim index As Integer = CInt(Math.Round(degrees / 45)) Mod 8

        Return directions(index)
    End Function

    Sub Main()
        Dim degrees As Double = 120

        Console.WriteLine($"For {degrees} degrees, Compass direction: {DegreesToDirection(degrees)}")
    End Sub

End Module



' run:
'
' For 120 degrees, Compass direction: South-East
'


 



answered 4 days ago by avibootz
edited 4 days ago by avibootz
...