Home | Namespaces | Hierarchy | Alphabetical List | Class list | Files | Namespace Members | Class members | File members | Tutorials
vector2d.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4 
5 #ifndef __IRR_POINT_2D_H_INCLUDED__
6 #define __IRR_POINT_2D_H_INCLUDED__
7 
8 #include "irrMath.h"
9 #include "dimension2d.h"
10 
11 namespace irr
12 {
13 namespace core
14 {
15 
16 
18 
20 template <class T>
21 class vector2d
22 {
23 public:
25  vector2d() : X(0), Y(0) {}
27  vector2d(T nx, T ny) : X(nx), Y(ny) {}
29  explicit vector2d(T n) : X(n), Y(n) {}
31  vector2d(const vector2d<T>& other) : X(other.X), Y(other.Y) {}
32 
33  vector2d(const dimension2d<T>& other) : X(other.Width), Y(other.Height) {}
34 
35  // operators
36 
37  vector2d<T> operator-() const { return vector2d<T>(-X, -Y); }
38 
39  vector2d<T>& operator=(const vector2d<T>& other) { X = other.X; Y = other.Y; return *this; }
40 
41  vector2d<T>& operator=(const dimension2d<T>& other) { X = other.Width; Y = other.Height; return *this; }
42 
43  vector2d<T> operator+(const vector2d<T>& other) const { return vector2d<T>(X + other.X, Y + other.Y); }
44  vector2d<T> operator+(const dimension2d<T>& other) const { return vector2d<T>(X + other.Width, Y + other.Height); }
45  vector2d<T>& operator+=(const vector2d<T>& other) { X+=other.X; Y+=other.Y; return *this; }
46  vector2d<T> operator+(const T v) const { return vector2d<T>(X + v, Y + v); }
47  vector2d<T>& operator+=(const T v) { X+=v; Y+=v; return *this; }
48  vector2d<T>& operator+=(const dimension2d<T>& other) { X += other.Width; Y += other.Height; return *this; }
49 
50  vector2d<T> operator-(const vector2d<T>& other) const { return vector2d<T>(X - other.X, Y - other.Y); }
51  vector2d<T> operator-(const dimension2d<T>& other) const { return vector2d<T>(X - other.Width, Y - other.Height); }
52  vector2d<T>& operator-=(const vector2d<T>& other) { X-=other.X; Y-=other.Y; return *this; }
53  vector2d<T> operator-(const T v) const { return vector2d<T>(X - v, Y - v); }
54  vector2d<T>& operator-=(const T v) { X-=v; Y-=v; return *this; }
55  vector2d<T>& operator-=(const dimension2d<T>& other) { X -= other.Width; Y -= other.Height; return *this; }
56 
57  vector2d<T> operator*(const vector2d<T>& other) const { return vector2d<T>(X * other.X, Y * other.Y); }
58  vector2d<T>& operator*=(const vector2d<T>& other) { X*=other.X; Y*=other.Y; return *this; }
59  vector2d<T> operator*(const T v) const { return vector2d<T>(X * v, Y * v); }
60  vector2d<T>& operator*=(const T v) { X*=v; Y*=v; return *this; }
61 
62  vector2d<T> operator/(const vector2d<T>& other) const { return vector2d<T>(X / other.X, Y / other.Y); }
63  vector2d<T>& operator/=(const vector2d<T>& other) { X/=other.X; Y/=other.Y; return *this; }
64  vector2d<T> operator/(const T v) const { return vector2d<T>(X / v, Y / v); }
65  vector2d<T>& operator/=(const T v) { X/=v; Y/=v; return *this; }
66 
68  bool operator<=(const vector2d<T>&other) const
69  {
70  return (X<other.X || core::equals(X, other.X)) ||
71  (core::equals(X, other.X) && (Y<other.Y || core::equals(Y, other.Y)));
72  }
73 
75  bool operator>=(const vector2d<T>&other) const
76  {
77  return (X>other.X || core::equals(X, other.X)) ||
78  (core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y)));
79  }
80 
82  bool operator<(const vector2d<T>&other) const
83  {
84  return (X<other.X && !core::equals(X, other.X)) ||
85  (core::equals(X, other.X) && Y<other.Y && !core::equals(Y, other.Y));
86  }
87 
89  bool operator>(const vector2d<T>&other) const
90  {
91  return (X>other.X && !core::equals(X, other.X)) ||
92  (core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y));
93  }
94 
95  bool operator==(const vector2d<T>& other) const { return equals(other); }
96  bool operator!=(const vector2d<T>& other) const { return !equals(other); }
97 
98  // functions
99 
101 
104  bool equals(const vector2d<T>& other) const
105  {
106  return core::equals(X, other.X) && core::equals(Y, other.Y);
107  }
108 
109  vector2d<T>& set(T nx, T ny) {X=nx; Y=ny; return *this; }
110  vector2d<T>& set(const vector2d<T>& p) { X=p.X; Y=p.Y; return *this; }
111 
113 
114  T getLength() const { return core::squareroot( X*X + Y*Y ); }
115 
117 
119  T getLengthSQ() const { return X*X + Y*Y; }
120 
122 
124  T dotProduct(const vector2d<T>& other) const
125  {
126  return X*other.X + Y*other.Y;
127  }
128 
130 
133  T getDistanceFrom(const vector2d<T>& other) const
134  {
135  return vector2d<T>(X - other.X, Y - other.Y).getLength();
136  }
137 
139 
142  T getDistanceFromSQ(const vector2d<T>& other) const
143  {
144  return vector2d<T>(X - other.X, Y - other.Y).getLengthSQ();
145  }
146 
148 
151  vector2d<T>& rotateBy(f64 degrees, const vector2d<T>& center=vector2d<T>())
152  {
153  degrees *= DEGTORAD64;
154  const f64 cs = cos(degrees);
155  const f64 sn = sin(degrees);
156 
157  X -= center.X;
158  Y -= center.Y;
159 
160  set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs));
161 
162  X += center.X;
163  Y += center.Y;
164  return *this;
165  }
166 
168 
171  {
172  f32 length = (f32)(X*X + Y*Y);
173  if ( length == 0 )
174  return *this;
175  length = core::reciprocal_squareroot ( length );
176  X = (T)(X * length);
177  Y = (T)(Y * length);
178  return *this;
179  }
180 
182 
186  {
187  if (Y == 0)
188  return X < 0 ? 180 : 0;
189  else
190  if (X == 0)
191  return Y < 0 ? 270 : 90;
192 
193  if ( Y > 0)
194  if (X > 0)
195  return atan((irr::f64)Y/(irr::f64)X) * RADTODEG64;
196  else
197  return 180.0-atan((irr::f64)Y/-(irr::f64)X) * RADTODEG64;
198  else
199  if (X > 0)
200  return 360.0-atan(-(irr::f64)Y/(irr::f64)X) * RADTODEG64;
201  else
202  return 180.0+atan(-(irr::f64)Y/-(irr::f64)X) * RADTODEG64;
203  }
204 
206 
208  inline f64 getAngle() const
209  {
210  if (Y == 0) // corrected thanks to a suggestion by Jox
211  return X < 0 ? 180 : 0;
212  else if (X == 0)
213  return Y < 0 ? 90 : 270;
214 
215  // don't use getLength here to avoid precision loss with s32 vectors
216  // avoid floating-point trouble as sqrt(y*y) is occasionally larger than y, so clamp
217  const f64 tmp = core::clamp(Y / sqrt((f64)(X*X + Y*Y)), -1.0, 1.0);
218  const f64 angle = atan( core::squareroot(1 - tmp*tmp) / tmp) * RADTODEG64;
219 
220  if (X>0 && Y>0)
221  return angle + 270;
222  else
223  if (X>0 && Y<0)
224  return angle + 90;
225  else
226  if (X<0 && Y<0)
227  return 90 - angle;
228  else
229  if (X<0 && Y>0)
230  return 270 - angle;
231 
232  return angle;
233  }
234 
236 
238  inline f64 getAngleWith(const vector2d<T>& b) const
239  {
240  f64 tmp = (f64)(X*b.X + Y*b.Y);
241 
242  if (tmp == 0.0)
243  return 90.0;
244 
245  tmp = tmp / core::squareroot((f64)((X*X + Y*Y) * (b.X*b.X + b.Y*b.Y)));
246  if (tmp < 0.0)
247  tmp = -tmp;
248  if ( tmp > 1.0 ) // avoid floating-point trouble
249  tmp = 1.0;
250 
251  return atan(sqrt(1 - tmp*tmp) / tmp) * RADTODEG64;
252  }
253 
255 
259  bool isBetweenPoints(const vector2d<T>& begin, const vector2d<T>& end) const
260  {
261  if (begin.X != end.X)
262  {
263  return ((begin.X <= X && X <= end.X) ||
264  (begin.X >= X && X >= end.X));
265  }
266  else
267  {
268  return ((begin.Y <= Y && Y <= end.Y) ||
269  (begin.Y >= Y && Y >= end.Y));
270  }
271  }
272 
274 
279  {
280  f64 inv = 1.0f - d;
281  return vector2d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d));
282  }
283 
285 
291  {
292  // this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;
293  const f64 inv = 1.0f - d;
294  const f64 mul0 = inv * inv;
295  const f64 mul1 = 2.0f * d * inv;
296  const f64 mul2 = d * d;
297 
298  return vector2d<T> ( (T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
299  (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2));
300  }
301 
303 
309  {
310  X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));
311  Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));
312  return *this;
313  }
314 
316  T X;
317 
319  T Y;
320 };
321 
324 
327 
328  template<class S, class T>
329  vector2d<T> operator*(const S scalar, const vector2d<T>& vector) { return vector*scalar; }
330 
331  // These methods are declared in dimension2d, but need definitions of vector2d
332  template<class T>
333  dimension2d<T>::dimension2d(const vector2d<T>& other) : Width(other.X), Height(other.Y) { }
334 
335  template<class T>
336  bool dimension2d<T>::operator==(const vector2d<T>& other) const { return Width == other.X && Height == other.Y; }
337 
338 } // end namespace core
339 } // end namespace irr
340 
341 #endif
342 

The Irrlicht Engine
The Irrlicht Engine Documentation © 2003-2010 by Nikolaus Gebhardt. Generated on Mon May 6 2013 17:41:02 by Doxygen (1.8.3.1)