OpenAstra
 
Loading...
Searching...
No Matches
starcoord.hpp
1#ifndef STAR_COORD_H
2#define STAR_COORD_H
3
4#include <cmath>
5
6#include "json.hpp"
7
8/*
9 * Radius is measured in kilo-Km, or kkm (consider the distance between sun and earth is average 150.000.000Km - 150M Km)
10 *
11 * The reason is that "kkm" cannot be easily confused with something else, and at the same time gives reasonable numbers
12 * like 150.000 for a earth-like planet, or 20.500.000 for the outer edges of the solar system itself (neptune is at 4.5bln km)
13 *
14 */
15class StarCoord
16{
17public:
18 static constexpr double precision = 0.01;
19
20 StarCoord()
21 : _x( 0.0 )
22 , _y( 0.0 ) {}
23
24 StarCoord(double x, double y )
25 : _x( x )
26 , _y( y ) {}
27
28 StarCoord( const StarCoord& other )
29 : _x( other._x )
30 , _y( other._y ) {}
31
32 double distanceKkm( const StarCoord& other ) const {
33 double dx = deltaX_kkm( other );
34 double dy = deltaY_kkm( other );
35 return std::sqrt( dx*dx + dy*dy );
36 }
37
38 double deltaX_kkm( const StarCoord& other ) const {
39 return _x - other._x;
40 }
41
42 double deltaY_kkm( const StarCoord& other ) const {
43 return _y - other._y;
44 }
45
46
47 double lenght() const {
48 return std::fabs( std::sqrt( _x*_x + _y*_y ) );
49 }
50
51
52 void operator += ( const StarCoord &b) {
53 _x += b._x;
54 _y += b._y;
55 }
56
57 const StarCoord &operator =( const StarCoord &b) {
58 _x = b._x;
59 _y = b._y;
60 return b;
61 }
62
63 double x() const { return _x; }
64 double y() const { return _y; }
65 double x( double x ) {
66 return _x = x;
67 }
68
69 double y( double y ) {
70 return _y = y;
71 }
72
73 bool isNaN() const {
74 return (_x == NAN) || (_y == NAN);
75 }
76
77 friend void to_json(nlohmann::json& j, const StarCoord& s) {
78 j = nlohmann::json{
79 {"x", s.x()},
80 {"y", s.y()}
81 };
82 }
83
84 friend void from_json(const nlohmann::json& j, StarCoord& s) {
85 s.x(j.at("x").get<double>());
86 s.y(j.at("y").get<double>());
87 }
88
89 friend StarCoord operator - (const StarCoord &a, const StarCoord &b) {
90 return StarCoord( a.x() - b.x(), a.y() - b.y() );
91 }
92
93 friend StarCoord operator + (const StarCoord &a, const StarCoord &b) {
94 return StarCoord( a.x() + b.x(), a.y() + b.y() );
95 }
96
97 friend bool operator < (const StarCoord &a, const StarCoord &b) {
98 if ( a.x() < b.x() )
99 return true;
100 if ( a.x() > b.x() )
101 return false;
102 return a.y() < b.y();
103 }
104
105 friend bool operator == (const StarCoord &a, const StarCoord &b) {
106 double delta_x = std::fabs( a.x() - b.x() );
107 double delta_y = std::fabs( a.y() - b.y() );
108 if ( ( delta_x < StarCoord::precision ) &&
109 ( delta_y < StarCoord::precision ) )
110 return true;
111 return false;
112 }
113
114 friend bool operator != (const StarCoord &a, const StarCoord &b) {
115 double delta_x = std::fabs( a.x() - b.x() );
116 double delta_y = std::fabs( a.y() - b.y() );
117 if ( ( delta_x > StarCoord::precision ) ||
118 ( delta_y > StarCoord::precision ) )
119 return true;
120 return false;
121 }
122
123private:
124 double _x;
125 double _y;
126
127};
128
129
130#endif // STAR_COORD_H