27 lines
896 B
C++
27 lines
896 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;
|
|
}
|
|
// # 4 flops = 1 SQRT, 2 MULTs, 1 ADD
|
|
double norm() { return sqrt(x * x + y * y); }
|
|
// # 3 flops = 2 MULTs, 1 ADD
|
|
double dot(Vector2d v) { return x * v.x + y * v.y; }
|
|
void reset() {
|
|
x = 0;
|
|
y = 0;
|
|
}
|
|
};
|