AI_TVector2.h

Go to the documentation of this file.
00001 #ifndef AI__VECTOR2_H
00002 #define AI__VECTOR2_H
00003 //------------------------------------------------------------------------------
00012 #include "AI_Math.h"
00013 #include <float.h>
00014 
00015 class AI_TVector2 {
00016 public:
00018     AI_TVector2();
00020     AI_TVector2(const float _x, const float _y);
00022     AI_TVector2(const AI_TVector2& vec);
00024     AI_TVector2(const float* p);
00026     void set(const float _x, const float _y);
00028     void set(const AI_TVector2& vec);
00030     void set(const float* p);
00032     float len() const;
00034     void norm();
00036     void operator+=(const AI_TVector2& v0);
00038     void operator-=(const AI_TVector2& v0);
00040     void operator*=(const float s);
00042     void operator/=(const float s);
00044     bool isequal(const AI_TVector2& v, const float tol) const;
00046     int compare(const AI_TVector2& v, float tol) const;
00048     void rotate(float angle);
00050     void lerp(const AI_TVector2& v0, float lerpVal);
00052     void lerp(const AI_TVector2& v0, const AI_TVector2& v1, float lerpVal);
00054     float dot(const AI_TVector2& v0) const;
00055 
00056     float x, y;
00057 };
00058 
00059 //------------------------------------------------------------------------------
00062 inline
00063 AI_TVector2::AI_TVector2() :
00064     x(0.0f),
00065     y(0.0f)
00066 {
00067     // empty
00068 }
00069 
00070 //------------------------------------------------------------------------------
00073 inline
00074 AI_TVector2::AI_TVector2(const float _x, const float _y) :
00075     x(_x),
00076     y(_y)
00077 {
00078     // empty
00079 }
00080 
00081 //------------------------------------------------------------------------------
00084 inline
00085 AI_TVector2::AI_TVector2(const AI_TVector2& vec) :
00086     x(vec.x),
00087     y(vec.y)
00088 {
00089     // empty
00090 }
00091 
00092 //------------------------------------------------------------------------------
00095 inline
00096 AI_TVector2::AI_TVector2(const float* p) :
00097     x(p[0]),
00098     y(p[1])
00099 {
00100     // empty
00101 }
00102 
00103 //------------------------------------------------------------------------------
00106 inline
00107 void
00108 AI_TVector2::set(const float _x, const float _y)
00109 {
00110     x = _x;
00111     y = _y;
00112 }
00113 
00114 //------------------------------------------------------------------------------
00117 inline
00118 void
00119 AI_TVector2::set(const AI_TVector2& v)
00120 {
00121     x = v.x;
00122     y = v.y;
00123 }
00124 
00125 //------------------------------------------------------------------------------
00128 inline
00129 void
00130 AI_TVector2::set(const float* p)
00131 {
00132     x = p[0];
00133     y = p[1];
00134 }
00135 
00136 //------------------------------------------------------------------------------
00139 inline
00140 float
00141 AI_TVector2::len() const
00142 {
00143     return (float) sqrt(x * x + y * y);
00144 }
00145 
00146 //------------------------------------------------------------------------------
00149 inline
00150 void
00151 AI_TVector2::norm()
00152 {
00153     float l = len();
00154     if (l > AI_TINY)
00155     {
00156         x /= l;
00157         y /= l;
00158     }
00159 }
00160 
00161 //------------------------------------------------------------------------------
00164 inline
00165 void
00166 AI_TVector2::operator +=(const AI_TVector2& v0)
00167 {
00168     x += v0.x;
00169     y += v0.y;
00170 }
00171 
00172 //------------------------------------------------------------------------------
00175 inline
00176 void
00177 AI_TVector2::operator -=(const AI_TVector2& v0)
00178 {
00179     x -= v0.x;
00180     y -= v0.y;
00181 }
00182 
00183 //------------------------------------------------------------------------------
00186 inline
00187 void
00188 AI_TVector2::operator *=(const float s)
00189 {
00190     x *= s;
00191     y *= s;
00192 }
00193 
00194 //------------------------------------------------------------------------------
00197 inline
00198 void
00199 AI_TVector2::operator /=(const float s)
00200 {
00201     x /= s;
00202     y /= s;
00203 }
00204 
00205 //------------------------------------------------------------------------------
00208 inline
00209 bool
00210 AI_TVector2::isequal(const AI_TVector2& v, const float tol) const
00211 {
00212     if (fabs(v.x - x) > tol)      return false;
00213     else if (fabs(v.y - y) > tol) return false;
00214     return true;
00215 }
00216 
00217 //------------------------------------------------------------------------------
00220 inline
00221 int
00222 AI_TVector2::compare(const AI_TVector2& v, float tol) const
00223 {
00224     if (fabs(v.x - x) > tol)      return (v.x > x) ? +1 : -1;
00225     else if (fabs(v.y - y) > tol) return (v.y > y) ? +1 : -1;
00226     else                          return 0;
00227 }
00228 
00229 //------------------------------------------------------------------------------
00232 inline
00233 void
00234 AI_TVector2::rotate(float angle)
00235 {
00236     // rotates this one around P(0,0).
00237     float sa, ca;
00238 
00239     sa = (float) sin(angle);
00240     ca = (float) cos(angle);
00241 
00242     // "handmade" multiplication
00243     AI_TVector2 help(ca * this->x - sa * this->y,
00244                   sa * this->x + ca * this->y);
00245 
00246     *this = help;
00247 }
00248 
00249 //------------------------------------------------------------------------------
00252 static
00253 inline
00254 AI_TVector2 operator +(const AI_TVector2& v0, const AI_TVector2& v1)
00255 {
00256     return AI_TVector2(v0.x + v1.x, v0.y + v1.y);
00257 }
00258 
00259 //------------------------------------------------------------------------------
00262 static
00263 inline
00264 AI_TVector2 operator -(const AI_TVector2& v0, const AI_TVector2& v1)
00265 {
00266     return AI_TVector2(v0.x - v1.x, v0.y - v1.y);
00267 }
00268 
00269 //------------------------------------------------------------------------------
00273 static
00274 inline
00275 float operator %(const AI_TVector2& v0, const AI_TVector2& v1)
00276 {
00277     return v0.x * v1.x + v0.y * v1.y;
00278 }
00279 
00280 
00281 //------------------------------------------------------------------------------
00284 static
00285 inline
00286 AI_TVector2 operator *(const AI_TVector2& v0, const float s)
00287 {
00288     return AI_TVector2(v0.x * s, v0.y * s);
00289 }
00290 
00291 //------------------------------------------------------------------------------
00294 static
00295 inline
00296 AI_TVector2 operator -(const AI_TVector2& v)
00297 {
00298     return AI_TVector2(-v.x, -v.y);
00299 }
00300 
00301 //------------------------------------------------------------------------------
00304 inline
00305 void
00306 AI_TVector2::lerp(const AI_TVector2& v0, float lerpVal)
00307 {
00308     x = v0.x + ((x - v0.x) * lerpVal);
00309     y = v0.y + ((y - v0.y) * lerpVal);
00310 }
00311 
00312 //------------------------------------------------------------------------------
00315 inline
00316 void
00317 AI_TVector2::lerp(const AI_TVector2& v0, const AI_TVector2& v1, float lerpVal)
00318 {
00319     x = v0.x + ((v1.x - v0.x) * lerpVal);
00320     y = v0.y + ((v1.y - v0.y) * lerpVal);
00321 }
00322 
00323 //------------------------------------------------------------------------------
00327 inline
00328 float
00329 AI_TVector2::dot(const AI_TVector2& v0) const
00330 {
00331     return ( x * v0.x + y * v0.y );
00332 }
00333 
00334 //------------------------------------------------------------------------------
00335 #endif
00336