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
coordinates_2dr.hpp
Go to the documentation of this file.
1#ifndef SCIFIR_UNITS_COORDINATES_COORDINATES_2DR_HPP_INCLUDED
2#define SCIFIR_UNITS_COORDINATES_COORDINATES_2DR_HPP_INCLUDED
3
5#include "../coordinates/direction.hpp"
6#include "../meca_number/angle.hpp"
7#include "../util/types.hpp"
8
9#include <iostream>
10#include <string>
11
12using namespace std;
13
14namespace scifir
15{
16 template<typename T = length>
18 {
19 public:
21 {}
22
25
28
31
36
37 template<typename U>
40
41 template<typename U>
44
49
51 {
53 y = x_coordinates.y;
54 theta = x_coordinates.theta;
55 return *this;
56 }
57
59 {
60 x = std::move(x_coordinates.x);
61 y = std::move(x_coordinates.y);
62 theta = std::move(x_coordinates.theta);
63 return *this;
64 }
65
67 {
69 y = x_coordinates.y;
70 return *this;
71 }
72
74 {
75 x = std::move(x_coordinates.x);
76 y = std::move(x_coordinates.y);
77 return *this;
78 }
79
85
92
93 T get_p() const
94 {
95 return T(scifir::sqrt(scifir::pow(x,2) + scifir::pow(y,2)));
96 }
97
99 {
100 return scifir::atan2(y.get_value(),x.get_value());
101 }
102
104 {
105 if (x == direction::LEFT)
106 {
107 theta = 180.0f;
108 }
109 else if(x == direction::RIGHT)
110 {
111 theta = 0.0f;
112 }
113 else if(x == direction::TOP)
114 {
115 theta = 90.0f;
116 }
117 else if(x == direction::BOTTOM)
118 {
119 theta = 270.0f;
120 }
121 else if(x == direction::LEFT_TOP)
122 {
123 theta = 135.0f;
124 }
125 else if(x == direction::RIGHT_TOP)
126 {
127 theta = 45.0f;
128 }
129 else if(x == direction::RIGHT_BOTTOM)
130 {
131 theta = 315.0f;
132 }
133 else if(x == direction::LEFT_BOTTOM)
134 {
135 theta = 225.0f;
136 }
137 }
138
140 {
141 x = new_x;
142 y = new_y;
143 }
144
146 {
149 }
150
158
160 {
161 x += x_displacement.x_projection();
162 y += x_displacement.y_projection();
163 }
164
166 {
167 x += new_x;
168 y += new_y;
169 }
170
172 {
175 }
176
178 {
179 return T(scifir::sqrt(scifir::pow(x,2) + scifir::pow(y,2)));
180 }
181
182 string display_cartesian() const
183 {
185 out << "(" << x << "," << y << ";" << theta << ")";
186 return out.str();
187 }
188
189 string display_polar() const
190 {
192 out << "(" << get_p() << "," << get_polar_theta() << ";" << theta << ")";
193 return out.str();
194 }
195
199
200 private:
202 {
206 if (init_coordinates_2dr.front() == '(')
207 {
208 init_coordinates_2dr.erase(0,1);
209 }
210 if (init_coordinates_2dr.back() == ')')
211 {
213 }
214 boost::split(init_coordinates,init_coordinates_2dr,boost::is_any_of(";"));
215 if (init_coordinates.size() > 0)
216 {
217 boost::split(init_values,init_coordinates[0],boost::is_any_of(","));
218 }
219 if (init_coordinates.size() > 1)
220 {
221 boost::split(init_angles,init_coordinates[1],boost::is_any_of(","));
222 }
223 if (init_values.size() == 2 and init_angles.size() == 1)
224 {
225 if (is_angle(init_values[1]))
226 {
228 }
229 else
230 {
232 }
233 theta = angle(init_angles[0]);
234 }
235 }
236 };
237
238 template<>
240 {
241 public:
243 {}
244
247
250
252 {}
253
258
261
264
269
271 {
272 x = x_coordinates.x;
273 y = x_coordinates.y;
274 theta = x_coordinates.theta;
275 return *this;
276 }
277
279 {
280 x = std::move(x_coordinates.x);
281 y = std::move(x_coordinates.y);
282 theta = std::move(x_coordinates.theta);
283 return *this;
284 }
285
292
294 {
295 x = std::move(x_coordinates.x);
296 y = std::move(x_coordinates.y);
297 return *this;
298 }
299
305
306 float get_p() const
307 {
308 return float(std::sqrt(std::pow(x,2) + std::pow(y,2)));
309 }
310
312 {
313 return scifir::atan2(y,x);
314 }
315
317 {
318 if (x == direction::LEFT)
319 {
320 theta = 180.0f;
321 }
322 else if(x == direction::RIGHT)
323 {
324 theta = 0.0f;
325 }
326 else if(x == direction::TOP)
327 {
328 theta = 90.0f;
329 }
330 else if(x == direction::BOTTOM)
331 {
332 theta = 270.0f;
333 }
334 else if(x == direction::LEFT_TOP)
335 {
336 theta = 135.0f;
337 }
338 else if(x == direction::RIGHT_TOP)
339 {
340 theta = 45.0f;
341 }
342 else if(x == direction::RIGHT_BOTTOM)
343 {
344 theta = 315.0f;
345 }
346 else if(x == direction::LEFT_BOTTOM)
347 {
348 theta = 225.0f;
349 }
350 }
351
352 void set_position(float new_x,float new_y)
353 {
354 x = new_x;
355 y = new_y;
356 }
357
359 {
362 }
363
364 void rotate(const angle& x_angle)
365 {
366 float x_coord = x;
367 float y_coord = y;
370 }
371
373 {
374 x += float(x_displacement.x_projection());
375 y += float(x_displacement.y_projection());
376 }
377
378 void move(float new_x,float new_y)
379 {
380 x += new_x;
381 y += new_y;
382 }
383
384 void move(float new_p,const angle& new_theta)
385 {
388 }
389
390 float distance_to_origin() const
391 {
392 return float(std::sqrt(std::pow(x,2) + std::pow(y,2)));
393 }
394
395 string display_cartesian() const
396 {
398 out << "(" << display_float(x) << "," << display_float(y) << ";" << theta << ")";
399 return out.str();
400 }
401
402 string display_polar() const
403 {
405 out << "(" << display_float(get_p()) << "," << get_polar_theta() << ";" << theta << ")";
406 return out.str();
407 }
408
409 float x;
410 float y;
412
413 private:
415 {
419 if (init_coordinates_2dr.front() == '(')
420 {
421 init_coordinates_2dr.erase(0,1);
422 }
423 if (init_coordinates_2dr.back() == ')')
424 {
426 }
427 boost::split(init_coordinates,init_coordinates_2dr,boost::is_any_of(";"));
428 if (init_coordinates.size() > 0)
429 {
430 boost::split(init_values,init_coordinates[0],boost::is_any_of(","));
431 }
432 if (init_coordinates.size() > 1)
433 {
434 boost::split(init_angles,init_coordinates[1],boost::is_any_of(","));
435 }
436 if (init_values.size() == 2 and init_angles.size() == 1)
437 {
438 if (init_values[0] == "" or init_values[1] == "" or init_angles[0] == "")
439 {
440 return;
441 }
442 if (is_angle(init_values[1]))
443 {
445 }
446 else
447 {
449 }
450 theta = angle(init_angles[0]);
451 }
452 }
453 };
454
455 template<typename T>
457 {
458 return x.display_cartesian();
459 }
460
461 string to_string(const coordinates_2dr<float>& x);
462
463 template<typename T,typename U>
465 {
466 return T(scifir::sqrt(scifir::pow(x.x - y.x,2) + scifir::pow(x.y - y.y,2)));
467 }
468
469 float distance(const coordinates_2dr<float>& x,const coordinates_2dr<float>& y);
470
471 template<typename T,typename U>
473 {
474 return T(scifir::sqrt(scifir::pow(x.x - y.x,2) + scifir::pow(x.y - y.y,2)));
475 }
476
477 float distance(const coordinates_2dr<float>& x,const coordinates_2d<float>& y);
478
479 template<typename T,typename U>
481 {
482 return T(scifir::sqrt(scifir::pow(x.x - y.x,2) + scifir::pow(x.y - y.y,2)));
483 }
484
485 float distance(const coordinates_2d<float>& x,const coordinates_2dr<float>& y);
486}
487
488template<typename T,typename U>
490{
491 if (x.x == y.x and x.y == y.y and x.theta == y.theta)
492 {
493 return true;
494 }
495 else
496 {
497 return false;
498 }
499}
500
501template<typename T,typename U>
503{
504 return !(x == y);
505}
506
507template<typename T,typename U>
509{
510 if (x.x == y.x and x.y == y.y)
511 {
512 return true;
513 }
514 else
515 {
516 return false;
517 }
518}
519
520template<typename T,typename U>
522{
523 return !(x == y);
524}
525
526template<typename T,typename U>
528{
529 if (x.x == y.x and x.y == y.y)
530 {
531 return true;
532 }
533 else
534 {
535 return false;
536 }
537}
538
539template<typename T,typename U>
541{
542 return !(x == y);
543}
544
545template<typename T>
546bool operator ==(const scifir::coordinates_2dr<T>& x, const string& init_coordinates_2dr)
547{
548 scifir::coordinates_2dr<T> y(init_coordinates_2dr);
549 return (x == y);
550}
551
552template<typename T>
553bool operator !=(const scifir::coordinates_2dr<T>& x, const string& init_coordinates_2dr)
554{
555 return !(x == init_coordinates_2dr);
556}
557
558template<typename T>
559bool operator ==(const string& init_coordinates_2dr, const scifir::coordinates_2dr<T>& x)
560{
561 scifir::coordinates_2dr<T> y(init_coordinates_2dr);
562 return (x == y);
563}
564
565template<typename T>
566bool operator !=(const string& init_coordinates_2dr, const scifir::coordinates_2dr<T>& x)
567{
568 return !(init_coordinates_2dr == x);
569}
570
571template<typename T>
573{
574 x += to_string(y);
575}
576
577template<typename T>
578string operator +(const string& x,const scifir::coordinates_2dr<T>& y)
579{
580 return x + to_string(y);
581}
582
583template<typename T>
584string operator +(const scifir::coordinates_2dr<T>& x,const string& y)
585{
586 return to_string(x) + y;
587}
588
589template<typename T>
590ostream& operator <<(ostream& os, const scifir::coordinates_2dr<T>& x)
591{
592 return os << to_string(x);
593}
594
595ostream& operator <<(ostream& os,const scifir::coordinates_2dr<float>& x);
596
597template<typename T>
599{
600 char a[256];
601 is.getline(a, 256);
602 string b(a);
603 boost::trim(b);
605 return is;
606}
607
608#endif // SCIFIR_UNITS_COORDINATES_COORDINATES_2DR_HPP_INCLUDED
Class that allows to work with angles. Each angle sizes 4 bytes. Initialization string example: "20°"...
Definition angle.hpp:77
string display_cartesian() const
void move(const scalar_unit &x_value)
coordinates_2dr(float new_p, const angle &new_polar_theta, const angle &new_theta)
void set_position(float new_p, const angle &new_theta)
void set_position(float new_x, float new_y)
void move(const displacement_2d &x_displacement)
coordinates_2dr(const scifir::coordinates_2d< float > &new_point, const angle &new_theta)
void initialize_from_string(string init_coordinates_2dr)
coordinates_2dr(scifir::coordinates_2d< float > &&new_point, const angle &new_theta)
coordinates_2dr(float new_x, float new_y, const angle &new_theta)
void move(float new_x, float new_y)
void rotate(const angle &x_angle)
coordinates_2dr(coordinates_2dr< float > &&x_coordinates)
coordinates_2dr(const string &init_coordinates_2dr)
void move(float new_p, const angle &new_theta)
coordinates_2dr(const coordinates_2dr< float > &x_coordinates)
coordinates_2dr(const scalar_unit &new_x, const scalar_unit &new_y, const angle &new_theta)
void move(const scalar_unit &new_x, const scalar_unit &new_y)
coordinates_2dr(const string &init_coordinates_2dr)
coordinates_2dr(scifir::coordinates_2d< U > &&new_coordinates, const angle &new_theta)
void initialize_from_string(string init_coordinates_2dr)
coordinates_2dr(const coordinates_2dr< T > &x_coordinates)
void point_to(direction::name x)
coordinates_2dr(const scifir::coordinates_2d< U > &new_coordinates, const angle &new_theta)
void move(const scalar_unit &new_p, const angle &new_theta)
void move(const displacement_2d &x_displacement)
coordinates_2dr< T > & operator=(const coordinates_2dr< T > &x_coordinates)
void set_position(const scalar_unit &new_p, const angle &new_theta)
coordinates_2dr(coordinates_2dr< T > &&x_coordinates)
coordinates_2dr(const scalar_unit &new_p, const angle &new_polar_theta, const angle &new_theta)
void set_position(const scalar_unit &new_x, const scalar_unit &new_y)
void rotate(const angle &x_angle)
static coordinates_2dr< T > origin(const coordinates_2dr< T > &origin, const coordinates_2dr< T > &coordinates)
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions....
ostream & operator<<(ostream &os, const scifir::coordinates_2dr< T > &x)
void operator+=(string &x, const scifir::coordinates_2dr< T > &y)
istream & operator>>(istream &is, scifir::coordinates_2dr< T > &x)
bool operator!=(const scifir::coordinates_2dr< T > &x, const scifir::coordinates_2dr< U > &y)
string operator+(const string &x, const scifir::coordinates_2dr< T > &y)
bool operator==(const scifir::coordinates_2dr< T > &x, const scifir::coordinates_2dr< U > &y)
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
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
scalar_unit pow(const scalar_unit &x, int exponent)
Exponentiates a scalar_unit to some numeric type, the dimensions are also exponentiated.
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 distance(const coordinates_1d< float > &x, const coordinates_1d< float > &y)
string display_float(const float &value, int number_of_decimals)
Definition types.cpp:36
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
angle sqrt(const angle &x)
Calculates the square root of the angle x and returns that new angle.
Definition angle.cpp:416
bool is_angle(const string &init_angle)
Checks if some string is an initialization string of an angle.
Definition angle.cpp:336