Box the compass: Difference between revisions

Content added Content deleted
(→‎{{header|Nim}}: Solution that matches the task)
(→‎{{header|UNIX Shell}}: sed-based capitalize() doesn't work on BSD/OS X, which also doesn't have bash 4 and its ${param^}.)
Line 6,542: Line 6,542:
{{trans|Logo}}
{{trans|Logo}}


Requires the standard POSIX bc(1) and sed(1) commands to function.
Requires the standard POSIX bc(1) command to perform floating-point arithmetic.


<lang sh># List of abbreviated compass point labels
<lang sh># List of abbreviated compass point labels
Line 6,557: Line 6,557:
320.62 320.63 337.50 354.37 354.38 )
320.62 320.63 337.50 354.37 354.38 )


capitalize() {

printf '%s%s\n' "$(tr a-z A-Z <<<"${1:0:1}")" "${1:1}"
# capitalize a string
function capitalize {
echo "$1" | sed 's/^./\U&/'
}
}


# convert compass point abbreviation to full text of label
# convert compass point abbreviation to full text of label
function expand_point {
function expand_point {
local label="$1"
local label=$1
set -- N north E east S south W west b " by "
set -- N north E east S south W west b " by "
while (( $# )); do
while (( $# )); do
label="${label//$1/$2}"
label=${label//$1/$2}
shift 2
shift 2
done
done
Line 6,581: Line 6,579:
# convert a compass angle from degrees into a box index (1..32)
# convert a compass angle from degrees into a box index (1..32)
function compass_point {
function compass_point {
# use bc or dc depending on what's on the system
#amod $(dc <<<"$1 5.625 + 11.25 / 1 + p") 32
#amod $(dc <<<"$1 5.625 + 11.25 / 1 + p") 32
amod $(bc <<<"($1 + 5.625) / 11.25 + 1") 32
amod $(bc <<<"($1 + 5.625) / 11.25 + 1") 32