-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector2.cpp
160 lines (121 loc) · 2.6 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
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
#include"Vector2.h"
#include <cmath>
template<class T>
Vector2<T>::Vector2() :x(0.0f), y(0.0f)
{
}
template<class T>
Vector2<T>::Vector2(T x) : x(x), y(x)
{
}
template<class T>
Vector2<T>::Vector2(T x, T y) : x(x), y(y)
{
}
template<class T>
sf::Vector2<float> Vector2<T>::ConverttoSF()
{
return sf::Vector2<float>(static_cast<T>(x), static_cast<T>(y));
}
template<class T>
Vector2<T> Vector2<T>::add(Vector2<T> a)
{
return Vector2<T>(this->x + a.x, this->y + a.y);
}
template<class T>
Vector2<T> Vector2<T>::sub(Vector2<T> a)
{
return Vector2<T>(this->x - a.x, this->y - a.y);
}
template<class T>
Vector2<T> Vector2<T>::divide(int n)
{
return Vector2<T>(this->x / n, this->y / n);
}
template<class T>
Vector2<T> Vector2<T>::mult(int n)
{
return Vector2<T>(this->x * n, this->y * n);
}
template<class T>
float Vector2<T>::dist(Vector2<T> a)
{
T DiffX = this->x - a.x;
T DiffY = this->y - a.y;
return sqrt(DiffX * DiffX + DiffY * DiffY);
}
template<class T>
Vector2<T> Vector2<T>::setMag(float NewMag)
{
auto mag = sqrt(this->x * this->x + this->y * this->y);
return Vector2<T>((this->x * NewMag) / mag, (this->y * NewMag) / mag);
}
template<class T>
Vector2<T> Vector2<T>::limit(int n)
{
auto mag = sqrt(this->x * this->x + this->y * this->y);
if (mag > n)
{
return Vector2<T>((this->x * n) / mag, (this->y * n) / mag);
}
return *this;
}
template<class T>
Vector2<T> Vector2<T>::operator+(const Vector2<T>& Right)
{
return Vector2<T>(x + Right.x, y + Right.y);
}
template<class T>
Vector2<T> Vector2<T>::operator-(const Vector2<T>& Right)
{
return Vector2<T>(x - Right.x, y - Right.y);
}
template<class T>
Vector2<T>& Vector2<T>::operator=(const Vector2<T>& Vector2Obj)
{
x = Vector2Obj.x;
y = Vector2Obj.y;
return *this;
}
template<class T>
bool Vector2<T>::operator!=(const Vector2<T> &obj)
{
return !((x == obj.x) && (y == obj.y));
}
template<class T>
bool Vector2<T>::operator==(const Vector2<T> &obj)
{
return ((x == obj.x) && (y == obj.y));
}
template<class T>
float Vector2<T>::Vector2::getLength()
{
return (float)sqrt(x*x + y * y);
}
template<class T>
Vector2<T> Vector2<T>::normalize()
{
float m = getLength();
x = x / m;
y = y / m;
return *this;
}
template<class T>
Vector2<T> Vector2<T>::setDir(float angle)
{
float M = getLength();
return Vector2<T>((float)(M* cos(angle)), (float)(M* sin(angle)));
}
template<class T>
float Vector2<T>::getAngle()
{
return (float)atan2(y, x);
}
template<class T>
float Vector2<T>::crossProduct(Vector2<T> A)
{
return (x * A.y) - (y * A.x);
}
template class Vector2<int>;
template class Vector2<float>;
template class Vector2<unsigned int>;