Box the compass: Difference between revisions

Content added Content deleted
(→‎Tcl: Added implementation)
(→‎{{header|Visual Basic .NET}}: Added implementation)
Line 142: Line 142:
32 North by west 354.37°
32 North by west 354.37°
1 North 354.38°
1 North 354.38°
</pre>

=={{header|Visual Basic .NET}}==

<lang visual basic .net>Module BoxingTheCompass
Dim _points(32) As String

Sub Main()
BuildPoints()

Dim heading As Double = 0D

For i As Integer = 0 To 32
heading = i * 11.25
Select Case i Mod 3
Case 1
heading += 5.62
Case 2
heading -= 5.62
End Select

Console.WriteLine("{0,2}: {1,-18} {2,6:F2}°", (i Mod 32) + 1, InitialUpper(GetPoint(heading)), heading)
Next
End Sub

Private Sub BuildPoints()
Dim cardinal As String() = New String() {"north", "east", "south", "west"}
Dim pointDesc As String() = New String() {"1", "1 by 2", "1-C", "C by 1", "C", "C by 2", "2-C", "2 by 1"}

Dim str1, str2, strC As String

For i As Integer = 0 To 3
str1 = cardinal(i)
str2 = cardinal((i + 1) Mod 4)
strC = IIf(str1 = "north" Or str1 = "south", str1 & str2, str2 & str1)
For j As Integer = 0 To 7
_points(i * 8 + j) = pointDesc(j).Replace("1", str1).Replace("2", str2).Replace("C", strC)
Next
Next
End Sub

Private Function InitialUpper(ByVal s As String) As String
Return s.Substring(0, 1).ToUpper() & s.Substring(1)
End Function

Private Function GetPoint(ByVal Degrees As Double) As String
Dim testD As Double = (Degrees / 11.25) + 0.5
If testD > 32 Then testD -= 32
Return _points(CInt(Math.Floor(testD)))
End Function
End Module
</lang>
Output:
<pre>
1: North 0.00°
2: North by east 16.87°
3: North-northeast 16.88°
4: Northeast by north 33.75°
5: Northeast 50.62°
6: Northeast by east 50.63°
7: East-northeast 67.50°
8: East by north 84.37°
9: East 84.38°
10: East by south 101.25°
11: East-southeast 118.12°
12: Southeast by east 118.13°
13: Southeast 135.00°
14: Southeast by south 151.87°
15: South-southeast 151.88°
16: South by east 168.75°
17: South 185.62°
18: South by west 185.63°
19: South-southwest 202.50°
20: Southwest by south 219.37°
21: Southwest 219.38°
22: Southwest by west 236.25°
23: West-southwest 253.12°
24: West by south 253.13°
25: West 270.00°
26: West by north 286.87°
27: West-northwest 286.88°
28: Northwest by west 303.75°
29: Northwest 320.62°
30: Northwest by north 320.63°
31: North-northwest 337.50°
32: North by west 354.37°
1: North 354.38°
</pre>
</pre>