There be many a land lubber that knows naught of the pirate ways and gives direction by degree!
They know not how to box the compass!



Task description

 Create a function that takes a heading in degrees and returns the correct 32-point compass heading.
 Use the function to print and display a table of Index, Compass point, and Degree; rather like the corresponding columns from, the first table of the wikipedia article, but use only the following 33 headings as input:
[0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5, 84.37, 84.38, 101.25, 118.12, 118.13, 135.0, 151.87, 151.88, 168.75, 185.62, 185.63, 202.5, 219.37, 219.38, 236.25, 253.12, 253.13, 270.0, 286.87, 286.88, 303.75, 320.62, 320.63, 337.5, 354.37, 354.38]. (They should give the same order of points but are spread throughout the ranges of acceptance).


Notes;
 The headings and indices can be calculated from this pseudocode:
for i in 0..32 inclusive:
    heading = i * 11.25
    case i %3:
      if 1: heading += 5.62; break
      if 2: heading -= 5.62; break
    end
    index = ( i mod 32) + 1
 The column of indices can be thought of as an enumeration of the thirty two cardinal points (see talk page)..


/*REXX program  "boxes the compass"   [from degree (º) headings  ───►  a 32 point set]. */
parse arg $                                      /*allow   º   headings to be specified.*/
if $=''  then $= 0 16.87 16.88 33.75 50.62 50.63 67.5 84.37 84.38 101.25 118.12 118.13   ,
                 135 151.87 151.88 168.75 185.62 185.63 202.5 219.37 219.38 236.25       ,
                 253.12 253.13 270 286.87 286.88 303.75 320.62 320.63 337.5 354.37 354.38
                                                 /* [↑]  use default, they're in degrees*/
@pts= 'n nbe n-ne nebn ne nebe e-ne ebn e ebs e-se sebe se sebs s-se sbe',
      "s sbw s-sw swbs sw swbw w-sw wbs w wbn w-nw nwbw nw nwbn n-nw nbw"
 
#= words(@pts) + 1                               /*#:  used for integer ÷ remainder (//)*/
dirs= 'north south east west'                    /*define cardinal compass directions.  */
                                                 /* [↓]  choose a glyph for degree  (°).*/
if 4=='f4'x  then degSym= "a1"x                  /*is this system an  EBCDIC  system?   */
             else degSym= "a7"x                  /*'f8'x  is the degree symbol: °  vs º */
                                                 /*──────────────────────────── f8 vs a7*/
say right(degSym'heading', 30)     center("compass heading", 20)
say right(     '════════', 30)     copies(              "═", 20)
pad= ' '                                         /*used to interject a blank for output.*/
         do j=1  for words($);   x= word($, j)   /*obtain one of the degree headings.   */
         say right( format(x, , 2)degSym, 30-1)          pad         boxHeading(x)
         end   /*j*/
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
boxHeading: y= arg(1)//360; if y<0  then y=360-y /*normalize heading within unit circle.*/
            z= word(@pts, trunc(max(1, (y/11.25 +1.5) // #))) /*convert degrees─►heading*/
                                               do k=1  for words(dirs);   d= word(dirs, k)
                                               z= changestr( left(d, 1),  z,  d)
                                               end   /*k*/    /* [↑]  old, haystack, new*/
            return changestr('b',  z,  " by ")                /*expand  "b" ───► " by ".*/