UGDK  0.5.0
 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/structure/types.h>
6 #include <ugdk/math.h>
7 #include <array>
8 #include <tuple>
9 
10 #ifdef SWIG
11 #pragma SWIG nowarn=312
12 #endif
13 
14 namespace ugdk {
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 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(double angle) const;
97 
99 
103 
105 
109 
111 
114  Vector2D Scale(const Vector2D& scale) const;
115 
117 
120  void Divide(const Vector2D& divider);
121 
123 
126  Vector2D Divided(const Vector2D& divider) const;
127 
128  // Static methods
129 
131 
134  static Vector2D Add(const Vector2D& a, const Vector2D& b) {
135  return Vector2D(a.x + b.x, a.y + b.y);
136  }
137 
139 
142  static Vector2D Subtract(const Vector2D& a, const Vector2D& b) {
143  return Vector2D(a.x - b.x, a.y - b.y);
144  }
145 
147 
150  static Vector2D Multiply(const Vector2D& a, double scalar) {
151  return Vector2D(a.x * scalar, a.y * scalar);
152  }
153 
155 
158  static double InnerProduct(const Vector2D& a, const Vector2D& b) {
159  return (a.x * b.x) + (a.y * b.y);
160  }
161 
163 
167  double length() const { return Length(); }
168 
170 
174  double angle() const { return Angle(); }
175 
177 
181  static Vector2D Normalized(Vector2D& a) { return a.Normalize(); }
182 
184 
188  static Vector2D Rotate(Vector2D& a, double angle) {
189  return a.Rotate(angle);
190  }
191 
192 
194 
197  bool operator==(const Vector2D& rhs) {
198  return x==rhs.x && y==rhs.y;
199  }
200 
201  // TODO document and revise
202  Vector2D& operator+=(const Vector2D &other);
203 
204  // TODO document and revise
205  Vector2D& operator-=(const Vector2D &other);
206 
207  // TODO document
208  Vector2D& operator*=(double other);
209 
211 
214  Vector2D operator+(const Vector2D& right) const;
215 
217 
220  // TODO revise
221  Vector2D operator-() const;
222 
224 
227  Vector2D operator-(const Vector2D& right) const;
228 
229 
231 
234  Vector2D operator*(double scalar) const;
235 
237 
240  Vector2D operator/(double scalar) const;
241 
243 
246  double operator*(const Vector2D &right) const;
247 
248  operator std::array<double, 2> () const {
249  std::array<double, 2> result {{ x,y }};
250  return result;
251  }
252 
253  operator std::pair<double, double> () const {
254  return std::make_pair(x, y);
255  }
256 };
257 
259 
262 Vector2D operator*(double scalar, const Vector2D &right);
263 
264 } // namespace math
265 } // namespace ugdk
266 
267 #endif // UGDK_MATH_VECTOR2D_H_
Vector2D & operator*=(double other)
Integer2D operator*(int scalar, const Integer2D &right)
Method that returns a integer equal to the a integer multiplied by a scalar.
Vector2D operator+(const Vector2D &right) const
Method that returns a vector equal to the sum of two others.
static Vector2D Rotate(Vector2D &a, double angle)
Deprecated. Use the method function Rotate.
Definition: vector2D.h:188
Vector2D Mirrored(const ugdk::enums::mirroraxis::MirrorAxis axis) const
Returns a new Vector2D, mirrored by the "axis" axis. Include types.h and use "using namespace ugdk::e...
Vector2D & operator+=(const Vector2D &other)
double Angle() const
Returns the angle (in radians) of this vector.
Definition: vector2D.h:18
void Mirror(const ugdk::enums::mirroraxis::MirrorAxis axis)
Mirrors this Vector2D (in-place) by the "axis" axis. Include types.h and use "using namespace ugdk::e...
Vector2D operator-() const
Method that returns a vector equal to the oposite of another.
double x
Definition: vector2D.h:45
Definition: integer2D.h:20
double angle() const
Deprecated. Use the upper case method.
Definition: vector2D.h:174
static double InnerProduct(const Vector2D &a, const Vector2D &b)
Static method that returns a scalar equal to the inner product of two vectors.
Definition: vector2D.h:158
Vector2D Scale(const Vector2D &scale) const
Returns a new vector which is this vector scaled coordinate by coordinate with "scale".
double length() const
Deprecated. Use the upper case method.
Definition: vector2D.h:167
Vector2D Rotate(double angle) const
Returns a new vector equal to this vector rotated by "angle" (in radians) counter-clockwise.
Definition: animation.h:11
Vector2D & operator-=(const Vector2D &other)
void Divide(const Vector2D &divider)
In-place integer division, coordinate by coordinate.
Vector2D()
Initializes both fields with 0.0.
Definition: vector2D.h:23
Vector2D Divided(const Vector2D &divider) const
Returns a new Integer2D equal to this divided coordinate by coordinate by "multiplier".
void set_y(double y_)
Definition: vector2D.h:52
Vector2D Normalize() const
Returns a new vector equal to this vector normalized.
double get_x() const
Definition: vector2D.h:49
double NormOne() const
Returns the norm-1 of this vector.
static Vector2D Subtract(const Vector2D &a, const Vector2D &b)
Static method that returns a vector equal to the subtraction of two others.
Definition: vector2D.h:142
static Vector2D Add(const Vector2D &a, const Vector2D &b)
Static method that returns a vector equal to the sum of two others.
Definition: vector2D.h:134
double y
Definition: vector2D.h:45
double Length() const
Returns the norm-2 of this vector.
static Vector2D Normalized(Vector2D &a)
Deprecated. Use the method function Normalize.
Definition: vector2D.h:181
Vector2D operator*(double scalar) const
Method that returns a vector equal to the a vector multiplied by a scalar.
void set_x(double x_)
Definition: vector2D.h:51
~Vector2D()
Definition: vector2D.h:41
Vector2D(double value)
Initializes both fields with val.
Definition: vector2D.h:29
double LengthSquared() const
Returns the norm-2 squared.
Definition: vector2D.h:76
Vector2D(double _x, double _y)
Initializes both fields with given values.
Definition: vector2D.h:36
static Vector2D Multiply(const Vector2D &a, double scalar)
Static method that returns a vector equal to the a vector multiplied by a scalar. ...
Definition: vector2D.h:150
double get_y() const
Definition: vector2D.h:50
bool operator==(const Vector2D &rhs)
Compares this vector to the given vector, return True if they are equivalent.
Definition: vector2D.h:197
MirrorAxis
Definition: types.h:29
Vector2D operator/(double scalar) const
Method that returns a vector equal to the a vector multiplied by the inverse of a scalar...
double val[2]
Definition: vector2D.h:46