Added a way to output vec2d in an easy way

This commit is contained in:
Karma Riuk 2023-03-14 21:45:44 +01:00
parent d0670178c0
commit 7a5bf13575

45
vec2d.h
View File

@ -2,73 +2,78 @@
#define VEC2D_H_INCLUDED #define VEC2D_H_INCLUDED
#include <cmath> #include <cmath>
#include <ostream>
class vec2d { class vec2d {
public: public:
double x; double x;
double y; double y;
vec2d & operator = (const vec2d & other) { vec2d& operator=(const vec2d& other) {
x = other.x; x = other.x;
y = other.y; y = other.y;
return *this; return *this;
} }
vec2d & operator += (const vec2d & other) { vec2d& operator+=(const vec2d& other) {
x += other.x; x += other.x;
y += other.y; y += other.y;
return *this; return *this;
} }
vec2d & operator -= (const vec2d & other) { vec2d& operator-=(const vec2d& other) {
x -= other.x; x -= other.x;
y -= other.y; y -= other.y;
return *this; return *this;
} }
vec2d & operator *= (double l) { vec2d& operator*=(double l) {
x *= l; x *= l;
y *= l; y *= l;
return *this; return *this;
} }
vec2d operator + (const vec2d & other) const { vec2d operator+(const vec2d& other) const {
return vec2d{x + other.x, y + other.y}; return vec2d{x + other.x, y + other.y};
} }
vec2d operator - (const vec2d & other) const { vec2d operator-(const vec2d& other) const {
return vec2d{x - other.x, y - other.y}; return vec2d{x - other.x, y - other.y};
} }
vec2d operator * (double l) const { vec2d operator*(double l) const {
return vec2d{x*l, y*l}; return vec2d{x * l, y * l};
} }
vec2d operator / (double a) const { vec2d operator/(double a) const {
return vec2d{x/a, y/a}; return vec2d{x / a, y / a};
} }
vec2d & rotate (double angle) { vec2d& rotate(double angle) {
double sin_a = sin(angle); double sin_a = sin(angle);
double cos_a = cos(angle); double cos_a = cos(angle);
double x1 = x; double x1 = x;
double y1 = y; double y1 = y;
x = cos_a*x1 - sin_a*y1; x = cos_a * x1 - sin_a * y1;
y = sin_a*x1 + cos_a*y1; y = sin_a * x1 + cos_a * y1;
return *this; return *this;
} }
static double dot (const vec2d & a, const vec2d & b) { static double dot(const vec2d& a, const vec2d& b) {
return a.x*b.x + a.y*b.y; return a.x * b.x + a.y * b.y;
} }
static double cross (const vec2d & a, const vec2d & b) { static double cross(const vec2d& a, const vec2d& b) {
return a.x*b.x + a.y*b.y; return a.x * b.x + a.y * b.y;
} }
}; };
static inline vec2d operator * (double l, const vec2d & v) { static inline std::ostream& operator<<(std::ostream& os, vec2d& p) {
return vec2d{v.x*l, v.y*l}; return os << '(' << p.x << ", " << p.y << ')';
}
static inline vec2d operator*(double l, const vec2d& v) {
return vec2d{v.x * l, v.y * l};
} }
#endif #endif