Angles (geometric), normalization and conversion

Revision as of 10:47, 13 July 2019 by rosettacode>Gerard Schildberger (added a (new) draft task, also added the REXX computer programming language entry.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This task is about the normalization and/or conversion of (geometric) angles using some common scales.

Angles (geometric), normalization and conversion is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.


The angular scales that will be used in this task are:

  •   degree
  •   gradian
  •   mil
  •   radian


Definitions

The angular scales used or referenced here:

  •   turn   is a full turn or 360 degrees, also shown as 360º
  •   degree   is   1/360   of a turn
  •   gradian   is   1/400   of a turn
  •   mil   is   1/6400   of a turn
  •   radian   is   1/2   of a turn   (or   0.5/   of a turn)


Or, to put it another way,   for a full circle:

  •   there are   360   degrees
  •   there are   400   gradians
  •   there are   6,400   mils
  •   there are   2   radians   (roughly equal to 6.283+)


A   mil   is approximately equal to a   milliradian   (which is   1/1000   of a radian).

There is another definition of a   mil   which is   1/1000   of a radian   ─── this definition won't be used in this Rosetta Code task.


Turns   are sometimes known or shown as:

  •   turn(s)
  •   360 degrees
  •   unit circle
  •   a (full) circle


Degrees   are sometimes known or shown as:

  •   degree(s)
  •   deg
  •   º       (a symbol)
  •   °       (a symbol)


Gradians   are sometimes known or shown as:

  •   gradian(s)
  •   grad(s)
  •   grade(s)
  •   gon(s)
  •   metric degree(s)


Mils   are sometimes known or shown as:

  •   mil(s)
  •   NATO mil(s)


Radians   are sometimes known or shown as:

  •   radian(s)
  •   rad(s)


Notes

In continental Europe, the French term   centigrade   was used for   1/100   of a grad (grade);   this was one reason for the adoption of the term   Celsius   to replace   centigrade   as the name of a temperature scale.

Gradians were commonly used in civil engineering.

Mils were normally used for artillery   (elevations for ranging).


Positive and negative angles

Although the definition of the measurement of an angle doesn't support the concept of a negative angle,   it's frequently useful to impose a convention that allows positive and negative angular values to represent orientations and/or rotations in opposite directions relative to some reference.   It is this reason that negative angles will keep their sign and not be normalized to positive angles.


Normalization

Normalization   (for this Rosetta Code task)   will keep the same sign,   but it will reduce the magnitude to less than a full circle;   in other words, less than 360º.

Normalization   shouldn't   change   -45º   to   315º,

An angle of   ,   +0º,   0.000000,   or   -0º   should be shown as   .


Task
  •   write a function (or equivalent) to do the normalization for each scale
  • Suggested names:
  • d2d,   g2g,   m2m,   and  r2r
  •   write a function (or equivalent) to convert one scale to another
  • Suggested names for comparison of different computer language function names:
  • d2g,   d2m,   and   d2r   for degrees
  • g2d,   g2m,   and   g2r   for gradians
  • m2d,   m2g,   and   m2r   for mils
  • r2d,   r2g,   and   r2m   for radians
  •   normalize all angles used   (except for the "original" or "base" angle)
  •   show the angles in every scale and convert them to all other scales
  •   show all output here on this page


For the (above) conversions,   use these dozen numbers   (in the order shown):

  •   -2   -1   0   1   2   6.2831853   16   57.2957795   359   399   6399   1000000




REXX

<lang rexx>/*REXX pgm normalizes an angle (in a scale), or converts angles from a scale to another.*/ numeric digits length( pi() ) - length(.) /*use the "length" of pi for precision.*/ parse arg x /*obtain optional arguments from the CL*/ if x= | x="," then x= '-2 -1 0 1 2 6.2831853 16 57.2957795 359 399 6399 1000000' w= 20; w7= w+7 /*W: # dec digits past the dec. point.*/ @deg = 'degrees'; @grd= "gradians"; @mil = 'mils'; @rad = "radians"

  1. = words(x)

call hdr @deg @grd @mil @rad

     do j=1  for #;            y= word(x,j)
     say shw(y)        fmt(d2d(y))              fmt(d2g(y))    fmt(d2m(y))    fmt(d2r(y))
     end   /*j*/

call hdr @grd @deg @mil @rad

     do j=1  for #;            y= word(x,j)
     say shw(y)        fmt(g2g(y))              fmt(g2d(y))    fmt(g2m(y))    fmt(g2r(y))
     end   /*j*/

call hdr @mil @deg @grd @rad

     do j=1  for #;            y= word(x,j)
     say shw(y)        fmt(m2m(y))              fmt(m2d(y))    fmt(m2g(y))    fmt(m2r(y))
     end   /*j*/

call hdr @rad @deg @grd @mil

     do j=1  for #;            y= word(x,j)
     say shw(y)        fmt(r2r(y))              fmt(r2d(y))    fmt(r2g(y))    fmt(r2m(y))
     end   /*j*/

exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ fmt: _= format(arg(1), 6,w); L= length(_); return left(format(_/1, 6),L) /*align a #*/ shw: _= format(arg(1),12,9); L= length(_); return left(format(_/1,12),L) /* " " "*/ pi: pi= 3.1415926535897932384626433832795028841971693993751058209749445923078; return pi d2g: return d2d(arg(1)) * 10 / 9 /*convert degrees ───► gradians. */ d2m: return d2d(arg(1)) * 160 / 9 /*convert degrees ───► mils. */ d2r: return d2d(arg(1)) * pi() / 180 /*convert degrees ───► radians. */ g2d: return g2g(arg(1)) * 0.9 /*convert gradians ───► degrees. */ g2m: return g2g(arg(1)) * 16 /*convert gradians ───► mils. */ g2r: return g2g(arg(1)) * pi() * 0.005 /*convert gradians ───► radians. */ m2d: return m2m(arg(1)) * 9 * 0.00625 /*convert mils ───► degrees. */ m2g: return m2m(arg(1)) / 16 /*convert mils ───► gradians. */ m2r: return m2m(arg(1)) * pi() / 3200 /*convert mils ───► radians. */ r2d: return r2r(arg(1)) * 180 / pi() /*convert radians ───► degrees. */ r2g: return r2r(arg(1)) * 200 / pi() /*convert radians ───► gradians. */ r2m: return r2r(arg(1)) * 3200 / pi() /*convert radians ───► mils. */ d2d: return arg(1) // 360 /*normalize degrees ───► a unit circle.*/ g2g: return arg(1) // 400 /*normalize gradians───► a unit circle.*/ m2m: return arg(1) // 6400 /*normalize mils ───► a unit circle.*/ r2r: return arg(1) // (pi() * 2) /*normalize radians ───► a unit circle.*/ /*──────────────────────────────────────────────────────────────────────────────────────*/ hdr: parse arg #o #a #b #c .; _= '═'; say /* [↓] the header line*/

                               @n  = 'normalized'
    say center(#o,23  )  center(@n #o,w7) center(#a,w7  ) center(#b,w7  ) center(#c,w7  )
    say center(,23,_)  center(,w7, _) center(,w7,_) center(,w7,_) center(,w7,_)
    return                                                      /* '↑'  seperator line.*/</lang>
output   when using the default input:
        degrees             normalized degrees               gradians                      mils                       radians
═══════════════════════ ═══════════════════════════ ═══════════════════════════ ═══════════════════════════ ═══════════════════════════
          -2               -2                          -2.22222222222222222222    -35.55555555555555555556     -0.03490658503988659154
          -1               -1                          -1.11111111111111111111    -17.77777777777777777778     -0.01745329251994329577
           0                0                           0                           0                           0
           1                1                           1.11111111111111111111     17.77777777777777777778      0.01745329251994329577
           2                2                           2.22222222222222222222     35.55555555555555555556      0.03490658503988659154
           6.2831853        6.2831853                   6.981317                  111.701072                    0.10966227099790767281
          16               16                          17.77777777777777777778    284.44444444444444444444      0.27925268031909273231
          57.2957795       57.2957795                  63.66197722222222222222   1018.59163555555555555556      0.9999999997716704269
         359              359                         398.88888888888888888889   6382.22222222222222222222      6.26573201465964318116
         399               39                          43.33333333333333333333    693.33333333333333333333      0.680678408277788535
        6399              279                         310                        4960                           4.86946861306417951962
     1000000              280                         311.11111111111111111111   4977.77777777777777777778      4.88692190558412281539

       gradians             normalized gradians               degrees                      mils                       radians
═══════════════════════ ═══════════════════════════ ═══════════════════════════ ═══════════════════════════ ═══════════════════════════
          -2               -2                          -1.8                       -32                          -0.03141592653589793238
          -1               -1                          -0.9                       -16                          -0.01570796326794896619
           0                0                           0                           0                           0
           1                1                           0.9                        16                           0.01570796326794896619
           2                2                           1.8                        32                           0.03141592653589793238
           6.2831853        6.2831853                   5.65486677                100.5309648                   0.09869604389811690553
          16               16                          14.4                       256                           0.25132741228718345908
          57.2957795       57.2957795                  51.56620155                916.732472                    0.89999999979450338421
         359              359                         323.1                      5744                           5.63915881319367886304
         399              399                         359.1                      6384                           6.26747734391163751073
        6399              399                         359.1                      6384                           6.26747734391163751073
     1000000                0                           0                           0                           0

         mils                 normalized mils                 degrees                    gradians                     radians
═══════════════════════ ═══════════════════════════ ═══════════════════════════ ═══════════════════════════ ═══════════════════════════
          -2               -2                          -0.1125                     -0.125                      -0.00196349540849362077
          -1               -1                          -0.05625                    -0.0625                     -0.00098174770424681039
           0                0                           0                           0                           0
           1                1                           0.05625                     0.0625                      0.00098174770424681039
           2                2                           0.1125                      0.125                       0.00196349540849362077
           6.2831853        6.2831853                   0.353429173125              0.39269908125               0.0061685027436323066
          16               16                           0.9                         1                           0.01570796326794896619
          57.2957795       57.2957795                   3.222887596875              3.58098621875               0.05624999998715646151
         359              359                          20.19375                    22.4375                      0.35244742582460492894
         399              399                          22.44375                    24.9375                      0.39171733399447734442
        6399             6399                         359.94375                   399.9375                      6.28220355947533966654
     1000000             1600                          90                         100                           1.57079632679489661923

        radians             normalized radians                degrees                    gradians                      mils
═══════════════════════ ═══════════════════════════ ═══════════════════════════ ═══════════════════════════ ═══════════════════════════
          -2               -2                        -114.5915590261646417536    -127.32395447351626861511  -2037.18327157626029784171
          -1               -1                         -57.2957795130823208768     -63.66197723675813430755  -1018.59163578813014892086
           0                0                           0                           0                           0
           1                1                          57.2957795130823208768      63.66197723675813430755   1018.59163578813014892086
           2                2                         114.5915590261646417536     127.32395447351626861511   2037.18327157626029784171
           6.2831853        6.2831853                 359.99999958863999622298    399.99999954293332913665   6399.99999268693326618633
          16                3.43362938564082704615    196.73247220931713402877    218.59163578813014892086   3497.4661726100823827337
          57.2957795        0.74711173538372170767     42.80634926218202230527     47.56261029131335811697    761.00176466101372987153
         359                0.85843749076357081526     49.18484519655319477054     54.64982799617021641171    874.39724793872346258733
         399                3.15932564768605195371    181.01602571984602984246    201.12891746649558871385   3218.06267946392941942158
        6399                2.71735729118096649006    155.69310421377129063139    172.99233801530143403488   2767.87740824482294455809
     1000000                5.92562114009385143291    339.51308232087679815481    377.23675813430755350535   6035.78813014892085608558