UGDK  0.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
vector2D.h
Go to the documentation of this file.
1 
2 #ifndef UGDK_MATH_VECTOR2D_H_
3 #define UGDK_MATH_VECTOR2D_H_
4 
5 #include <ugdk/base/types.h>
6 
7 #ifdef SWIG
8 #pragma SWIG nowarn=312
9 #endif
10 
11 namespace ugdk {
12 
13 namespace math {
14 class Integer2D;
15 } // namespace math
16 
17 // 2 dimension vectors, using doubles.
18 class Vector2D {
19  public:
21 
23  Vector2D() : x(0.0), y(0.0) {}
24 
26 
29  explicit Vector2D(double value) : x(value), y(value) {}
30 
32 
36  Vector2D(double _x, double _y) : x(_x), y(_y) {}
37 
39  Vector2D(const ugdk::math::Integer2D& int2d);
40 
41  ~Vector2D() { }
42 
43  // Fields are accessible as x/y or val[0] and val[1].
44  union {
45  struct { double x, y; };
46  struct { double val[2]; };
47  };
48 
49  double get_x() const { return x; }
50  double get_y() const { return y; }
51  void set_x(double x_) { x = x_; }
52  void set_y(double y_) { y = y_; }
53 
54 
56 
60  double NormOne() const;
61 
63 
68  double Length() const;
69 
71 
76  double LengthSquared() const { return (x*x) + (y*y); }
77 
79 
82  double Angle() const;
83 
85 
89  Vector2D Normalize() const;
90 
92 
96  Vector2D Rotate(const double angle) const;
97 
99 
103 
105 
109 
111 
114  Vector2D Scale(const Vector2D &scale) const;
115 
116  // Static methods
117 
119 
122  static Vector2D Add(const Vector2D &a, const Vector2D &b) {
123  return Vector2D(a.x + b.x, a.y + b.y);
124  }
125 
127 
130  static Vector2D Subtract(const Vector2D &a, const Vector2D &b) {
131  return Vector2D(a.x - b.x, a.y - b.y);
132  }
133 
135 
138  static Vector2D Multiply(const Vector2D &a, const double &scalar) {
139  return Vector2D(a.x * scalar, a.y * scalar);
140  }
141 
143 
146  static double InnerProduct(const Vector2D &a, const Vector2D &b) {
147  return (a.x * b.x) + (a.y * b.y);
148  }
149 
151 
155  double length() const { return Length(); }
156 
158 
162  double angle() const { return Angle(); }
163 
165 
169  static Vector2D Normalized(Vector2D &a) { return a.Normalize(); }
170 
172 
176  static Vector2D Rotate(Vector2D &a, double angle) {
177  return a.Rotate(angle);
178  }
179 
180 
182 
185  bool operator==(const Vector2D &rhs) {
186  return x==rhs.x && y==rhs.y;
187  }
188 
189  // TODO document and revise
190  Vector2D& operator+=(const Vector2D &other);
191 
192  // TODO document and revise
193  Vector2D& operator-=(const Vector2D &other);
194 
196 
199  Vector2D operator+(const Vector2D &right) const;
200 
202 
205  // TODO revise
206  Vector2D operator-() const;
207 
209 
212  Vector2D operator-(const Vector2D &right) const;
213 
214 
216 
219  Vector2D operator*(const double &scalar) const;
220 
222 
225  Vector2D operator/(const double &scalar) const;
226 
228 
231  double operator*(const Vector2D &right) const;
232 };
233 
235 
238 Vector2D operator*(const double &scalar, const Vector2D &right);
239 
240 } // namespace ugdk
241 
242 #endif // UGDK_MATH_VECTOR2D_H_