levelz-cpp 0.2.0
Loading...
Searching...
No Matches
coordinate.hpp
1#pragma once
2
3#include <vector>
4#include <array>
5
6namespace LevelZ {
7
11 struct Coordinate {
16 virtual std::string to_string() const = 0;
17
22 virtual double getMagnitude() const = 0;
23 };
24
29 public:
33 double x;
34
38 double y;
39
43 Coordinate2D() : x(0), y(0) {}
44
50 Coordinate2D(int x, int y) : x(x), y(y) {}
51
57 Coordinate2D(double x, double y) : x(x), y(y) {}
58
63 Coordinate2D(std::vector<int> xy) : x(xy.at(0)), y(xy.at(1)) {}
64
69 Coordinate2D(std::array<int, 2> xy) : x(xy.at(0)), y(xy.at(1)) {}
70
75 Coordinate2D(std::vector<double> xy) : x(xy.at(0)), y(xy.at(1)) {}
76
81 Coordinate2D(std::array<double, 2> xy) : x(xy.at(0)), y(xy.at(1)) {}
82
87 double getMagnitude() const {
88 return x * x + y * y;
89 }
90
96 bool operator==(const Coordinate2D& other) const {
97 return x == other.x && y == other.y;
98 }
99
105 bool operator!=(const Coordinate2D& other) const {
106 return x != other.x || y != other.y;
107 }
108
114 Coordinate2D operator+(const Coordinate2D& other) const {
115 return Coordinate2D(x + other.x, y + other.y);
116 }
117
123 Coordinate2D operator-(const Coordinate2D& other) const {
124 return Coordinate2D(x - other.x, y - other.y);
125 }
126
132 Coordinate2D operator*(int scalar) const {
133 return Coordinate2D(x * scalar, y * scalar);
134 }
135
141 Coordinate2D operator/(int scalar) const {
142 return Coordinate2D(x / scalar, y / scalar);
143 }
144
149 std::string to_string() const {
150 return "[" + std::to_string(x) + "," + std::to_string(y) + "]";
151 }
152
158 std::ostream& operator<<(std::ostream &strm) {
159 return strm << to_string();
160 }
161
167 static Coordinate2D from_string(const std::string& str) {
168 std::string x = str.substr(1, str.find(",") - 1);
169 std::string y = str.substr(str.find(",") + 1, str.find("]") - str.find(",") - 1);
170 return Coordinate2D(std::stod(x), std::stod(y));
171 }
172 };
173
178 public:
182 double x;
183
187 double y;
188
192 double z;
193
197 Coordinate3D() : x(0), y(0), z(0) {}
198
205 Coordinate3D(int x, int y, int z) : x(x), y(y), z(z) {}
206
213 Coordinate3D(double x, double y, double z) : x(x), y(y), z(z) {}
214
219 explicit Coordinate3D(std::vector<int> xyz) : x(xyz.at(0)), y(xyz.at(1)), z(xyz.at(2)) {}
220
225 explicit Coordinate3D(std::array<int, 3> xyz) : x(xyz.at(0)), y(xyz.at(1)), z(xyz.at(2)) {}
226
231 explicit Coordinate3D(std::vector<double> xyz) : x(xyz.at(0)), y(xyz.at(1)), z(xyz.at(2)) {}
232
237 explicit Coordinate3D(std::array<double, 3> xyz) : x(xyz.at(0)), y(xyz.at(1)), z(xyz.at(2)) {}
238
243 double getMagnitude() const {
244 return x * x + y * y + z * z;
245 }
246
252 bool operator==(const Coordinate3D& other) const {
253 return x == other.x && y == other.y && z == other.z;
254 }
255
261 bool operator!=(const Coordinate3D& other) const {
262 return x != other.x || y != other.y || z != other.z;
263 }
264
270 Coordinate3D operator+(const Coordinate3D& other) const {
271 return Coordinate3D(x + other.x, y + other.y, z + other.z);
272 }
273
279 Coordinate3D operator-(const Coordinate3D& other) const {
280 return Coordinate3D(x - other.x, y - other.y, z - other.z);
281 }
282
288 Coordinate3D operator*(int scalar) const {
289 return Coordinate3D(x * scalar, y * scalar, z * scalar);
290 }
291
297 Coordinate3D operator/(int scalar) const {
298 return Coordinate3D(x / scalar, y / scalar, z / scalar);
299 }
300
305 std::string to_string() const {
306 return "[" + std::to_string(x) + "," + std::to_string(y) + "," + std::to_string(z) + "]";
307 }
308
314 std::ostream& operator<<(std::ostream &strm) {
315 return strm << to_string();
316 }
317
323 static Coordinate3D from_string(const std::string& str) {
324 std::string x = str.substr(1, str.find(",") - 1);
325 std::string y = str.substr(str.find(",") + 1, str.rfind(",") - str.find(",") - 1);
326 std::string z = str.substr(str.rfind(",") + 1, str.find("]") - str.rfind(",") - 1);
327 return Coordinate3D(std::stod(x), std::stod(y), std::stod(z));
328 }
329 };
330}
Definition coordinate.hpp:28
Coordinate2D(std::vector< int > xy)
Definition coordinate.hpp:63
double getMagnitude() const
Definition coordinate.hpp:87
Coordinate2D(std::array< int, 2 > xy)
Definition coordinate.hpp:69
static Coordinate2D from_string(const std::string &str)
Definition coordinate.hpp:167
Coordinate2D(double x, double y)
Definition coordinate.hpp:57
bool operator==(const Coordinate2D &other) const
Definition coordinate.hpp:96
std::ostream & operator<<(std::ostream &strm)
Definition coordinate.hpp:158
double y
Definition coordinate.hpp:38
Coordinate2D(std::vector< double > xy)
Definition coordinate.hpp:75
Coordinate2D()
Definition coordinate.hpp:43
Coordinate2D operator/(int scalar) const
Definition coordinate.hpp:141
bool operator!=(const Coordinate2D &other) const
Definition coordinate.hpp:105
Coordinate2D operator+(const Coordinate2D &other) const
Definition coordinate.hpp:114
Coordinate2D operator-(const Coordinate2D &other) const
Definition coordinate.hpp:123
std::string to_string() const
Definition coordinate.hpp:149
double x
Definition coordinate.hpp:33
Coordinate2D operator*(int scalar) const
Definition coordinate.hpp:132
Coordinate2D(int x, int y)
Definition coordinate.hpp:50
Coordinate2D(std::array< double, 2 > xy)
Definition coordinate.hpp:81
Definition coordinate.hpp:177
std::string to_string() const
Definition coordinate.hpp:305
double x
Definition coordinate.hpp:182
double z
Definition coordinate.hpp:192
Coordinate3D operator/(int scalar) const
Definition coordinate.hpp:297
Coordinate3D()
Definition coordinate.hpp:197
Coordinate3D(double x, double y, double z)
Definition coordinate.hpp:213
Coordinate3D(std::array< int, 3 > xyz)
Definition coordinate.hpp:225
Coordinate3D(int x, int y, int z)
Definition coordinate.hpp:205
Coordinate3D operator+(const Coordinate3D &other) const
Definition coordinate.hpp:270
Coordinate3D operator-(const Coordinate3D &other) const
Definition coordinate.hpp:279
Coordinate3D(std::vector< int > xyz)
Definition coordinate.hpp:219
static Coordinate3D from_string(const std::string &str)
Definition coordinate.hpp:323
std::ostream & operator<<(std::ostream &strm)
Definition coordinate.hpp:314
double getMagnitude() const
Definition coordinate.hpp:243
Coordinate3D operator*(int scalar) const
Definition coordinate.hpp:288
double y
Definition coordinate.hpp:187
Coordinate3D(std::vector< double > xyz)
Definition coordinate.hpp:231
Coordinate3D(std::array< double, 3 > xyz)
Definition coordinate.hpp:237
bool operator==(const Coordinate3D &other) const
Definition coordinate.hpp:252
bool operator!=(const Coordinate3D &other) const
Definition coordinate.hpp:261
Definition coordinate.hpp:11
virtual double getMagnitude() const =0
virtual std::string to_string() const =0