-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2.cpp
More file actions
181 lines (163 loc) · 5.32 KB
/
Vector2.cpp
File metadata and controls
181 lines (163 loc) · 5.32 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// The functions contained in this file are pretty dummy
// and are included only as a placeholder. Nevertheless,
// they *will* get included in the static library if you
// don't remove them :)
//
// Obviously, you 'll have to write yourself the super-duper
// functions to include in the resulting library...
// Also, it's not necessary to write every function in this file.
// Feel free to add more files in this project. They will be
// included in the resulting library.
// A function adding two integers and returning the result
#ifndef LINEARCHEXT_VECTOR2_CPP
#define LINEARCHEXT_VECTOR2_CPP
#include <cmath>
#include "Vector2.hpp"
#include "Misc.hpp"
namespace LinearchExt{
Vector2::Vector2(){};
Vector2::Vector2(double x, double y){
this->x = x;
this->y = y;
}
Vector2 Vector2::zero = Vector2(0,0);
Vector2 Vector2::up = Vector2(0,1);
Vector2 Vector2::down = Vector2(0,-1);
Vector2 Vector2::left = Vector2(-1,0);
Vector2 Vector2::right = Vector2(1,0);
double Vector2::Magnitude(){
return sqrt(x*x + y*y);
}
Vector2 Vector2::Normalized(){
double mag = Magnitude();
if(mag == 0)
return *this;
return Vector2(x/mag, y/mag);
}
void Vector2::Normalize(){
double mag = Magnitude();
if(mag == 0)
return;
x /= mag;
y /= mag;
}
double Vector2::Distance(Vector2 a, Vector2 b){
return (b-a).Magnitude();
}
double Vector2::Dot(Vector2 a, Vector2 b){
return a.x * b.x + a.y * b.y;
}
double Vector2::Cross(Vector2 a, Vector2 b){
return a.x * b.y - a.y * b.x;
}
Vector2 Vector2::ClampMagnitude(Vector2 v, double m){
if (v.IsZero())
return v;
double mag = v.Magnitude();
return v / mag * Min(mag, m);
}
Vector2 Vector2::Rotate(Vector2 v, double rad){
double sina = sin(rad);
double cosa = cos(rad);
return Vector2(v.x * cosa - v.y * sina, v.x * sina + v.y * cosa);
}
Vector2 Vector2::Rotate(Vector2 v0, double rad, Vector2 pivot){
Vector2 v = v0 - pivot;
double sina = sin(rad);
double cosa = cos(rad);
return Vector2(v.x * cosa - v.y * sina, v.x * sina + v.y * cosa) + pivot;
}
Vector2 Vector2::Lerp(Vector2 a, Vector2 b, double t){
return a + (b-a) * t;
}
Vector2 Vector2::MoveTowards(Vector2 a, Vector2 b, double maxDelta){
return a + ClampMagnitude(b-a, maxDelta);
}
bool Vector2::IsZero (){
return x==0 && y==0;
}
Vector2 Vector2::Project(Vector2 v, Vector2 to){
if (v.IsZero())
return v;
if (to.IsZero())
return to;
double mag = to.Magnitude();
return Dot(v,to) * to / (mag * mag);
}
Vector2 Vector2::Reject(Vector2 v, Vector2 to){
return v - Project(v, to);
}
Vector2 Vector2::Reflect(Vector2 v, Vector2 onNormal){
return v - 2 * Project(v, onNormal);
}
Vector2 operator*(const double& d, const Vector2& v){
return Vector2(v.x * d, v.y * d);
}
Vector2 operator*(const Vector2& v, const double& d){
return Vector2(v.x * d, v.y * d);
}
Vector2 operator/(const double& d, const Vector2& v){
return Vector2(d/v.x, d/v.y);
}
Vector2 operator/(const Vector2& v, const double& d){
return Vector2(v.x / d, v.y / d);
}
Vector2 operator*(const int& d, const Vector2& v){
return Vector2(v.x * d, v.y * d);
}
Vector2 operator*(const Vector2& v, const int& d){
return Vector2(v.x * d, v.y * d);
}
Vector2 operator/(const int& d, const Vector2& v){
return Vector2(d/v.x, d/v.y);
}
Vector2 operator/(const Vector2& v, const int& d){
return Vector2(v.x / d, v.y / d);
}
Vector2 operator*(const float& d, const Vector2& v){
return Vector2(v.x * d, v.y * d);
}
Vector2 operator*(const Vector2& v, const float& d){
return Vector2(v.x * d, v.y * d);
}
Vector2 operator/(const float& d, const Vector2& v){
return Vector2(d/v.x, d/v.y);
}
Vector2 operator/(const Vector2& v, const float& d){
return Vector2(v.x / d, v.y / d);
}
Vector2 operator+(const Vector2& a, const Vector2& b){
return Vector2(a.x + b.x, a.y + b.y);
}
Vector2 operator-(const Vector2& a, const Vector2& b){
return Vector2(a.x - b.x, a.y - b.y);
}
Vector2 operator*(const Vector2& a, const Vector2& b){
return Vector2(a.x * b.x, a.y * b.y);
}
Vector2 operator/(const Vector2& a, const Vector2& b){
return Vector2(a.x / b.x, a.y / b.y);
}
bool operator==(const Vector2& a, const Vector2& b){
return a.x == b.x && a.y == b.y;
}
bool operator!=(const Vector2& a, const Vector2& b){
return a.x != b.x || a.y != b.y;
}
Vector2 Vector2::operator+=(Vector2 b){
return Vector2(x+b.x, y+b.y);
}
Vector2 Vector2::operator-=(Vector2 b){
return Vector2(x-b.x, y-b.y);
}
Vector2 Vector2::operator*=(Vector2 b){
return Vector2(x*b.x, y*b.y);
}
Vector2 Vector2::operator/=(Vector2 b){
return Vector2(x/b.x, y/b.y);
}
std::ostream& operator << (std::ostream &o,const Vector2 &v){
return o << "(" << v.x << ", " << v.y << ")";
}
}
#endif // VECTOR2_CPP