How to resolve the algorithm Box the compass step by step in the Red programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Box the compass step by step in the Red programming language

Table of Contents

Problem Statement

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!

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Box the compass step by step in the Red programming language

Source code in the red programming language

Red []

d:  charset [#"N"  #"E" #"S" #"W"]                                                ;; main directions
hm: #()                                                                           ;; hm = hashmap - key= heading, value = [box , compass point]

compass-points: [N NbE NNE NEbN NE NEbE ENE EbN E EbS ESE SEbE SE SEbS SSE
 SbE S SbW SSW SWbS SW SWbW WSW WbS W WbN WNW NWbW NW NWbN NNW NbW N ]

expand: func [cp repl][                                                         ;; expand compass point to words
  parse cp [ copy a thru d ahead 2 d insert "-" ]                               ;; insert "-" after first direction, if followed by 2 more
  foreach [src dst ] repl  [ replace/all cp to-string src to-string dst ]       ;; N -> north ...
  uppercase/part cp 1                                                           ;; convert first letter to uppercase
]

print-line: does [ print [pad/left hm/:heading/1 3   pad  hm/:heading/2 20  heading ] ]

forall compass-points [
 i: (index? compass-points) - 1                                                       ;; so i = 0..33
 heading: i * 11.25 + either 1 = rem: i % 3  [ 5.62]                                  ;; rem = remainder
                                             [ either  rem = 2 [-5.62] [0.0] ]
  hm/:heading:  reduce [ (i % 32 + 1 ) expand to-string compass-points/1 [N north b " by " S south E east W west] ]  
  print-line heading
]


  

You may also check:How to resolve the algorithm Break OO privacy step by step in the F# programming language
You may also check:How to resolve the algorithm Price fraction step by step in the Factor programming language
You may also check:How to resolve the algorithm Read a file character by character/UTF8 step by step in the C++ programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Arturo programming language
You may also check:How to resolve the algorithm Comments step by step in the PowerShell programming language