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.cpp
Go to the documentation of this file.
1#include "./angle.hpp"
2
3#include "../util/types.hpp"
4
5#include "boost/algorithm/string.hpp"
6#include "unicode/unistr.h"
7#include "unicode/uchar.h"
8
9#include <cmath>
10#include <iostream>
11#include <sstream>
12#include <string>
13
14using namespace std;
15
16namespace scifir
17{
18 angle::angle() : value(0.0f)
19 {}
20
21 angle::angle(const angle& x) : value(x.get_value())
22 {}
23
24 angle::angle(angle&& x) : value(std::move(x.get_value()))
25 {}
26
28 {
30 {
32 }
33 else if (init_type == angle::RADIAN)
34 {
36 }
37 else if (init_type == angle::GRADIAN)
38 {
40 }
41 else if (init_type == angle::TURN)
42 {
44 }
46 }
47
49 {
51 {
53 }
54 else if (init_type == angle::RADIAN)
55 {
57 }
58 else if (init_type == angle::GRADIAN)
59 {
61 }
62 else if (init_type == angle::TURN)
63 {
65 }
67 }
68
70 {
72 {
74 }
75 else if (init_type == angle::RADIAN)
76 {
78 }
79 else if (init_type == angle::GRADIAN)
80 {
82 }
83 else if (init_type == angle::TURN)
84 {
86 }
88 }
89
91 {
93 {
95 }
96 else if (init_type == angle::RADIAN)
97 {
99 }
100 else if (init_type == angle::GRADIAN)
101 {
103 }
104 else if (init_type == angle::TURN)
105 {
107 }
109 }
110
111 angle::angle(const string& init_angle) : value()
112 {
114 }
115
117 {
118 if (x.has_empty_dimensions())
119 {
120 value = float(x);
122 }
123 else
124 {
125 cerr << "An angle cannot be initialized with dimensions" << endl;
126 value = 0.0f;
127 }
128 }
129
131 {
132 value = x.get_value();
133 return *this;
134 }
135
137 {
138 value = std::move(x.get_value());
139 return *this;
140 }
141
143 {
146 return *this;
147 }
148
150 {
152 return *this;
153 }
154
156 {
157 if (x.has_empty_dimensions())
158 {
159 value = x.get_value();
161 }
162 else
163 {
164 cerr << "An angle cannot be initialized with dimensions" << endl;
165 }
166 return *this;
167 }
168
170 {
171 return angle(value + x.get_value());
172 }
173
175 {
176 return angle(value - x.get_value());
177 }
178
180 {
181 return angle(value * x.get_value());
182 }
183
185 {
186 return angle(value / x.get_value());
187 }
188
190 {
191 return angle(std::pow(value,x.get_value()));
192 }
193
195 {
196 value += x.get_value();
198 }
199
201 {
202 value -= x.get_value();
204 }
205
207 {
208 value *= x.get_value();
210 }
211
213 {
214 value /= x.get_value();
216 }
217
219 {
220 value = std::pow(value,x.get_value());
222 }
223
225 {
226 value++;
227 return *this;
228 }
229
231 {
232 angle tmp = angle(*this);
233 operator ++();
234 return tmp;
235 }
236
238 {
239 value--;
240 return *this;
241 }
242
244 {
245 angle tmp = angle(*this);
246 operator --();
247 return tmp;
248 }
249
251 {
252 value += 180.0f;
254 }
255
260
262 {
264 if (get_value() == -0)
265 {
266 output << 0;
267 }
268 else
269 {
271 }
272 output << "°";
273 return output.str();
274 }
275
277 {
278 if(isfinite(value))
279 {
280 if (value >= 360.0f)
281 {
282 while (value >= 360.0f)
283 {
284 value -= 360.0f;
285 }
286 }
287 else if (value < 0.0f)
288 {
289 while (value < 0.0f)
290 {
291 value += 360.0f;
292 }
293 }
294 }
295 }
296
298 {
299 if (init_angle.length() >= 5 and init_angle.substr(init_angle.length() - 4) == " deg")
300 {
301 value = stof(init_angle.substr(0,init_angle.length() - 4));
303 return;
304 }
305 else if (init_angle.length() >= 5 and init_angle.substr(init_angle.length() - 4) == " rad")
306 {
307 value = radian_to_degree(stof(init_angle.substr(0,init_angle.length() - 4)));
309 return;
310 }
311 else if (init_angle.length() >= 6 and init_angle.substr(init_angle.length() - 5) == " grad")
312 {
313 value = gradian_to_degree(stof(init_angle.substr(0,init_angle.length() - 5)));
315 return;
316 }
317 else if (init_angle.length() >= 4 and init_angle.substr(init_angle.length() - 3) == " tr")
318 {
319 value = turn_to_degree(stof(init_angle.substr(0,init_angle.length() - 3)));
321 return;
322 }
323 icu::UnicodeString init_angle_unicode = icu::UnicodeString(init_angle.c_str());
324 if (init_angle_unicode.endsWith(0x00B0) or init_angle_unicode.endsWith(0x00BA) or init_angle_unicode.endsWith(0x03B8) or init_angle_unicode.endsWith(0x03A6))
325 {
328 }
329 }
330
331 string to_string(const angle& x)
332 {
333 return x.display(2);
334 }
335
336 bool is_angle(const string& init_angle)
337 {
338 icu::UnicodeString x_unicode = icu::UnicodeString(init_angle.c_str());
339 int total_chars = x_unicode.countChar32();
340 bool loop = false;
341 if (x_unicode[total_chars - 1] == 0x00B0 || x_unicode[total_chars - 1] == 0x00BA)
342 {
343 loop = true;
344 }
345 else if (total_chars >= 5 and init_angle.substr(init_angle.length() - 4) == " deg")
346 {
347 loop = true;
348 total_chars -= 4;
349 }
350 else if (total_chars >= 5 and init_angle.substr(init_angle.length() - 4) == " rad")
351 {
352 loop = true;
353 total_chars -= 4;
354 }
355 else if (total_chars>= 6 and init_angle.substr(init_angle.length() - 5) == " grad")
356 {
357 loop = true;
358 total_chars -= 5;
359 }
360 else if (total_chars >= 4 and init_angle.substr(init_angle.length() - 3) == " tr")
361 {
362 loop = true;
363 total_chars -= 3;
364 }
365 if (loop == false)
366 {
367 return false;
368 }
369 bool dot_present = false;
370 for (int i = 0; i < (total_chars - 1); i++)
371 {
372 if (x_unicode[i] == '.')
373 {
374 if (dot_present)
375 {
376 return false;
377 }
378 else
379 {
380 dot_present = true;
381 }
382 }
383 else if (!u_isdigit(x_unicode[i]))
384 {
385 return false;
386 }
387 }
388 return true;
389 }
390
391 bool parallel(const angle& x, const angle& y)
392 {
393 if(x == y or (x + 180.0f) == y)
394 {
395 return true;
396 }
397 else
398 {
399 return false;
400 }
401 }
402
403 bool orthogonal(const angle& x, const angle& y)
404 {
405 float difference = std::abs((x - y).get_value());
406 if (difference == 90.0f or difference == 270.0f)
407 {
408 return true;
409 }
410 else
411 {
412 return false;
413 }
414 }
415
416 angle sqrt(const angle& x)
417 {
418 return angle(std::sqrt(x.get_value()));
419 }
420
421 angle sqrt_nth(const angle& x, int index)
422 {
423 return angle(std::pow(x.get_value(), float(1.0f / index)));
424 }
425
426 float sin(const angle& x)
427 {
428 return std::sin(x.get_radian());
429 }
430
431 float cos(const angle& x)
432 {
433 return std::cos(x.get_radian());
434 }
435
436 float tan(const angle& x)
437 {
438 return std::tan(x.get_radian());
439 }
440
441 angle asin(float x)
442 {
443 return angle(radian_to_degree(std::asin(x)));
444 }
445
446 angle acos(float x)
447 {
448 return angle(radian_to_degree(std::acos(x)));
449 }
450
451 angle atan(float x)
452 {
453 return angle(radian_to_degree(std::atan(x)));
454 }
455
456 angle atan2(float y,float x)
457 {
458 return angle(radian_to_degree(std::atan2(y,x)));
459 }
460
461 float sinh(const angle& x)
462 {
463 return std::sinh(x.get_radian());
464 }
465
466 float cosh(const angle& x)
467 {
468 return std::cosh(x.get_radian());
469 }
470
471 float tanh(const angle& x)
472 {
473 return std::tanh(x.get_radian());
474 }
475
476 angle asinh(float x)
477 {
478 return angle(radian_to_degree(std::asinh(x)));
479 }
480
481 angle acosh(float x)
482 {
483 return angle(radian_to_degree(std::acosh(x)));
484 }
485
486 angle atanh(float x)
487 {
488 return angle(radian_to_degree(std::atanh(x)));
489 }
490}
491
493{
494 if(x.get_value() == y.get_value())
495 {
496 return true;
497 }
498 else
499 {
500 return false;
501 }
502}
503
505{
506 return !(x == y);
507}
508
509bool operator <(const scifir::angle& x, const scifir::angle& y)
510{
511 if(x.get_value() < y.get_value())
512 {
513 return true;
514 }
515 else
516 {
517 return false;
518 }
519}
520
521bool operator >(const scifir::angle& x, const scifir::angle& y)
522{
523 if(x.get_value() > y.get_value())
524 {
525 return true;
526 }
527 else
528 {
529 return false;
530 }
531}
532
534{
535 return !(x > y);
536}
537
539{
540 return !(x < y);
541}
542
543bool operator ==(const scifir::angle& x, const string& init_angle)
544{
545 scifir::angle y = scifir::angle(init_angle);
546 return (x == y);
547}
548
549bool operator !=(const scifir::angle& x, const string& init_angle)
550{
551 return !(x == init_angle);
552}
553
554bool operator ==(const string& init_angle, const scifir::angle& x)
555{
556 scifir::angle y = scifir::angle(init_angle);
557 return (x == y);
558}
559
560bool operator !=(const string& init_angle, const scifir::angle& x)
561{
562 return !(init_angle == x);
563}
564
565void operator +=(string& x, const scifir::angle& y)
566{
567 ostringstream output;
568 output << y;
569 x += output.str();
570}
571
572string operator +(const string& x, const scifir::angle& y)
573{
574 ostringstream output;
575 output << x;
576 output << y;
577 return output.str();
578}
579
580string operator +(const scifir::angle& y, const string& x)
581{
582 ostringstream output;
583 output << y;
584 output << x;
585 return output.str();
586}
587
588ostream& operator <<(ostream& os, const scifir::angle& x)
589{
590 return os << to_string(x);
591}
592
593istream& operator >>(istream& is, scifir::angle& x)
594{
595 char a[256];
596 is.getline(a, 256);
597 string b(a);
598 boost::trim(b);
599 x = scifir::angle(b);
600 return is;
601}
istream & operator>>(istream &is, scifir::angle &x)
Initializes an angle with an initialization string obtained from an input stream.
Definition angle.cpp:593
bool operator<(const scifir::angle &x, const scifir::angle &y)
Checks if the value of angle x is lower than the value of angle y.
Definition angle.cpp:509
bool operator!=(const scifir::angle &x, const scifir::angle &y)
Checks if two angles have not equal value.
Definition angle.cpp:504
bool operator==(const scifir::angle &x, const scifir::angle &y)
Checks if two angles have equal value.
Definition angle.cpp:492
bool operator>(const scifir::angle &x, const scifir::angle &y)
Checks if the value of angle x is greather than the value of angle y.
Definition angle.cpp:521
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>=(const scifir::angle &x, const scifir::angle &y)
Checks if the value of angle x is equal or greather than the value of angle y.
Definition angle.cpp:538
bool operator<=(const scifir::angle &x, const scifir::angle &y)
Checks if the value of angle x is lower or equal than the value of angle y.
Definition angle.cpp:533
string operator+(const string &x, const scifir::angle &y)
Concatenates the string x with the string representation of angle y into a new string.
Definition angle.cpp:572
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
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 represents dimensions of the SI system of units. Each dimension sizes 6 bytes,...
Definition dimension.hpp:31
@ NUMERATOR
The dimension is at the numerator.
Definition dimension.hpp:38
@ DEGREE
Degree, plural degrees. Dimension of angle. Symbol θ.
Definition dimension.hpp:35
@ NONE
There is no prefix. Then, the dimension is not increased or decreased by some factor.
Definition prefix.hpp:16
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions....
bool has_empty_dimensions() const
Checks if there aren't base dimensions.
const float & get_value() const
Read-only getter of the value.
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
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
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
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
angle atan(float x)
Calculates the atan of some value x and returns the result as angle in degrees.
Definition angle.cpp:451
string display_float(const float &value, int number_of_decimals)
Definition types.cpp:36
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
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
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 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