How to resolve the algorithm Angles (geometric), normalization and conversion step by step in the C programming language
How to resolve the algorithm Angles (geometric), normalization and conversion step by step in the C 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 C programming language
The provided C code defines a set of functions to normalize and convert angles between different units of measurement: degrees, grads, mils, and radians.
-
Normalization Functions:
normalize2deg(double a)
: Normalizes an anglea
to the range [0, 360) degrees.normalize2grad(double a)
: Normalizes an anglea
to the range [0, 400) grads.normalize2mil(double a)
: Normalizes an anglea
to the range [0, 6400) mils.normalize2rad(double a)
: Normalizes an anglea
to the range [0, 2π) radians.
-
Conversion Functions:
-
Degree Conversions:
deg2grad(double a)
: Converts an anglea
from degrees to grads.deg2mil(double a)
: Converts an anglea
from degrees to mils.deg2rad(double a)
: Converts an anglea
from degrees to radians.
-
Grad Conversions:
grad2deg(double a)
: Converts an anglea
from grads to degrees.grad2mil(double a)
: Converts an anglea
from grads to mils.grad2rad(double a)
: Converts an anglea
from grads to radians.
-
Mil Conversions:
mil2deg(double a)
: Converts an anglea
from mils to degrees.mil2grad(double a)
: Converts an anglea
from mils to grads.mil2rad(double a)
: Converts an anglea
from mils to radians.
-
Radian Conversions:
rad2deg(double a)
: Converts an anglea
from radians to degrees.rad2grad(double a)
: Converts an anglea
from radians to grads.rad2mil(double a)
: Converts an anglea
from radians to mils.
-
The code also defines constants for π and 2π.
Example Usage:
- To convert 60 degrees to radians:
double rad = deg2rad(60);
- To normalize an angle of -170 degrees to the range [0, 360):
double deg = normalize2deg(-170);
This code is a set of functions that convert angles from one unit of measurement to another. The functions are:
normalize2deg
: normalizes an angle to the range [0, 360) degrees.normalize2grad
: normalizes an angle to the range [0, 400) grads.normalize2mil
: normalizes an angle to the range [0, 6400) mils.normalize2rad
: normalizes an angle to the range [0, 2π) radians.deg2grad
: converts an angle from degrees to grads.deg2mil
: converts an angle from degrees to mils.deg2rad
: converts an angle from degrees to radians.grad2deg
: converts an angle from grads to degrees.grad2mil
: converts an angle from grads to mils.grad2rad
: converts an angle from grads to radians.mil2deg
: converts an angle from mils to degrees.mil2grad
: converts an angle from mils to grads.mil2rad
: converts an angle from mils to radians.rad2deg
: converts an angle from radians to degrees.rad2grad
: converts an angle from radians to grads.rad2mil
: converts an angle from radians to mils.
The code defines the constants PI
and TWO_PI
to represent the values of π and 2π, respectively.
These constants are used in the normalize2rad()
and rad2*()
functions.
The normalize2*()
functions work by adding or subtracting the appropriate amount to the input angle until it is within the desired range.
For example, the normalize2deg()
function adds 360 degrees to the input angle if it is negative, and subtracts 360 degrees from the input angle if it is greater than or equal to 360 degrees.
The conversion functions work by multiplying the input angle by the appropriate conversion factor.
For example, the deg2rad()
function multiplies the input angle by PI / 180
to convert it from degrees to radians.
Source code in the c programming language
#define PI 3.141592653589793
#define TWO_PI 6.283185307179586
double normalize2deg(double a) {
while (a < 0) a += 360;
while (a >= 360) a -= 360;
return a;
}
double normalize2grad(double a) {
while (a < 0) a += 400;
while (a >= 400) a -= 400;
return a;
}
double normalize2mil(double a) {
while (a < 0) a += 6400;
while (a >= 6400) a -= 6400;
return a;
}
double normalize2rad(double a) {
while (a < 0) a += TWO_PI;
while (a >= TWO_PI) a -= TWO_PI;
return a;
}
double deg2grad(double a) {return a * 10 / 9;}
double deg2mil(double a) {return a * 160 / 9;}
double deg2rad(double a) {return a * PI / 180;}
double grad2deg(double a) {return a * 9 / 10;}
double grad2mil(double a) {return a * 16;}
double grad2rad(double a) {return a * PI / 200;}
double mil2deg(double a) {return a * 9 / 160;}
double mil2grad(double a) {return a / 16;}
double mil2rad(double a) {return a * PI / 3200;}
double rad2deg(double a) {return a * 180 / PI;}
double rad2grad(double a) {return a * 200 / PI;}
double rad2mil(double a) {return a * 3200 / PI;}
You may also check:How to resolve the algorithm Pick random element step by step in the GAP programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Middle-square method step by step in the Haskell programming language
You may also check:How to resolve the algorithm Dijkstra's algorithm step by step in the Wren programming language
You may also check:How to resolve the algorithm Seven-sided dice from five-sided dice step by step in the VBScript programming language
You may also check:How to resolve the algorithm Vigenère cipher step by step in the ALGOL 68 programming language