Box the compass: Difference between revisions

(Added XPL0 example.)
Line 2,746:
1 North 354.38</syntaxhighlight>
=={{header|Java}}==
<syntaxhighlight lang="java">
{{trans|Visual Basic .NET}}
<syntaxhighlight lang="java">public class BoxingTheCompassBoxTheCompass {
privatepublic static void main(String[] pointsargs) = new String[32];{
float heading;
for (int index = 0; index <= 32; index++) {
public static void main(String[] args){
buildPoints() heading = index * 11.25f;
switch (index % 3) {
double case 1 -> heading += 05.62;
case 2 -> heading -= 5.62;
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("%-2d ", index % 32);
/* we could also use 'Compass.values()[index % 32]' here */
System.out.printf("%s\t%18s\t%s°\n",(i % 32) + 1, initialUpper(getPoint(heading)), heading);
System.out.printf("%-20s", Compass.parse(heading));
System.out.printf("%6.2f%n", heading);
}
}
 
public enum Compass {
private static void buildPoints(){
String[]N, cardinalNbE, =NNE, {"north"NEbN, "east"NE, "south"NEbE, "west"};ENE, EbN,
String[]E, pointDesc = {"1"EbS, "1 by 2"ESE, "1-C"SEbE, "C by 1"SE, "C"SEbS, "C by 2"SSE, "2-C"SbE, "2 by 1"};
S, SbW, SSW, SWbS, SW, SWbW, WSW, WbS,
StringW, WbN, WNW, NWbW, NW, str1NWbN, str2NNW, strCNbW;
 
for(intpublic ifloat =midpoint() 0;i <= 3;i++){
str1float midpoint = cardinal[i](360 / 32f) * ordinal();
str2return midpoint == cardinal[(i0 +? 1)360 %: 4]midpoint;
}
strC = (str1.equals("north") || str1.equals("south")) ? (str1 + str2): (str2 + str1);
 
for(int j = 0;j <= 7;j++){
public float[] bounds() {
points[i * 8 + j] = pointDesc[j].replace("1", str1).replace("2", str2).replace("C", strC);
float bound = (360 / 32f) / 2f;
float midpoint = midpoint();
float boundA = midpoint - bound;
float boundB = midpoint + bound;
if (boundB > 360) boundB -= 360;
return new float[] { boundA, boundB };
}
 
public static Compass parse(float degrees) {
float[] bounds;
float[] boundsN = N.bounds();
for (Compass value : Compass.values()) {
bounds = value.bounds();
if (degrees >= boundsN[0] || degrees < boundsN[1])
return N;
if (degrees >= bounds[0] && degrees < bounds[1])
return value;
}
return null;
}
 
@Override
public String toString() {
String[] strings = new String[name().length()];
int index = 0;
for (char letter : name().toCharArray()) {
switch (letter) {
case 'N' -> strings[index] = "north";
case 'E' -> strings[index] = "east";
case 'S' -> strings[index] = "south";
case 'W' -> strings[index] = "west";
case 'b' -> strings[index] = "by";
}
index++;
}
String string
= strings[0].substring(0, 1).toUpperCase() +
strings[0].substring(1);
switch (strings.length) {
case 2 -> string += strings[1];
case 3 -> {
if (strings[1].equals("by")) {
string += " %s %s".formatted(strings[1], strings[2]);
} else {
string += "-%s%s".formatted(strings[1], strings[2]);
}
}
case 4 -> {
string += String.join(" ", strings[1], strings[2], strings[3]);
}
}
return string;
}
}
}
</syntaxhighlight>
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)];
}
}</syntaxhighlight>
Output:
<pre>
<pre>1 North 0.0°
0 North 0.00
2 North by east 16.87°
3 1 North-northeast by east 16.88°87
2 North-northeast 16.88
4 Northeast by north 33.75°
3 Northeast by north 33.75
5 Northeast 50.62°
6 4 Northeast by east 50.63°62
5 Northeast by east 50.63
7 East-northeast 67.5°
6 East-northeast 67.50
8 East by north 84.37°
9 7 East by north East 84.38°37
8 East 84.38
10 East by south 101.25°
9 East by south 101.25
11 East-southeast 118.12°
10 East-southeast 118.12
12 Southeast by east 118.13°
11 Southeast by east 118.13
13 Southeast 135.0°
14 12 Southeast by south 151 135.87°00
13 Southeast by south 151.87
15 South-southeast 151.88°
14 South-southeast 151.88
16 South by east 168.75°
17 15 South by east South 185168.62°75
18 16 South South by west 185.63°62
17 South by west 185.63
19 South-southwest 202.5°
18 South-southwest 202.50
20 Southwest by south 219.37°
19 Southwest by south 219.37
21 Southwest 219.38°
22 20 Southwest by west 236 219.25°38
21 Southwest by west 236.25
23 West-southwest 253.12°
24 22 West-southwest West by south 253.13°12
25 23 West by south West 270253.13
24 West 270.00
26 West by north 286.87°
25 West by north 286.87
27 West-northwest 286.88°
26 West-northwest 286.88
28 Northwest by west 303.75°
27 Northwest by west 303.75
29 Northwest 320.62°
30 28 Northwest by north 320.63°62
29 Northwest by north 320.63
31 North-northwest 337.5°
30 North-northwest 337.50
32 North by west 354.37°
1 31 North by west North 354.38°</pre>37
0 North 354.38
</pre>
 
=={{header|JavaScript}}==
===ES5===
118

edits