How to resolve the algorithm Angles (geometric), normalization and conversion step by step in the J programming language

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Angles (geometric), normalization and conversion step by step in the J programming language

Table of Contents

Problem Statement

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

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

The angular scales used or referenced here:

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

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:

Degrees   are sometimes known or shown as:

Gradians   are sometimes known or shown as:

Mils   are sometimes known or shown as:

Radians   are sometimes known or shown as:

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 of the gun barrel for ranging).

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   (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º,   0.000000,   or   -0º   should be shown as   0º.

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

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Angles (geometric), normalization and conversion step by step in the J programming language

Source code in the j programming language

   TAU =: 2p1 NB. tauday.com

   normalize =: * * 1 | | NB. signum times the fractional part of absolute value

   TurnTo=: &*

   as_turn    =:    1 TurnTo
   as_degree  =:  360 TurnTo
   as_gradian =:  400 TurnTo
   as_mil     =: 6400 TurnTo
   as_radian  =:  TAU TurnTo

   Turn    =: adverb def 'normalize as_turn inv m'
   Degree  =: adverb def 'normalize as_degree inv m'
   Gradian =: adverb def 'normalize as_gradian inv m'
   Mil     =: adverb def 'normalize as_mil inv m'
   Radian  =: adverb def 'normalize as_radian inv m'


   as_degree   100 Gradian
90


   NAMES =: > turn`degree`gradian`mil`radian
   ALL =: as_turn`as_degree`as_gradian`as_mil`as_radian
   to_all=: NAMES ; ALL`:0

   VALUES =: _&".'-2   -1   0   1   2   6.2831853   16   57.2957795   359   399   6399   1000000'


   to_all   VALUES Turn
+-------+------------------------------------+
|turn   |0 0 0 0 0 0.283185 0 0.29578 0 0 0 0|
|degree |0 0 0 0 0  101.947 0 106.481 0 0 0 0|
|gradian|0 0 0 0 0  113.274 0 118.312 0 0 0 0|
|mil    |0 0 0 0 0  1812.39 0 1892.99 0 0 0 0|
|radian |0 0 0 0 0  1.77931 0 1.85844 0 0 0 0|
+-------+------------------------------------+

   to_all   VALUES Degree
+-------+---------------------------------------------------------------------------------------------------------------+
|turn   |_0.00555556 _0.00277778 0 0.00277778 0.00555556 0.0174533 0.0444444 0.159155 0.997222 0.108333   0.775 0.777778|
|degree |         _2          _1 0          1          2   6.28319        16  57.2958      359       39     279      280|
|gradian|   _2.22222    _1.11111 0    1.11111    2.22222   6.98132   17.7778   63.662  398.889  43.3333     310  311.111|
|mil    |   _35.5556    _17.7778 0    17.7778    35.5556   111.701   284.444  1018.59  6382.22  693.333    4960  4977.78|
|radian | _0.0349066  _0.0174533 0  0.0174533  0.0349066  0.109662  0.279253        1  6.26573 0.680678 4.86947  4.88692|
+-------+---------------------------------------------------------------------------------------------------------------+

   to_all   VALUES Gradian
+-------+----------------------------------------------------------------------------------------------+
|turn   |    _0.005   _0.0025 0   0.0025     0.005 0.015708     0.04 0.143239  0.8975  0.9975  0.9975 0|
|degree |      _1.8      _0.9 0      0.9       1.8  5.65487     14.4  51.5662   323.1   359.1   359.1 0|
|gradian|        _2        _1 0        1         2  6.28319       16  57.2958     359     399     399 0|
|mil    |       _32       _16 0       16        32  100.531      256  916.732    5744    6384    6384 0|
|radian |_0.0314159 _0.015708 0 0.015708 0.0314159 0.098696 0.251327      0.9 5.63916 6.26748 6.26748 0|
+-------+----------------------------------------------------------------------------------------------+

   to_all   VALUES Mil
+-------+-------------------------------------------------------------------------------------------------------------------+
|turn   |_0.0003125  _0.00015625 0  0.00015625 0.0003125 0.000981748   0.0025 0.00895247 0.0560937 0.0623438 0.999844   0.25|
|degree |   _0.1125     _0.05625 0     0.05625    0.1125    0.353429      0.9    3.22289   20.1937   22.4438  359.944     90|
|gradian|    _0.125      _0.0625 0      0.0625     0.125    0.392699        1    3.58099   22.4375   24.9375  399.938    100|
|mil    |        _2           _1 0           1         2     6.28319       16    57.2958       359       399     6399   1600|
|radian |_0.0019635 _0.000981748 0 0.000981748 0.0019635   0.0061685 0.015708    0.05625  0.352447  0.391717   6.2822 1.5708|
+-------+-------------------------------------------------------------------------------------------------------------------+

   to_all   VALUES Radian
+-------+---------------------------------------------------------------------------------------------------+
|turn   |_0.31831 _0.159155 0 0.159155 0.31831       1 0.546479 0.118907 0.136625 0.502822 0.432481 0.943092|
|degree |_114.592  _57.2958 0  57.2958 114.592     360  196.732  42.8063  49.1848  181.016  155.693  339.513|
|gradian|_127.324   _63.662 0   63.662 127.324     400  218.592  47.5626  54.6498  201.129  172.992  377.237|
|mil    |_2037.18  _1018.59 0  1018.59 2037.18    6400  3497.47  761.002  874.397  3218.06  2767.88  6035.79|
|radian |      _2        _1 0        1       2 6.28319  3.43363 0.747112 0.858437  3.15933  2.71736  5.92562|
+-------+---------------------------------------------------------------------------------------------------+


  

You may also check:How to resolve the algorithm Imaginary base numbers step by step in the D programming language
You may also check:How to resolve the algorithm First-class functions step by step in the Clojure programming language
You may also check:How to resolve the algorithm Write entire file step by step in the K programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the EGL programming language
You may also check:How to resolve the algorithm McNuggets problem step by step in the FOCAL programming language