Box the compass: Difference between revisions

Content added Content deleted
(Add Factor)
(→‎{{header|Nim}}: Solution that matches the task)
Line 4,316: Line 4,316:


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>import strfmt
<lang nim>import math, sequtils, strformat, strutils


const names = [
const
headingNames: array[1..32, string] = [
"North", "North by east", "North-northeast", "Northeast by north",
"Northeast", "Northeast by east", "East-northeast", "East by north",
"North", "North by east", "North-northeast", "Northeast by north",
"East", "East by south", "East-southeast", "Southeast by east",
"Northeast", "Northeast by east", "East-northeast", "East by north",
"Southeast", "Southeast by south","South-southeast", "South by east",
"East", "East by south", "East-southeast", "Southeast by east",
"South", "South by west", "South-southwest", "Southwest by south",
"Southeast", "Southeast by south","South-southeast", "South by east",
"Southwest", "Southwest by west", "West-southwest", "West by south",
"South", "South by west", "South-southwest", "Southwest by south",
"West", "West by north", "West-northwest", "Northwest by west",
"Southwest", "Southwest by west", "West-southwest", "West by south",
"Northwest", "Northwest by north", "North-northwest", "North by west", "North"]
"West", "West by north", "West-northwest", "Northwest by west",
"Northwest", "Northwest by north", "North-northwest", "North by west"]
maxNameLength = headingNames.mapIt(it.len).max
degreesPerHeading = 360 / 32

func toCompassIndex(degree: float): 1..32 =
var degree = (degree + degreesPerHeading / 2).floorMod 360
int(degree / degreesPerHeading) + 1

func toCompassHeading(degree: float): string = headingNames[degree.toCompassIndex]


for i in 0..32:
for i in 0..32:
let j = i mod 32
let
var d = float(i) * 11.25
heading = float(i) * 11.25 + (case i mod 3
if i mod 3 == 1: d += 5.62
of 1: 5.62
if i mod 3 == 2: d -= 5.62
of 2: -5.62
else: 0)
printlnfmt "{:2} {:18} {:>6.2f}", j + 1, names[j], d</lang>
index = heading.toCompassIndex
compassHeading = heading.toCompassHeading.alignLeft(maxNameLength)
echo fmt"{index:>2} {compassHeading} {heading:6.2f}"</lang>
Output:
Output:
<pre> 1 North 0
<pre> 1 North 0.00
2 North by east 16.87
2 North by east 16.87
3 North-northeast 16.88
3 North-northeast 16.88