35 lines
1022 B
C++
35 lines
1022 B
C++
#pragma once
|
|
#include <math.h>
|
|
|
|
struct Vector2d {
|
|
double x, y;
|
|
Vector2d(double a, double b) : x(a), y(b) {}
|
|
Vector2d() : x(.0), y(.0) {}
|
|
Vector2d operator+(Vector2d v) const { return Vector2d(v.x + x, v.y + y); }
|
|
Vector2d operator-(Vector2d v) const { return Vector2d(x - v.x, y - v.y); }
|
|
Vector2d operator-(double a) const { return Vector2d(x - a, y - a); }
|
|
Vector2d operator+(double a) const { return Vector2d(x + a, y + a); }
|
|
Vector2d operator/(double a) const { return Vector2d(x / a, y / a); }
|
|
Vector2d operator*(double a) const { return Vector2d(x * a, y * a); }
|
|
Vector2d operator+=(const Vector2d &v) {
|
|
*this = *this + v;
|
|
return *this;
|
|
}
|
|
Vector2d operator-=(const Vector2d &v) {
|
|
*this = *this - v;
|
|
return *this;
|
|
}
|
|
|
|
Vector2d reverse_sign() { return Vector2d(-x, -y); }
|
|
|
|
// # 1 ADD, 2 MULTs, 1 SQRT
|
|
double norm() { return sqrt(x * x + y * y); }
|
|
|
|
// # 1 ADD, 2 MULTs
|
|
double dot(Vector2d v) { return x * v.x + y * v.y; }
|
|
void reset() {
|
|
x = 0;
|
|
y = 0;
|
|
}
|
|
};
|