Box the compass: Difference between revisions

→‎{{header|Nim}}: Solution that matches the task
(Add Factor)
(→‎{{header|Nim}}: Solution that matches the task)
Line 4,316:
 
=={{header|Nim}}==
<lang nim>import strfmtmath, sequtils, strformat, strutils
 
const names = [
headingNames: array[1..32, string] = [
"North", "North by east", "North-northeast", "Northeast by north",
"NortheastNorth", "NortheastNorth by east", "EastNorth-northeast", "EastNortheast by north",
"EastNortheast", "EastNortheast by southeast", "East-southeastnortheast", "SoutheastEast by eastnorth",
"SoutheastEast", "SoutheastEast by south", "SouthEast-southeast", "SouthSoutheast by east",
"SouthSoutheast", "SouthSoutheast by westsouth", "South-southwestsoutheast", "SouthwestSouth by southeast",
"SouthwestSouth", "SouthwestSouth by west", "WestSouth-southwest", "WestSouthwest by south",
"WestSouthwest", "WestSouthwest by northwest", "West-northwestsouthwest", "NorthwestWest by westsouth",
"NorthwestWest", "NorthwestWest by north", "NorthWest-northwest", "NorthNorthwest by west", "North"]
"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:
let j = i mod 32
var d heading = float(i) * 11.25 + (case i mod 3
if i mod 3 ==of 1: d += 5.62
if i mod 3 ==of 2: d -= 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:
<pre> 1 North 0.00
2 North by east 16.87
3 North-northeast 16.88