-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec2.hpp
57 lines (41 loc) · 1014 Bytes
/
vec2.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef VEC2_HPP
#define VEC2_HPP
class Vec2 {
public:
double x, y;
Vec2(double x, double y) : x{x}, y{y} {}
Vec2 operator+(const Vec2 rhs) {
return Vec2(x + rhs.x, y + rhs.y);
}
Vec2 operator-(const Vec2 rhs) {
return Vec2(x - rhs.x, y - rhs.y);
}
Vec2 operator-() {
return Vec2(-x, -y);
}
Vec2 operator+=(const Vec2 rhs) {
*this = *this + rhs;
return *this;
}
Vec2 operator/(const double lambda) {
return Vec2(x / lambda, y / lambda);
}
Vec2 operator*(const double lambda) {
return Vec2(x * lambda, y * lambda);
}
void print() {
std::cout << std::fixed;
std::cout << std::setprecision(4) << "(" << x << ", " << y << ")\n" << std::flush;
}
double norm() {
return sqrt(x*x + y*y);
}
double dot(const Vec2 &rhs) {
return x * rhs.x + y * rhs.y;
}
friend Vec2 operator*(const double lambda, const Vec2 &v);
};
Vec2 operator*(const double lambda, const Vec2 &v) {
return Vec2(v.x * lambda, v.y * lambda);
}
#endif