-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvector2.h
62 lines (49 loc) · 1.08 KB
/
vector2.h
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
58
59
60
61
62
#pragma once
#include <cmath>
class Vector2 {
public:
float x = 0;
float y = 0;
public:
Vector2() = default;
~Vector2() = default;
Vector2(float x, float y) :x(x), y(y){}
Vector2 operator+(const Vector2& vec) const{
return Vector2(x + vec.x, y + vec.y);
}
Vector2 operator+(float val) const {
return Vector2(x + val, y + val);
}
Vector2 operator-(const Vector2& vec) const {
return Vector2(x - vec.x, y - vec.y);
}
void operator+=(const Vector2& vec) {
x += vec.x, y += vec.y;
}
void operator-=(const Vector2& vec) {
x -= vec.x, y -= vec.y;
}
Vector2 operator*(const Vector2& vec) const {
return Vector2(x * vec.x, y * vec.y);
}
Vector2 operator*(float val) const {
return Vector2(x * val, y * val);
}
void operator*=(float val){
x *= val, y *= val;
}
bool operator==(const Vector2& vec) const{
if (vec.x == x && vec.y == y) { return true; }
else { return false; }
}
float length() {
return sqrt(x * x + y * y);
}
Vector2 normalize() {
float len = length();
if (len == 0) {
return Vector2(0, 0);
}
return Vector2(x / len, y / len);
}
};