-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector2.cpp
53 lines (48 loc) · 1.27 KB
/
vector2.cpp
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
#include "vector2.h"
Vector2::Vector2(double x, double y) {
this->x = x;
this->y = y;
}
Vector2::Vector2() {
this->x = 0;
this->y = 0;
}
Vector2& Vector2::operator=(const Vector2& other) {
this->x = other.x;
this->y = other.y;
}
Vector2 Vector2::operator+(Vector2 other) {
Vector2 temp;
temp.x = this->x + other.x;
temp.y = this->y + other.y;
return temp;
}
Vector2 Vector2::operator-(Vector2 other) {
Vector2 temp;
temp.x = this->x - other.x;
temp.y = this->y - other.y;
return temp;
}
Vector2 Vector2::operator*(double multiplicand) {
Vector2 temp;
temp.x = this->x * multiplicand;
temp.y = this->y * multiplicand;
return temp;
}
double Vector2::dot(Vector2 other) {
return (this->x * other.x) + (this->y * other.y);
}
double Vector2::magnitude() {
return sqrt(pow(this->x,2) + pow(this->y,2));
}
int Vector2::rightOf(Vector2 other) {
Vector2 temp(-1 * this->y, this->x); // Rotate this vector 90 degrees counter clockwise
double result = other.dot(temp);
return (result > 0) ? 1 : ((result < 0) ? -1 : 0);
}
// Returns the projection vector of this onto other
Vector2 Vector2::project(Vector2 other) {
// this = duck
// other = player
return other * (this->dot(other) / other.dot(other));
}