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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Angles (geometric), normalization and conversion step by step in the Ruby 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 Ruby programming language

The provided Ruby code defines a module called Angles that provides conversion between different angle units. Here's a breakdown of the code:

1. Angle Base Constants:

BASES = {"d" => 360, "g" => 400, "m" => 6400, "r" => Math::PI*2 ,"h" => 24 }

This defines a hash named BASES that maps angle unit codes ("d", "g", "m", "r", "h") to their corresponding base values.

2. Dynamic Method Creation:

def self.method_missing(meth, angle)
 ...
end

This is a method defined in the Angles module that allows dynamic method creation. It handles methods in the format of "unitA2unitB", where unitA and unitB are angle unit codes ("d", "g", "m", "r", "h").

3. Angle Conversion: Inside the method_missing method:

  • It extracts the from and to unit bases from the method name (e.g., d2g would extract "d" as from and "g" as to).
  • It calculates the angle conversion using mod = (angle.to_f * to / from) % to. This converts the angle from the from unit to the to unit.
  • If the angle is negative, it subtracts to to account for negative angles.

4. Demo: The demo at the end of the code uses the dynamic method creation to convert an array of angles (test) from one unit to another. It prints the results in a table format, showing conversions between all the supported units.

Example Usage: Let's say we want to convert 180 degrees to radians. We can do this by calling the dynamically created method d2r on the Angles module:

angle = 180
result = Angles.d2r(angle)
puts result  # Prints: 3.141592653589793

In summary, the Angles module provides a dynamic and convenient way to convert angles between different units, making it easy to work with different angle representations in Ruby code.

Source code in the ruby programming language

module Angles
  BASES = {"d" => 360, "g" => 400, "m" => 6400, "r" => Math::PI*2 ,"h" => 24 }
 
  def self.method_missing(meth, angle)
    from, to = BASES.values_at(*meth.to_s.split("2"))
    raise NoMethodError, meth if (from.nil? or to.nil?)
    mod = (angle.to_f * to / from) % to
    angle < 0 ? mod - to : mod
  end

end

#Demo
names = Angles::BASES.keys
puts " " + "%12s "*names.size % names
test = [-2, -1, 0, 1, 2*Math::PI, 16, 360/(2*Math::PI), 360-1, 400-1, 6400-1, 1_000_000]

test.each do |n|
  names.each do |first|
    res = names.map{|last| Angles.send((first + "2" + last).to_sym, n)}
    puts first + "%12g "*names.size % res
  end
  puts
end


  

You may also check:How to resolve the algorithm Lucky and even lucky numbers step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Cholesky decomposition step by step in the Wren programming language
You may also check:How to resolve the algorithm Elementary cellular automaton step by step in the Haskell programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Sather programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the Scheme programming language