scifir-units 2.0.0
scifir-units is a library of units of measurement, angles, coordinates, fields, and related data, all lightweight, that help in the development of scientific software and scientific machines
Loading...
Searching...
No Matches
angle.hpp
Go to the documentation of this file.
1#ifndef SCIFIR_UNITS_MECA_NUMBER_ANGLE_HPP_INCLUDED
2#define SCIFIR_UNITS_MECA_NUMBER_ANGLE_HPP_INCLUDED
3
4#include "../units/constants.hpp"
5#include "../util/is_number.hpp"
6#include "../units/scalar_unit.hpp"
7
8#include <cmath>
9#include <numbers>
10#include <string>
11
12using namespace std;
13
14namespace scifir
15{
16 inline float radian_to_degree(float x)
17 {
18 return x * 180.0f / std::numbers::pi_v<float>;
19 }
20
21 inline float gradian_to_degree(float x)
22 {
23 return x * 0.9f;
24 }
25
26 inline float turn_to_degree(float x)
27 {
28 return x * 360.0f;
29 }
30
31 inline float degree_to_radian(float x)
32 {
33 return x * std::numbers::pi_v<float> / 180.0f;
34 }
35
36 inline float gradian_to_radian(float x)
37 {
38 return x * 0.015708f;
39 }
40
41 inline float turn_to_radian(float x)
42 {
43 return x * 2 * std::numbers::pi_v<float>;
44 }
45
46 inline float degree_to_gradian(float x)
47 {
48 return x / 0.9f;
49 }
50
51 inline float radian_to_gradian(float x)
52 {
53 return x * 200.0f / std::numbers::pi_v<float>;
54 }
55
56 inline float turn_to_gradian(float x)
57 {
58 return x * 400.f;
59 }
60
61 inline float degree_to_turn(float x)
62 {
63 return x / 360.0f;
64 }
65
66 inline float radian_to_turn(float x)
67 {
68 return x / (2.0f * std::numbers::pi_v<float>);
69 }
70
71 inline float gradian_to_turn(float x)
72 {
73 return x / 400.0f;
74 }
75
76 class angle
77 {
78 public:
80
81 angle();
82 angle(const angle& x);
83 angle(angle&& x);
86 explicit angle(long double new_value,angle::type init_type = angle::DEGREE);
88 explicit angle(const string& init_angle);
89 explicit angle(const scalar_unit& x);
90
91 angle& operator =(const angle& x);
94 angle& operator =(const string& init_angle);
95 angle& operator =(const scalar_unit& x);
96
97 explicit operator float() const
98 {
99 return float(value);
100 }
101
102 inline const float& get_value() const
103 {
104 return value;
105 }
106
107 angle operator +(const angle& x) const;
108 angle operator -(const angle& x) const;
109 angle operator *(const angle& x) const;
110 angle operator /(const angle& x) const;
111 angle operator ^(const angle& x) const;
112 void operator +=(const angle& x);
113 void operator -=(const angle& x);
114 void operator *=(const angle& x);
115 void operator /=(const angle& x);
116 void operator ^=(const angle& x);
117
120 {
123 }
124
127 {
128 return angle(value + x);
129 }
130
133 {
134 return angle(value - x);
135 }
136
139 {
140 return angle(value * x);
141 }
142
145 {
146 return angle(value / x);
147 }
148
151 {
152 return angle(std::pow(value, x));
153 }
154
157 {
158 value += x;
160 }
161
164 {
165 value -= x;
167 }
168
171 {
172 value *= x;
174 }
175
178 {
179 value /= x;
181 }
182
185 {
186 value = std::pow(value, x);
188 }
189
191 angle operator ++(int);
193 angle operator --(int);
194
195 void invert();
196
198
199 string display(int number_of_decimals = 2) const;
200
201 inline float get_degree() const
202 {
203 return value;
204 }
205
206 inline float get_radian() const
207 {
208 return degree_to_radian(value);
209 }
210
211 protected:
212 float value;
213
215
216 private:
217 void normalize_value();
218 };
219
220 string to_string(const angle& x);
221 bool is_angle(const string& init_angle);
222 bool parallel(const angle& x, const angle& y);
223 bool orthogonal(const angle& x, const angle& y);
224
225 angle sqrt(const angle& x);
226 angle sqrt_nth(const angle& x,int index);
227
228 float sin(const angle& x);
229 float cos(const angle& x);
230 float tan(const angle& x);
231 angle asin(float x);
232 angle acos(float x);
233 angle atan(float x);
234 angle atan2(float y,float x);
235 float sinh(const angle& x);
236 float cosh(const angle& x);
237 float tanh(const angle& x);
238 angle asinh(float x);
239 angle acosh(float x);
240 angle atanh(float x);
241
242 inline float asin_degree(float x)
243 {
244 return radian_to_degree(std::asin(x));
245 }
246
247 inline float acos_degree(float x)
248 {
249 return radian_to_degree(std::acos(x));
250 }
251
252 inline float atan_degree(float x)
253 {
254 return radian_to_degree(std::atan(x));
255 }
256
257 inline float asinh_degree(float x)
258 {
259 return radian_to_degree(std::asinh(x));
260 }
261
262 inline float acosh_degree(float x)
263 {
264 return radian_to_degree(std::acosh(x));
265 }
266
267 inline float atanh_degree(float x)
268 {
269 return radian_to_degree(std::atanh(x));
270 }
271}
272
273template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
275{
276 return scifir::angle(x + y.get_value());
277}
278
279template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
281{
282 return scifir::angle(x - y.get_value());
283}
284
285template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
287{
288 return scifir::angle(x * y.get_value());
289}
290
291template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
293{
294 return scifir::angle(x / y.get_value());
295}
296
297template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
299{
300 return scifir::angle(pow(x, y.get_value()));
301}
302
303template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
304bool operator ==(T x, const scifir::angle& y)
305{
306 return (x == y.get_value());
307}
308
309template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
310bool operator !=(T x, const scifir::angle& y)
311{
312 return (x != y.get_value());
313}
314
315template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
316bool operator <(T x, const scifir::angle& y)
317{
318 return (x < y.get_value());
319}
320
321template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
322bool operator >(T x, const scifir::angle& y)
323{
324 return (x > y.get_value());
325}
326
327template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
328bool operator <=(T x, const scifir::angle& y)
329{
330 return (x <= y.get_value());
331}
332
333template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
334bool operator >=(T x, const scifir::angle& y)
335{
336 return (x >= y.get_value());
337}
338
339template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
340bool operator ==(const scifir::angle& y, T x)
341{
342 return (x == y.get_value());
343}
344
345template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
346bool operator !=(const scifir::angle& y, T x)
347{
348 return (x != y.get_value());
349}
350
351template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
352bool operator <(const scifir::angle& y, T x)
353{
354 return (y.get_value() < x);
355}
356
357template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
358bool operator >(const scifir::angle& y, T x)
359{
360 return (y.get_value() > x);
361}
362
363template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
364bool operator <=(const scifir::angle& y, T x)
365{
366 return (y.get_value() <= x);
367}
368
369template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
370bool operator >=(const scifir::angle& y, T x)
371{
372 return (y.get_value() >= x);
373}
374
375bool operator ==(const scifir::angle& x, const scifir::angle& y);
376bool operator !=(const scifir::angle& x, const scifir::angle& y);
377bool operator <(const scifir::angle& x, const scifir::angle& y);
378bool operator >(const scifir::angle& x, const scifir::angle& y);
379bool operator <=(const scifir::angle& x, const scifir::angle& y);
380bool operator >=(const scifir::angle& x, const scifir::angle& y);
381
382bool operator ==(const scifir::angle& x, const string& init_angle);
383bool operator !=(const scifir::angle& x, const string& init_angle);
384
385bool operator ==(const string& init_angle, const scifir::angle& x);
386bool operator !=(const string& init_angle, const scifir::angle& x);
387
388void operator +=(string& x, const scifir::angle& y);
389string operator +(const string& x, const scifir::angle& y);
390string operator +(const scifir::angle& y, const string& x);
391
392ostream& operator <<(ostream& os, const scifir::angle& x);
393istream& operator >>(istream& is, scifir::angle& x);
394
395#endif // SCIFIR_UNITS_MECA_NUMBER_ANGLE_HPP_INCLUDED
istream & operator>>(istream &is, scifir::angle &x)
Initializes an angle with an initialization string obtained from an input stream.
Definition angle.cpp:593
scifir::angle operator-(T x, const scifir::angle &y)
Creates a new angle as the substraction of the numeric type with the value of this angle.
Definition angle.hpp:280
scifir::angle operator^(T x, const scifir::angle &y)
Creates a new angle as the power of a numeric type with the value of this angle.
Definition angle.hpp:298
bool operator>=(T x, const scifir::angle &y)
Checks if the value of angle y is lower or equal than a numeric type x.
Definition angle.hpp:334
scifir::angle operator+(T x, const scifir::angle &y)
Creates a new angle as the sum of a numeric type with the value of this angle.
Definition angle.hpp:274
scifir::angle operator/(T x, const scifir::angle &y)
Creates a new angle as the division of the value of this angle with the numeric type.
Definition angle.hpp:292
ostream & operator<<(ostream &os, const scifir::angle &x)
Sends the string representation of angle x to an output stream.
Definition angle.cpp:588
bool operator<=(T x, const scifir::angle &y)
Checks if the value of angle y is equal or greather than a numeric type x.
Definition angle.hpp:328
bool operator>(T x, const scifir::angle &y)
Checks if the value of angle y is lower to a numeric type x.
Definition angle.hpp:322
scifir::angle operator*(T x, const scifir::angle &y)
Creates a new angle as the multiplication of a numeric type with the value of this angle.
Definition angle.hpp:286
bool operator<(T x, const scifir::angle &y)
Checks if the value of angle y is greather to a numeric type x.
Definition angle.hpp:316
bool operator!=(T x, const scifir::angle &y)
Checks if the value of angle y is not equal to a numeric type x.
Definition angle.hpp:310
bool operator==(T x, const scifir::angle &y)
Checks if the value of angle y is equal to a numeric type x.
Definition angle.hpp:304
void operator+=(string &x, const scifir::angle &y)
Concatenates to string x the string representation of angle y.
Definition angle.cpp:565
Class that allows to work with angles. Each angle sizes 4 bytes. Initialization string example: "20°"...
Definition angle.hpp:77
angle & operator--()
Decrements the value by one.
Definition angle.cpp:237
void initialize_from_string(string init_angle)
Internal function. Sets the value of the angle to a new value using the initialization string of angl...
Definition angle.cpp:297
const float & get_value() const
Gets the value of the angle, in degrees.
Definition angle.hpp:102
void operator^=(const angle &x)
Powers the value of this angle with the value of angle x. The value is normalized after.
Definition angle.cpp:218
scalar_unit to_scalar_unit() const
Creates a scalar_unit with the same value and degree dimensions.
Definition angle.cpp:256
float value
Value of the angle. It is stored in degrees.
Definition angle.hpp:212
type
Represents an type of angle, which can be a degree or a radian. The value of the angle inside the ang...
Definition angle.hpp:79
@ DEGREE
The angle is in DEGREE. A degree is defined as a 1/360 part of a circle. The entire circle correspond...
Definition angle.hpp:79
@ RADIAN
The angle is in RADIAN. A radian is defined as the length of the perimeter of the circle that conform...
Definition angle.hpp:79
angle operator+(const angle &x) const
Creates a new angle as the sum of other two.
Definition angle.cpp:169
float get_degree() const
Gets the value of the angle in degrees.
Definition angle.hpp:201
void operator/=(const angle &x)
Divides the value of this angle with the value of angle x. The value is normalized after.
Definition angle.cpp:212
angle & operator++()
Increments the value by one.
Definition angle.cpp:224
angle operator-(const angle &x) const
Creates a new angle as the substraction of this angle and another angle x.
Definition angle.cpp:174
void operator-=(const angle &x)
Substract the value of the angle x to the value of this angle. The value is normalized after.
Definition angle.cpp:200
angle & operator=(const angle &x)
Copy assignment of angle. The value is copied to the value of angle x.
Definition angle.cpp:130
void operator+=(const angle &x)
Sums the value of the angle x to the value of this angle. The value is normalized after.
Definition angle.cpp:194
float get_radian() const
Gets the value of the angle in radians.
Definition angle.hpp:206
void invert()
Inverts the angle to the opposite direction in a 2D plane, which is to add 180 degrees....
Definition angle.cpp:250
angle()
Default constructor of angle. The value is set to 0.
Definition angle.cpp:18
angle operator/(const angle &x) const
Creates a new angle as the division of this angle and another angle x.
Definition angle.cpp:184
string display(int number_of_decimals=2) const
Creates an string from the angle, with the value and the degrees symbol.
Definition angle.cpp:261
void normalize_value()
Internal function. Normalizes the value, which means to maintain the same angle within 0 and 360....
Definition angle.cpp:276
angle operator*(const angle &x) const
Creates a new angle as the multiplication of other two.
Definition angle.cpp:179
angle operator^(const angle &x) const
Creates a new angle by powering the angle class with the value of another angle x.
Definition angle.cpp:189
void operator*=(const angle &x)
Multiplies the value of this angle with the value of angle x. The value is normalized after.
Definition angle.cpp:206
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions....
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
angle sqrt_nth(const angle &x, int index)
Calculates the nth root of the angle x and returns that new angle.
Definition angle.cpp:421
float atanh_degree(float x)
Calculates the atanh receiving x in degrees. It uses the atanh() function of the standard library of ...
Definition angle.hpp:267
angle asin(float x)
Calculates the asin of some value x and returns the result as angle in degrees.
Definition angle.cpp:441
float cos(const angle &x)
Calculates the cos of angle x. It uses the cos() function of the standard library of C++,...
Definition angle.cpp:431
float degree_to_gradian(float x)
Definition angle.hpp:46
bool orthogonal(const angle &x, const angle &y)
Checks if two angles in a 2D correspond to orthogonal lines (or orthogonal vectors).
Definition angle.cpp:403
float tanh(const angle &x)
Calculates the tanh of angle x. It uses the tanh() function of the standard library of C++,...
Definition angle.cpp:471
float radian_to_degree(float x)
Converts a radian to degree.
Definition angle.hpp:16
float sinh(const angle &x)
Calculates the sinh of angle x. It uses the sinh() function of the standard library of C++,...
Definition angle.cpp:461
float degree_to_turn(float x)
Definition angle.hpp:61
float gradian_to_radian(float x)
Definition angle.hpp:36
float atan_degree(float x)
Calculates the atan receiving x in degrees. It uses the atan() function of the standard library of C+...
Definition angle.hpp:252
float turn_to_radian(float x)
Definition angle.hpp:41
string to_string(const aid &x)
Creates a string representation of aid, it's for aid equivalent to the display() function of aid.
Definition aid.cpp:582
angle atan2(float y, float x)
Definition angle.cpp:456
float degree_to_radian(float x)
Converts a degree to a radian.
Definition angle.hpp:31
angle atan(float x)
Calculates the atan of some value x and returns the result as angle in degrees.
Definition angle.cpp:451
float acos_degree(float x)
Calculates the acos receiving x in degrees. It uses the acos() function of the standard library of C+...
Definition angle.hpp:247
float cosh(const angle &x)
Calculates the cosh of angle x. It uses the cosh() function of the standard library of C++,...
Definition angle.cpp:466
angle acosh(float x)
Calculates the acosh of some value x and returns the result as angle in degrees.
Definition angle.cpp:481
float turn_to_gradian(float x)
Definition angle.hpp:56
float acosh_degree(float x)
Calculates the acosh receiving x in degrees. It uses the acosh() function of the standard library of ...
Definition angle.hpp:262
float asin_degree(float x)
Calculates the asin receiving x in degrees. It uses the asin() function of the standard library of C+...
Definition angle.hpp:242
float radian_to_turn(float x)
Definition angle.hpp:66
angle asinh(float x)
Calculates the asinh of some value x and returns the result as angle in degrees.
Definition angle.cpp:476
angle atanh(float x)
Calculates the atanh of some value x and returns the result as angle in degrees.
Definition angle.cpp:486
angle acos(float x)
Calculates the acos of some value x and returns the result as angle in degrees.
Definition angle.cpp:446
float sin(const angle &x)
Calculates the sin of angle x. It uses the sin() function of the standard library of C++,...
Definition angle.cpp:426
float turn_to_degree(float x)
Definition angle.hpp:26
float asinh_degree(float x)
Calculates the asinh receiving x in degrees. It uses the asinh() function of the standard library of ...
Definition angle.hpp:257
angle sqrt(const angle &x)
Calculates the square root of the angle x and returns that new angle.
Definition angle.cpp:416
bool parallel(const angle &x, const angle &y)
Checks if two angles in a 2D correspond to parallel lines (or parallel vectors).
Definition angle.cpp:391
float gradian_to_degree(float x)
Definition angle.hpp:21
float radian_to_gradian(float x)
Definition angle.hpp:51
float gradian_to_turn(float x)
Definition angle.hpp:71
float tan(const angle &x)
Calculates the tan of angle x. It uses the tan() function of the standard library of C++,...
Definition angle.cpp:436
bool is_angle(const string &init_angle)
Checks if some string is an initialization string of an angle.
Definition angle.cpp:336