00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 public:
00012
00013 union
00014 {
00015 SCALAR array[2];
00016 struct { SCALAR x, y; };
00017 struct { SCALAR u, v; };
00018 };
00019
00020 inline void set(SCALAR px, SCALAR py)
00021 {
00022 x = px;
00023 y = py;
00024 }
00025
00026 inline void setNull()
00027 {
00028 x = y = 0;
00029 }
00030
00031
00032
00033 SCALAR operator[](const long i)
00034 {
00035 assert(i >= 0 && i < 2);
00036 return(array[i]);
00037 }
00038
00039 SCALAR operator[](const unsigned long i)
00040 {
00041 assert(i < 2);
00042 return(array[i]);
00043 }
00044
00045 SCALAR operator[](const int i)
00046 {
00047 assert(i >= 0 && i < 2);
00048 return(array[i]);
00049 }
00050
00051 SCALAR operator[](const unsigned int i)
00052 {
00053 assert(i < 2);
00054 return(array[i]);
00055 }
00056
00057 SCALAR operator[](const short i)
00058 {
00059 assert(i >= 0 && i < 2);
00060 return(array[i]);
00061 }
00062
00063 SCALAR operator[](const unsigned short i)
00064 {
00065 assert(i < 2);
00066 return(array[i]);
00067 }
00068
00069 SCALAR operator[](const char i)
00070 {
00071 assert(i >= 0 && i < 2);
00072 return(array[i]);
00073 }
00074
00075 SCALAR operator[](const unsigned char i)
00076 {
00077 assert(i < 2);
00078 return(array[i]);
00079 }
00080
00081
00082
00083 inline bool operator==(VECTOR &pv)
00084 {
00085 return(x == pv.x && y == pv.y);
00086 }
00087
00088 bool nearlyEqual(VECTOR& pv, SCALAR e)
00089 {
00090 return(fabs(x-pv.x) < e && fabs(y-pv.y) < e);
00091 }
00092
00093
00094
00095 inline bool operator!=(VECTOR &pv)
00096 {
00097 return(x != pv.x || y != pv.y);
00098 }
00099
00100 #ifndef SCALARUNSIGNED
00101 inline VECTOR operator-()
00102 {
00103 return(VECTOR(-x, -y));
00104 }
00105 #endif
00106
00107 inline VECTOR& operator=(VECTOR &pv)
00108 {
00109 x = pv.x;
00110 y = pv.y;
00111 return(*this);
00112 }
00113
00114 inline VECTOR& operator=(float &ps)
00115 {
00116 x = y = (SCALAR)ps;
00117 return(*this);
00118 }
00119
00120 inline VECTOR& operator=(double &ps)
00121 {
00122 x = y = (SCALAR)ps;
00123 return(*this);
00124 }
00125
00126 inline VECTOR& operator=(long &ps)
00127 {
00128 x = y = (SCALAR)ps;
00129 return(*this);
00130 }
00131
00132 inline VECTOR& operator=(unsigned long &ps)
00133 {
00134 x = y = (SCALAR)ps;
00135 return(*this);
00136 }
00137
00138 inline VECTOR& operator=(int &ps)
00139 {
00140 x = y = (SCALAR)ps;
00141 return(*this);
00142 }
00143
00144 inline VECTOR& operator=(unsigned int &ps)
00145 {
00146 x = y = (SCALAR)ps;
00147 return(*this);
00148 }
00149
00150 inline VECTOR& operator=(short &ps)
00151 {
00152 x = y = (SCALAR)ps;
00153 return(*this);
00154 }
00155
00156 inline VECTOR& operator=(unsigned short &ps)
00157 {
00158 x = y = (SCALAR)ps;
00159 return(*this);
00160 }
00161
00162 inline VECTOR& operator=(char &ps)
00163 {
00164 x = y = (SCALAR)ps;
00165 return(*this);
00166 }
00167
00168 inline VECTOR& operator=(unsigned char &ps)
00169 {
00170 x = y = (SCALAR)ps;
00171 return(*this);
00172 }
00173
00174
00175
00176 inline operator SCALAR*()
00177 {
00178 return(array);
00179 }
00180
00181
00183
00184
00185 inline VECTOR operator+(VECTOR &pv)
00186 {
00187 return(VECTOR(x + pv.x, y + pv.y));
00188 }
00189
00190 inline VECTOR& operator+=(VECTOR &pv)
00191 {
00192 x += pv.x;
00193 y += pv.y;
00194 return(*this);
00195 }
00196
00197 inline VECTOR operator-(VECTOR &pv)
00198 {
00199 return(VECTOR(x - pv.x, y - pv.y));
00200 }
00201
00202 inline VECTOR& operator-=(VECTOR &pv)
00203 {
00204 x -= pv.x;
00205 y -= pv.y;
00206 return(*this);
00207 }
00208
00209
00210 inline VECTOR operator*(VECTOR &pv)
00211 {
00212 return(VECTOR(x * pv.x, y * pv.y));
00213 }
00214
00215 inline VECTOR& operator*=(VECTOR &pv)
00216 {
00217 x *= pv.x;
00218 y *= pv.y;
00219 }
00220
00221 inline VECTOR operator/(VECTOR &pv)
00222 {
00223 return(VECTOR(x / pv.x, y / pv.y));
00224 }
00225
00226 inline VECTOR& operator/=(VECTOR &pv)
00227 {
00228 x /= pv.x;
00229 y /= pv.y;
00230 }
00231
00232
00234
00235
00236 inline VECTOR operator+(SCALAR s)
00237 {
00238 return(VECTOR(x + s, y + s));
00239 }
00240
00241 inline VECTOR& operator+=(SCALAR s)
00242 {
00243 x += s;
00244 y += s;
00245 }
00246
00247 inline VECTOR operator-(SCALAR s)
00248 {
00249 return(VECTOR(x - s, y - s));
00250 }
00251
00252 inline VECTOR& operator-=(SCALAR s)
00253 {
00254 x -= s;
00255 y -= s;
00256 }
00257
00258 inline VECTOR operator*(SCALAR s)
00259 {
00260 return(VECTOR(x * s, y * s));
00261 }
00262
00263 inline VECTOR& operator*=(SCALAR s)
00264 {
00265 x *= s;
00266 y *= s;
00267 return(*this);
00268 }
00269
00270 friend inline VECTOR operator*(SCALAR& s, VECTOR& pv);
00271
00272 inline VECTOR operator/(SCALAR s)
00273 {
00274 return(VECTOR(x / s, y / s));
00275 }
00276
00277 inline VECTOR& operator/=(SCALAR s)
00278 {
00279 x /= s;
00280 y /= s;
00281 return(*this);
00282 }
00283
00284
00286
00287
00288 inline void Normalize()
00289 {
00290 SCALAR f = getLength();
00291 x /= f;
00292 y /= f;
00293 }
00294
00295 inline void unit()
00296 {
00297 Normalize();
00298 }
00299
00300
00301 inline SCALAR getLength()
00302 {
00303
00304 return((SCALAR)sqrt(x*x + y*y));
00305 }
00306
00307 inline SCALAR len()
00308 {
00309 return(getLength());
00310 }
00311
00312 inline SCALAR getQuadLength()
00313 {
00314
00315 return(x*x + y*y);
00316 }
00317
00318 inline SCALAR quadlen()
00319 {
00320 return(getQuadLength());
00321 }
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 inline SCALAR getDotProduct(VECTOR &pv)
00337 {
00338 return(x*pv.x + y*pv.y);
00339 }
00340
00341 inline SCALAR dot(VECTOR &pv)
00342 {
00343 return(getDotProduct(pv));
00344 }
00345
00346
00347 inline SCALAR getAngle(VECTOR &pv)
00348 {
00349 return((SCALAR)(acos((double)getDotProduct(pv)) * (double)MUL_RAD_TO_DEG));
00350 }
00351
00352 inline SCALAR angle(VECTOR &pv)
00353 {
00354 return(getAngle(pv));
00355 }
00356
00357
00358 inline SCALAR getDistance(VECTOR &pv)
00359 {
00360 VECTOR t(pv.x - x, pv.y - y);
00361 return(t.getLength());
00362 }
00363
00364 inline SCALAR getQuadDistance(VECTOR &pv)
00365 {
00366 VECTOR t(pv.x - x, pv.y - y);
00367 return(t.getQuadLength());
00368 }
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420 inline void Translate(VECTOR &pv)
00421 {
00422 (*this) += pv;
00423 }
00424
00425 inline void Translate(SCALAR px, SCALAR py)
00426 {
00427 x += px;
00428 y += py;
00429 }
00430
00431
00432 inline void Scale(VECTOR &pv)
00433 {
00434 (*this) *= pv;
00435 }
00436
00437 inline void Scale(SCALAR sx, SCALAR sy)
00438 {
00439 x *= sx;
00440 y *= sy;
00441 }
00442 };
00443
00444
00445 VECTOR operator*(SCALAR& s, VECTOR& pv)
00446 {
00447 return(pv * s);
00448 }