-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVector2D.cpp
More file actions
executable file
·255 lines (211 loc) · 5.25 KB
/
Vector2D.cpp
File metadata and controls
executable file
·255 lines (211 loc) · 5.25 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/******************************************************************************
*
* COPYRIGHT Vinícius G. Mendonça ALL RIGHTS RESERVED.
*
* This software cannot be copied, stored, distributed without
* Vinícius G.Mendonça prior authorization.
*
* This file was made available on http://www.pontov.com.br and it is free
* to be restributed or used under Creative Commons license 2.5 br:
* http://creativecommons.org/licenses/by-sa/2.5/br/
*
*******************************************************************************
* Este software nao pode ser copiado, armazenado, distribuido sem autorização
* a priori de Vinícius G. Mendonça
*
* Este arquivo foi disponibilizado no site http://www.pontov.com.br e esta
* livre para distribuição seguindo a licença Creative Commons 2.5 br:
* http://creativecommons.org/licenses/by-sa/2.5/br/
*
******************************************************************************/
#include "Vector2D.h"
#include <stdexcept>
#include <sstream>
using namespace math;
Vector2D::Vector2D() : x(0), y(0)
{
}
Vector2D::Vector2D(float _x, float _y) : x(_x), y(_y)
{
}
Vector2D::Vector2D(float xy[2]) : x(xy[0]), y(xy[1])
{
}
Vector2D Vector2D::createBySizeAngle(float size, float angle)
{
return Vector2D(cos(angle) * size,
sin(angle) * size);
}
Vector2D& Vector2D::set(float _x, float _y)
{
x = _x;
y = _y;
return *this;
}
Vector2D& Vector2D::set(const float xy[2])
{
x = xy[0];
y = xy[1];
return *this;
}
Vector2D& Vector2D::set(const Vector2D& other)
{
if (&other == this)
return *this;
x = other.x;
y = other.y;
return *this;
}
float Vector2D::getSizeSqr() const
{
return x * x + y * y;
}
float Vector2D::getSize() const
{
return sqrtf(getSizeSqr());
}
Vector2D& Vector2D::setSize(float _size)
{
//Avoid the size calculation for the zero vector
if (_size == 0)
return set(0,0);
//Calculate the new size.
return (*this *= (_size / getSize()));
}
Vector2D& Vector2D::truncate(float size)
{
if (getSizeSqr() > size * size)
setSize(size);
return *this;
}
float Vector2D::getAngle() const
{
return atan2f(y,x);
}
Vector2D& Vector2D::rotate(float angle)
{
float s = sin(angle);
float c = cos(angle);
float newX = x * c - y * s;
float newY = x * s + y * c;
x = newX;
y = newY;
return *this;
}
Vector2D& Vector2D::normalize()
{
return (*this /= getSize());
}
float Vector2D::operator[] (long index) const
{
if (index == 0)
return x;
if (index == 1)
return y;
std::stringstream ss;
ss << "Index out of bounds: " << index << " size: 2";
throw std::out_of_range(ss.str());
}
float& Vector2D::operator[] (long index)
{
if (index == 0)
return x;
if (index == 1)
return y;
std::stringstream ss;
ss << "Index out of bounds: " << index << " size: 2";
throw std::out_of_range(ss.str());
}
Vector2D& Vector2D::operator += (const Vector2D& other)
{
x += other.x;
y += other.y;
return *this;
}
Vector2D& Vector2D::operator -= (const Vector2D& other)
{
x -= other.x;
y -= other.y;
return *this;
}
Vector2D& Vector2D::operator *= (float c)
{
x *= c;
y *= c;
return *this;
}
Vector2D& Vector2D::operator /= (float c)
{
x /= c;
y /= c;
return *this;
}
Vector2D Vector2D::operator -(void) const
{
return Vector2D(-x, -y);
}
Vector2D Vector2D::operator +(const Vector2D& other) const
{
return Vector2D(x + other.x, y + other.y);
}
Vector2D Vector2D::operator -(const Vector2D& other) const
{
return Vector2D(x - other.x, y - other.y);
}
Vector2D Vector2D::operator *(float c) const
{
return Vector2D(x*c, y*c);
}
Vector2D math::operator*(float scalar, const Vector2D& vector)
{
return vector * scalar;
}
Vector2D Vector2D::operator /(float c) const
{
return Vector2D(x/c, y/c);
}
bool Vector2D::operator ==(const Vector2D& other) const
{
return x == other.x && y == other.y;
}
float Vector2D::dot(const Vector2D& other) const
{
return x * other.x + y * other.y;
}
/**
* Return the smallest angle between 2 vectors.
* The signal is used to indicate the angle rotation direction.
* Positive for counter-clockwise and negative for clockwise direction
*
* This method uses difference between two vectors.
*/
float Vector2D::angleBetween(const Vector2D other) const
{
float angle = other.getAngle() - getAngle();
if (fabs(angle) < PI)
return angle;
return static_cast<float>(angle + (angle < 0 ? 2*PI : -2*PI));
}
/**
* Return the smallest angle between 2 vectors.
* The signal is used to indicate the angle rotation direction.
* Positive for counter-clockwise and negative for clockwise direction
*
* This method uses the dot product.
*/
float Vector2D::angleSign(const Vector2D& v2)const
{
/** Must guarantee that v2 is a normalized vector */
Vector2D v(v2);
v.normalize();
float dp, angPi ;
dp = dot( v ); //dot product
if(dp >= 1.0) dp = 1.0f;
if(dp <=-1.0) dp =-1.0f;
angPi = (float)acos( dp );
//side test
if (y*v.x > x*v.y)
return -angPi;
else
return angPi;
}