-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathVector2.cpp
More file actions
116 lines (85 loc) · 2.2 KB
/
Vector2.cpp
File metadata and controls
116 lines (85 loc) · 2.2 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "Vector2.h"
#include <math.h>
#include <iostream>
Vector2::Vector2(float x, float y)
:x(x), y(y)
{}
Vector2::Vector2(const Vector2& v)
:x(v.x), y(v.y)
{}
Vector2& Vector2::operator=(const Vector2& vect){
x = vect.x;
y = vect.y;
return *this;
}
Vector2::~Vector2(){}
float Vector2::length() const {
return sqrt(x * x + y * y);
}
Vector2& Vector2::lerp(const Vector2& a, const Vector2& b, float t, Vector2& dest){
dest.x = a.x + ((b.x - a.x) * t);
dest.y = a.y + ((b.y - a.y) * t);
return dest;
}
Vector2& Vector2::sub(const Vector2& left, const Vector2& right, Vector2& dest){
dest.x = left.x - right.x;
dest.y = left.y - right.y;
return dest;
}
Vector2& Vector2::add(const Vector2& left, const Vector2& right, Vector2& dest){
dest.x = left.x + right.x;
dest.y = left.y + right.y;
return dest;
}
float Vector2::dot(const Vector2& left, const Vector2& right){
return left.x * right.x + left.y * right.y;
}
Vector2& Vector2::operator+=(const Vector2& right){
Vector2::add(*this, right, *this);
return *this;
}
Vector2& Vector2::operator-=(const Vector2& right){
Vector2::sub(*this, right, *this);
return *this;
}
Vector2 Vector2::operator+(const Vector2& right) const{
Vector2 ret;
Vector2::add(*this, right, ret);
return ret;
}
Vector2 Vector2::operator-(const Vector2& right) const{
Vector2 ret;
Vector2::sub(*this, right, ret);
return ret;
}
float Vector2::operator*(const Vector2& b) const{
return Vector2::dot(*this, b);
}
float Vector2::angle(const Vector2& left, const Vector2& right){
float dot = Vector2::dot(left, right);
return acos(dot/(left.length() * right.length()));
}
float Vector2::dist(const Vector2& a, const Vector2& b){
float x = a.x - b.x;
float y = a.y - b.y;
return sqrt(x * x + y * y);
}
Vector2 Vector2::getNormalized(const Vector2& input){
Vector2 ret(input);
ret.normalize();
return ret;
}
Vector2& Vector2::scale(float s){
x *= s;
y *= s;
return *this;
}
Vector2& Vector2::normalize(){
float mag = length();
x /= mag;
y /= mag;
return *this;
}
void Vector2::print() const{
std::cout << x << " " << y << "\n";
}