Neo  0.5.0
Developer Documentation
Vector2.h
Go to the documentation of this file.
1 //========================================================================
2 // Copyright (c) 2003-2011 Anael Seghezzi <www.maratis3d.com>
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 //
8 // Permission is granted to anyone to use this software for any purpose,
9 // including commercial applications, and to alter it and redistribute it
10 // freely, subject to the following restrictions:
11 //
12 // 1. The origin of this software must not be misrepresented; you must not
13 // claim that you wrote the original software. If you use this software
14 // in a product, an acknowledgment in the product documentation would
15 // be appreciated but is not required.
16 //
17 // 2. Altered source versions must be plainly marked as such, and must not
18 // be misrepresented as being the original software.
19 //
20 // 3. This notice may not be removed or altered from any source
21 // distribution.
22 //
23 //========================================================================
24 
25 
26 #ifndef __VECTOR2_H
27 #define __VECTOR2_H
28 
29 namespace Neo
30 {
31 class NEO_CORE_EXPORT Vector2
32 {
33 public:
34 
35  float x;
36  float y;
37 
38 public:
39 
40  Vector2(void):
41  x(0.0f),
42  y(0.0f)
43  {}
44 
45  Vector2(float newX, float newY):
46  x(newX),
47  y(newY)
48  {}
49 
50  Vector2(const float value):
51  x(value),
52  y(value)
53  {}
54 
55  Vector2(const float * values):
56  x((*values)),
57  y((*values)+1)
58  {}
59 
60  Vector2(const Vector2 & vec):
61  x(vec.x),
62  y(vec.y)
63  {}
64 
65  Vector2(const Vector3 & vec);
66 
67 public:
68 
69  Vector2 add(const Vector2 & vec) const
70  {
71  return Vector2(x + vec.x, y + vec.y);
72  }
73 
74  Vector2 substract(const Vector2 & vec) const
75  {
76  return Vector2(x - vec.x, y - vec.y);
77  }
78 
79  Vector2 multiply(const float value) const
80  {
81  return Vector2(x * value, y * value);
82  }
83 
84  Vector2 divide(const float value) const
85  {
86  if (value == 0.0f) return Vector2(0.0f, 0.0f);
87  return Vector2(x / value, y / value);
88  }
89 
90  bool equals(const Vector2 & vec) const
91  {
92  return !((*this) == vec);
93  }
94 
95  inline Vector2 operator + (const float value) const
96  {
97  return Vector2(x + value, y + value);
98  }
99 
100  inline Vector2 operator - (const float value) const
101  {
102  return Vector2(x - value, y - value);
103  }
104 
105  inline Vector2 operator + (const Vector2 & vec) const
106  {
107  return Vector2(x + vec.x, y + vec.y);
108  }
109 
110  inline Vector2 operator - (const Vector2 & vec) const
111  {
112  return Vector2(x - vec.x, y - vec.y);
113  }
114 
115  inline Vector2 operator * (const float value) const
116  {
117  return Vector2(x * value, y * value);
118  }
119 
120  inline Vector2 operator * (const Vector2 & vec) const
121  {
122  return Vector2(x * vec.x, y * vec.y);
123  }
124 
125  inline Vector2 operator / (const Vector2 & vec) const
126  {
127  return Vector2(x / vec.x, y / vec.y);
128  }
129 
130  inline Vector2 operator / (const float value) const
131  {
132  if(value == 0.0f) return Vector2(0.0f, 0.0f);
133  return Vector2(x / value, y / value);
134  }
135 
136  friend inline Vector2 operator * (float factor, const Vector2 & vec)
137  {
138  return vec * factor;
139  }
140 
141  inline bool operator == (const Vector2 & vec) const
142  {
143  if(x == vec.x && y == vec.y){
144  return true;
145  }
146 
147  return false;
148  }
149 
150  inline void operator = (const float value)
151  {
152  x = value;
153  y = value;
154  }
155 
156  inline bool operator != (const Vector2 & vec) const
157  {
158  return !((*this) == vec);
159  }
160 
161  inline void operator += (const Vector2 & vec)
162  {
163  x += vec.x;
164  y += vec.y;
165  }
166 
167  inline void operator -= (const Vector2 & vec)
168  {
169  x -= vec.x;
170  y -= vec.y;
171  }
172 
173  inline void operator *= (const Vector2 & vec)
174  {
175  x *= vec.x;
176  y *= vec.y;
177  }
178 
179  inline void operator /= (const Vector2 & vec)
180  {
181  x /= vec.x;
182  y /= vec.y;
183  }
184 
185  inline void operator *= (const float value)
186  {
187  x *= value;
188  y *= value;
189  }
190 
191  inline void operator /= (const float value)
192  {
193  if(value == 0.0f){
194  x = 0.0f;
195  y = 0.0f;
196  }
197  else
198  {
199  x /= value;
200  y /= value;
201  }
202  }
203 
204  inline Vector2 operator - (void) const
205  {
206  return Vector2(-x, -y);
207  }
208 
209  inline Vector2 operator + (void) const
210  {
211  return *this;
212  }
213 
214  inline operator float* () const
215  {
216  return (float*) this;
217  }
218 
219  inline operator const float* () const
220  {
221  return (const float*) this;
222  }
223 
224 public:
225 
226  inline void set(float newX, float newY)
227  {
228  x = newX;
229  y = newY;
230  }
231 
232  inline void loadIdentity(void)
233  {
234  x = 0.0f;
235  y = 0.0f;
236  }
237 
238  void normalize();
239  void rotate(const float angle);
240 
241  Vector2 getNormalized() const;
242  Vector2 getRotated(const float angle) const;
243 
244  // return angle that vec needs to rotate
245  inline float getAngleBetween(const Vector2 & vec)
246  {
247  float angle = (float)((atan2(y, x) - atan2(vec.y, vec.x))*RAD_TO_DEG);
248 
249  if(angle > 180)
250  angle -= 360;
251  if(angle < -180)
252  angle += 360;
253 
254  return angle;
255  }
256 
257  inline float getLength() const
258  {
259  return sqrtf((x*x) + (y*y));
260  }
261 
262  inline float getSquaredLength() const
263  {
264  return ((x*x) + (y*y));
265  }
266 
267  inline float dotProduct(const Vector2 & vec) const
268  {
269  return (x*vec.x) + (y*vec.y);
270  }
271 
272  inline Vector2 lerp(const Vector2 & vec, float factor) const
273  {
274  return ((*this) * (1.0f - factor)) + (vec * factor);
275  }
276 };
277 }
278 
279 #endif
Vector2 add(const Vector2 &vec) const
Definition: Vector2.h:69
void set(float newX, float newY)
Definition: Vector2.h:226
Definition: Vector3.h:31
Vector2 divide(const float value) const
Definition: Vector2.h:84
Vector2 lerp(const Vector2 &vec, float factor) const
Definition: Vector2.h:272
float y
Definition: Vector2.h:36
Vector2(void)
Definition: Vector2.h:40
void loadIdentity(void)
Definition: Vector2.h:232
float x
Definition: Vector2.h:35
Vector2(float newX, float newY)
Definition: Vector2.h:45
bool equals(const Vector2 &vec) const
Definition: Vector2.h:90
float getLength() const
Definition: Vector2.h:257
Vector2(const Vector2 &vec)
Definition: Vector2.h:60
Vector2(const float *values)
Definition: Vector2.h:55
float dotProduct(const Vector2 &vec) const
Definition: Vector2.h:267
Definition: Vector2.h:31
float getSquaredLength() const
Definition: Vector2.h:262
Vector2(const float value)
Definition: Vector2.h:50
float getAngleBetween(const Vector2 &vec)
Definition: Vector2.h:245
Definition: Color.h:29
Vector2 substract(const Vector2 &vec) const
Definition: Vector2.h:74
Vector2 multiply(const float value) const
Definition: Vector2.h:79