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_2d.hpp
Go to the documentation of this file.
1#ifndef SCIFIR_UNITS_COORDINATES_COORDINATES_2D_HPP_INCLUDED
2#define SCIFIR_UNITS_COORDINATES_COORDINATES_2D_HPP_INCLUDED
3
4#include "../units/base_units.hpp"
5#include "../derived_units/physics_units.hpp"
6#include "../util/types.hpp"
7
8#include <iostream>
9#include <string>
10
11using namespace std;
12
13namespace scifir
14{
15 template<typename T = length>
17 {
18 public:
19 enum type { CARTESIAN, POLAR };
20
22 {}
23
26
29
31 {}
32
37
49
54
56 {
58 y = x_coordinates.y;
59 return *this;
60 }
61
63 {
64 x = std::move(x_coordinates.x);
65 y = std::move(x_coordinates.y);
66 return *this;
67 }
68
74
81
82 T get_p() const
83 {
84 return T(scifir::sqrt(scifir::pow(x,2) + scifir::pow(y,2)));
85 }
86
88 {
89 return scifir::atan2(y.get_value(),x.get_value());
90 }
91
93 {
94 x = new_x;
95 y = new_y;
96 }
97
99 {
102 }
103
111
113 {
114 x += x_displacement.x_projection();
115 y += x_displacement.y_projection();
116 }
117
119 {
120 x += new_x;
121 y += new_y;
122 }
123
125 {
128 }
129
131 {
132 return T(scifir::sqrt(scifir::pow(x,2) + scifir::pow(y,2)));
133 }
134
135 string display_cartesian() const
136 {
138 out << "(" << x << "," << y << ")";
139 return out.str();
140 }
141
142 string display_polar() const
143 {
145 out << "(" << get_p() << "," << get_theta() << ")";
146 return out.str();
147 }
148
151
152 private:
154 {
155 vector<string> values;
156 if (init_coordinates_2d.front() == '(')
157 {
158 init_coordinates_2d.erase(0,1);
159 }
160 if (init_coordinates_2d.back() == ')')
161 {
163 }
164 boost::split(values,init_coordinates_2d,boost::is_any_of(","));
165 if (values.size() == 2)
166 {
167 if (is_angle(values[1]))
168 {
169 set_position(T(values[0]),angle(values[1]));
170 }
171 else
172 {
173 set_position(T(values[0]),T(values[1]));
174 }
175 }
176 }
177 };
178
179 template<>
181 {
182 public:
184 {}
185
188
191
192 explicit coordinates_2d(float new_x,float new_y) : x(new_x),y(new_y)
193 {}
194
195 explicit coordinates_2d(float new_p,const angle& new_theta)
196 {
198 }
199
211
216
223
225 {
226 x = std::move(x_coordinates.x);
227 y = std::move(x_coordinates.y);
228 return *this;
229 }
230
236
237 float get_p() const
238 {
239 return float(std::sqrt(std::pow(x,2) + std::pow(y,2)));
240 }
241
243 {
244 return scifir::atan2(y,x);
245 }
246
247 void set_position(float new_x,float new_y)
248 {
249 x = new_x;
250 y = new_y;
251 }
252
254 {
257 }
258
259 void rotate(const angle& x_angle)
260 {
261 float x_coord = x;
262 float y_coord = y;
265 }
266
268 {
269 x += float(x_displacement.x_projection());
270 y += float(x_displacement.y_projection());
271 }
272
273 void move(float new_x,float new_y)
274 {
275 x += new_x;
276 y += new_y;
277 }
278
279 void move(float new_p,const angle& new_theta)
280 {
283 }
284
285 float distance_to_origin() const
286 {
287 return float(std::sqrt(std::pow(x,2) + std::pow(y,2)));
288 }
289
290 string display_cartesian() const
291 {
293 out << "(" << display_float(x) << "," << display_float(y) << ")";
294 return out.str();
295 }
296
297 string display_polar() const
298 {
300 out << "(" << display_float(get_p(),2) << "," << get_theta() << ")";
301 return out.str();
302 }
303
304 float x;
305 float y;
306
307 private:
309 {
310 vector<string> values;
311 if (init_coordinates_2d.front() == '(')
312 {
313 init_coordinates_2d.erase(0,1);
314 }
315 if (init_coordinates_2d.back() == ')')
316 {
318 }
319 boost::split(values,init_coordinates_2d,boost::is_any_of(","));
320 if (values.size() == 2)
321 {
322 if (values[0] == "" or values[1] == "")
323 {
324 return;
325 }
326 if (is_angle(values[1]))
327 {
328 set_position(stof(values[0]),angle(values[1]));
329 }
330 else
331 {
332 set_position(stof(values[0]),stof(values[1]));
333 }
334 }
335 }
336 };
337
338 template<typename T>
340 {
341 return x.display_cartesian();
342 }
343
344 string to_string(const coordinates_2d<float>& x);
345
346 template<typename T,typename U>
348 {
349 return T(scifir::sqrt(scifir::pow(x.x - y.x,2) + scifir::pow(x.y - y.y,2)));
350 }
351
352 float distance(const coordinates_2d<float>& x,const coordinates_2d<float>& y);
353
355 {
357 return scalar_unit(std::sqrt(std::pow(float(x),2) + std::pow(float(y),2)),x.get_dimensions());
358 }
359
361 {
363 return scifir::angle(scifir::atan_degree(float(y)/float(x)));
364 }
365
367 {
368 return p * scifir::cos(theta);
369 }
370
372 {
373 return p * scifir::sin(theta);
374 }
375
376 inline float cartesian_2d_to_polar_p(float x,float y)
377 {
378 return float(std::sqrt(std::pow(x,2) + std::pow(y,2)));
379 }
380
381 inline angle cartesian_2d_to_polar_theta(float x,float y)
382 {
384 }
385
386 inline float polar_to_cartesian_2d_x(float p,const angle& theta)
387 {
388 return p * scifir::cos(theta);
389 }
390
391 inline float polar_to_cartesian_2d_y(float p,const angle& theta)
392 {
393 return p * scifir::sin(theta);
394 }
395}
396
397template<typename T,typename U>
399{
400 if (x.x == y.x and x.y == y.y)
401 {
402 return true;
403 }
404 else
405 {
406 return false;
407 }
408}
409
410template<typename T,typename U>
412{
413 return !(x == y);
414}
415
416template<typename T>
417bool operator ==(const scifir::coordinates_2d<T>& x, const string& init_coordinates_2d)
418{
419 scifir::coordinates_2d<T> y(init_coordinates_2d);
420 return (x == y);
421}
422
423template<typename T>
424bool operator !=(const scifir::coordinates_2d<T>& x, const string& init_coordinates_2d)
425{
426 return !(x == init_coordinates_2d);
427}
428
429template<typename T>
430bool operator ==(const string& init_coordinates_2d, const scifir::coordinates_2d<T>& x)
431{
432 scifir::coordinates_2d<T> y(init_coordinates_2d);
433 return (x == y);
434}
435
436template<typename T>
437bool operator !=(const string& init_coordinates_2d, const scifir::coordinates_2d<T>& x)
438{
439 return !(init_coordinates_2d == x);
440}
441
442template<typename T>
443void operator +=(string& x, const scifir::coordinates_2d<T>& y)
444{
445 x += to_string(y);
446}
447
448template<typename T>
449string operator +(const string& x,const scifir::coordinates_2d<T>& y)
450{
451 return x + to_string(y);
452}
453
454template<typename T>
455string operator +(const scifir::coordinates_2d<T>& x,const string& y)
456{
457 return to_string(x) + y;
458}
459
460template<typename T>
461ostream& operator <<(ostream& os,const scifir::coordinates_2d<T>& x)
462{
463 return os << to_string(x);
464}
465
466ostream& operator <<(ostream& os,const scifir::coordinates_2d<float>& x);
467
468template<typename T>
470{
471 char a[256];
472 is.getline(a, 256);
473 string b(a);
474 boost::trim(b);
476 return is;
477}
478
479#endif // SCIFIR_UNITS_COORDINATES_COORDINATES_2D_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)
void initialize_from_string(string init_coordinates_2d)
void move(float new_p, const angle &new_theta)
coordinates_2d(float new_p, const angle &new_theta)
coordinates_2d(const string &init_coordinates_2d)
void move(const displacement_2d &x_displacement)
void rotate(const angle &x_angle)
coordinates_2d(const coordinates_2d< float > &x_coordinates)
void set_position(float new_p, const angle &new_theta)
coordinates_2d(float new_x, float new_y)
void set_position(float new_x, float new_y)
coordinates_2d(coordinates_2d< length >::type coordinates_type, const string &coord1, const string &coord2)
coordinates_2d(coordinates_2d< float > &&x_coordinates)
void move(float new_x, float new_y)
coordinates_2d(const string &init_coordinates_2d)
void move(const displacement_2d &x_displacement)
static coordinates_2d< T > origin(const coordinates_2d< T > &origin, const coordinates_2d< T > &coordinates)
void rotate(const angle &x_angle)
void move(const scalar_unit &new_x, const scalar_unit &new_y)
coordinates_2d(const coordinates_2d< T > &x_coordinates)
coordinates_2d(coordinates_2d::type coordinates_type, const string &coord1, const string &coord2)
coordinates_2d(coordinates_2d< T > &&x_coordinates)
coordinates_2d< T > & operator=(const coordinates_2d< T > &x_coordinates)
string display_cartesian() const
void initialize_from_string(string init_coordinates_2d)
coordinates_2d(const scalar_unit &new_x, const scalar_unit &new_y)
coordinates_2d(const scalar_unit &new_p, const angle &new_theta)
void set_position(const scalar_unit &new_p, const angle &new_theta)
void set_position(const scalar_unit &new_x, const scalar_unit &new_y)
void move(const scalar_unit &new_p, const angle &new_theta)
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions....
const vector< dimension > & get_dimensions() const
Read-only getter of the dimensions.
void change_dimensions(const string &init_dimensions)
Changes the dimensions to the dimensions specified by the initialization string of dimensions.
string operator+(const string &x, const scifir::coordinates_2d< T > &y)
ostream & operator<<(ostream &os, const scifir::coordinates_2d< T > &x)
bool operator!=(const scifir::coordinates_2d< T > &x, const scifir::coordinates_2d< U > &y)
bool operator==(const scifir::coordinates_2d< T > &x, const scifir::coordinates_2d< U > &y)
void operator+=(string &x, const scifir::coordinates_2d< T > &y)
istream & operator>>(istream &is, scifir::coordinates_2d< T > &x)
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
scalar_unit polar_to_cartesian_2d_x(const scalar_unit &p, const angle &theta)
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
angle cartesian_2d_to_polar_theta(const scalar_unit &x, scalar_unit y)
scalar_unit pow(const scalar_unit &x, int exponent)
Exponentiates a scalar_unit to some numeric type, the dimensions are also exponentiated.
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
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)
scalar_unit cartesian_2d_to_polar_p(const scalar_unit &x, scalar_unit y)
string display_float(const float &value, int number_of_decimals)
Definition types.cpp:36
scalar_unit polar_to_cartesian_2d_y(const scalar_unit &p, const angle &theta)
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