UGDK
|
00001 00002 #ifndef UGDK_MATH_VECTOR2D_H_ 00003 #define UGDK_MATH_VECTOR2D_H_ 00004 00005 #include <ugdk/base/types.h> 00006 00007 #ifdef SWIG 00008 #pragma SWIG nowarn=312 00009 #endif 00010 00011 namespace ugdk { 00012 00013 namespace math { 00014 class Integer2D; 00015 } // namespace math 00016 00017 // 2 dimension vectors, using doubles. 00018 class Vector2D { 00019 public: 00021 00023 Vector2D() : x(0.0), y(0.0) {} 00024 00026 00029 explicit Vector2D(double value) : x(value), y(value) {} 00030 00032 00036 Vector2D(double _x, double _y) : x(_x), y(_y) {} 00037 00039 Vector2D(const ugdk::math::Integer2D& int2d); 00040 00041 ~Vector2D() { } 00042 00043 // Fields are accessible as x/y or val[0] and val[1]. 00044 union { 00045 struct { double x, y; }; 00046 struct { double val[2]; }; 00047 }; 00048 00049 double get_x() const { return x; } 00050 double get_y() const { return y; } 00051 void set_x(double x_) { x = x_; } 00052 void set_y(double y_) { y = y_; } 00053 00054 00056 00060 double NormOne() const; 00061 00063 00068 double Length() const; 00069 00071 00076 double LengthSquared() const { return (x*x) + (y*y); } 00077 00079 00082 double Angle() const; 00083 00085 00089 Vector2D Normalize() const; 00090 00092 00096 Vector2D Rotate(const double angle) const; 00097 00099 00102 void Mirror(const ugdk::enums::mirroraxis::MirrorAxis axis); 00103 00105 00108 Vector2D Mirrored(const ugdk::enums::mirroraxis::MirrorAxis axis) const; 00109 00111 00114 Vector2D Scale(const Vector2D &scale) const; 00115 00116 // Static methods 00117 00119 00122 static Vector2D Add(const Vector2D &a, const Vector2D &b) { 00123 return Vector2D(a.x + b.x, a.y + b.y); 00124 } 00125 00127 00130 static Vector2D Subtract(const Vector2D &a, const Vector2D &b) { 00131 return Vector2D(a.x - b.x, a.y - b.y); 00132 } 00133 00135 00138 static Vector2D Multiply(const Vector2D &a, const double &scalar) { 00139 return Vector2D(a.x * scalar, a.y * scalar); 00140 } 00141 00143 00146 static double InnerProduct(const Vector2D &a, const Vector2D &b) { 00147 return (a.x * b.x) + (a.y * b.y); 00148 } 00149 00151 00155 double length() const { return Length(); } 00156 00158 00162 double angle() const { return Angle(); } 00163 00165 00169 static Vector2D Normalized(Vector2D &a) { return a.Normalize(); } 00170 00172 00176 static Vector2D Rotate(Vector2D &a, double angle) { 00177 return a.Rotate(angle); 00178 } 00179 00180 00182 00185 bool operator==(const Vector2D &rhs) { 00186 return x==rhs.x && y==rhs.y; 00187 } 00188 00189 // TODO document and revise 00190 Vector2D& operator+=(const Vector2D &other); 00191 00192 // TODO document and revise 00193 Vector2D& operator-=(const Vector2D &other); 00194 00196 00199 Vector2D operator+(const Vector2D &right) const; 00200 00202 00205 // TODO revise 00206 Vector2D operator-() const; 00207 00209 00212 Vector2D operator-(const Vector2D &right) const; 00213 00214 00216 00219 Vector2D operator*(const double &scalar) const; 00220 00222 00225 Vector2D operator/(const double &scalar) const; 00226 00228 00231 double operator*(const Vector2D &right) const; 00232 }; 00233 00235 00238 Vector2D operator*(const double &scalar, const Vector2D &right); 00239 00240 } // namespace ugdk 00241 00242 #endif // UGDK_MATH_VECTOR2D_H_