Box the compass: Difference between revisions

+Java, fix VB.NET highlighting
(+Java, fix VB.NET highlighting)
Line 70:
 
Here, I compute the name (and index) for each of the numbers, and then find the unique list of names (and indices) represented in each row -- which is always only one name (and only one index) -- and convert the whole thing to characters.
=={{header|Java}}==
 
{{trans|Visual Basic .NET}}
<lang java>public class BoxingTheCompass{
private static String[] points = new String[32];
public static void main(String[] args){
buildPoints();
double heading = 0;
for(int i = 0; i<= 32;i++){
heading = i * 11.25;
switch(i % 3){
case 1:
heading += 5.62;
break;
case 2:
heading -= 5.62;
break;
default:
}
System.out.printf("%s\t%18s\t%s°\n",(i % 32) + 1, initialUpper(getPoint(heading)), heading);
}
}
private static void buildPoints(){
String[] cardinal = {"north", "east", "south", "west"};
String[] pointDesc = {"1", "1 by 2", "1-C", "C by 1", "C", "C by 2", "2-C", "2 by 1"};
String str1, str2, strC;
for(int i = 0;i <= 3;i++){
str1 = cardinal[i];
str2 = cardinal[(i + 1) % 4];
strC = (str1.equals("north") || str1.equals("south")) ? (str1 + str2): (str2 + str1);
for(int j = 0;j <= 7;j++){
points[i * 8 + j] = pointDesc[j].replace("1", str1).replace("2", str2).replace("C", strC);
}
}
}
private static String initialUpper(String s){
return s.substring(0, 1).toUpperCase() + s.substring(1);
}
private static String getPoint(double degrees){
double testD = (degrees / 11.25) + 0.5;
return points[(int)Math.floor(testD % 32)];
}
}</lang>
Output:
<pre>1 North 0.0°
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.5°
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.0°
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.5°
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.0°
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.5°
32 North by west 354.37°
1 North 354.38°</pre>
=={{header|Python}}==
<lang python>majors = 'north east south west'.split()
Line 196 ⟶ 280:
=={{header|Visual Basic .NET}}==
 
<lang visual basic .netvbnet>Module BoxingTheCompass
Dim _points(32) As String
 
Anonymous user