00001
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #pragma once
00035
00036 #include <iostream>
00037 #include <cmath>
00038
00039 #include "../System/K3dString.h"
00040
00041
00042
00044
00046
00047 class K3dVector2
00048 {
00049
00050 float m_afVector[2];
00051 int m_iId;
00052 public:
00053
00054
00055
00056 K3dVector2();
00057
00058 K3dVector2( const float vector[2] );
00059
00060 K3dVector2( float x, float y);
00062 ~K3dVector2();
00063
00065
00067
00068
00069 float *GetVector();
00071 int &GetId()
00072 {
00073 return m_iId;
00074 }
00075
00076 float &operator[]( int index) ;
00077 float operator[]( int index) const;
00078
00079 float GetScalar(const int index);
00080
00081 void SetScalar(const float fScalar, const int index);
00082
00083 void Set( K3dVector2 vec );
00084
00085 void Set( float vector[2] );
00086
00087 void Set( float x, float y);
00088
00089 void SetX(const float fX);
00090
00091 void SetY(const float fY);
00092 float &GetX()
00093 {
00094 return m_afVector[0];
00095 }
00096 float &GetY()
00097 {
00098 return m_afVector[1];
00099 }
00100
00102
00104 K3dVector2& operator= (const K3dVector2& rkV);
00105
00107
00109 bool operator== (const K3dVector2& rkV);
00110 bool operator!= (const K3dVector2& rkV);
00111 bool operator< (const K3dVector2& rkV);
00112 bool operator<= (const K3dVector2& rkV);
00113 bool operator> (const K3dVector2& rkV);
00114 bool operator>= (const K3dVector2& rkV);
00115
00116 int CompareArrays(const K3dVector2& rkV);
00117
00118
00120
00122
00123
00124 K3dVector2 operator+(const K3dVector2& rkV );
00125
00126 K3dVector2 operator+( float fScalar );
00127
00128 K3dVector2 operator-(K3dVector2 &rkVector );
00129
00130 K3dVector2 operator-( float fScalar );
00131 K3dVector2 operator- ();
00132
00133 K3dVector2 operator*( K3dVector2 &rkVector );
00134
00135 K3dVector2 operator*( float fScalar );
00136
00137 K3dVector2 operator/( K3dVector2 &rkVector);
00138
00139 K3dVector2 operator/( float fScalar );
00140
00141
00143
00145
00146
00147 K3dVector2& operator+=(const K3dVector2 &rkVector);
00148
00149 K3dVector2& operator+=(float fScalar );
00150
00151 K3dVector2& operator-=(const K3dVector2 &rkVector);
00152
00153 K3dVector2& operator-=(float fScalar );
00154
00155 K3dVector2 operator*=( const K3dVector2 &rkVector );
00156
00157 K3dVector2 operator*=( float fScalar );
00158
00159 K3dVector2 operator/=( const K3dVector2 &rkVector );
00160
00161 K3dVector2 operator/=( float fScalar );
00162
00163
00164
00166
00168
00169 K3dVector2 Sqr();
00170
00171 void Add( const K3dVector2& vec );
00172
00173 void AddScalar( const float fScalar, const int index );
00174
00175 void SubtractScalar( const float fScalar, const int index );
00176
00177 void Reset();
00178
00179 float Length();
00180
00181 float SquaredLength ();
00182
00183 void Normalize();
00184
00185 void VectorBetween(K3dVector2 &_rkVec_1,K3dVector2 &_rkVec_2);
00186
00187 void AddVector(K3dVector2 &_rkVec_1, K3dVector2 &_rkVec_2);
00188
00189 void DivideVector(K3dVector2 &_rkVec, float divider);
00190
00191 void Translate(const K3dVector2 &_rkVertex, const K3dVector2 &_rkDirectVec, const float distance);
00192
00193 void Invert();
00194
00195 float Dot( const K3dVector2 &_rkV);
00196
00197 void Angle();
00198
00199 };
00200
00201
00202
00203
00204 inline bool K3dVector2::operator== (const K3dVector2& rkV)
00205 {
00206 return memcmp(m_afVector, rkV.m_afVector, 2*sizeof(float)) == 0;
00207 }
00208
00209
00210
00211 inline bool K3dVector2::operator!= (const K3dVector2& rkV)
00212 {
00213 return memcmp(m_afVector, rkV.m_afVector, 2*sizeof(float)) != 0;
00214 }
00215
00216
00217
00218 inline bool K3dVector2::operator< (const K3dVector2& rkV)
00219 {
00220 return CompareArrays(rkV) < 0;
00221 }
00222
00223
00224
00225 inline bool K3dVector2::operator<= (const K3dVector2& rkV)
00226 {
00227 return CompareArrays(rkV) <= 0;
00228 }
00229
00230
00231
00232 inline bool K3dVector2::operator> (const K3dVector2& rkV)
00233 {
00234 return CompareArrays(rkV) > 0;
00235 }
00236
00237
00238
00239 inline bool K3dVector2::operator>= (const K3dVector2& rkV)
00240 {
00241 return CompareArrays(rkV) >= 0;
00242 }
00243
00244
00245
00246
00247 inline float &K3dVector2::operator[]( int i )
00248 {
00249 if(!(0 <= i) && (i < 2) )
00250 {
00251 cerr << "Error index -- Index must be from 0 to 1" << i <<endl;
00252 return m_afVector[0];
00253 }
00254 return m_afVector[i];
00255 }
00256
00257 inline float K3dVector2::operator[]( int i ) const
00258 {
00259 if(!(0 <= i) && (i < 2) )
00260 {
00261 cerr << "Error index -- Index must be from 0 to 1" << i <<endl;
00262 return m_afVector[0];
00263 }
00264 return m_afVector[i];
00265 }
00266
00267
00268
00269
00270 inline float *K3dVector2::GetVector()
00271 {
00272 return m_afVector;
00273 }
00274
00275
00276
00277
00278 inline float K3dVector2::GetScalar(const int i)
00279 {
00280 if(!(0 <= i) && (i < 2) )
00281 {
00282 cerr << "Error index -- Index must be from 0 to 1" << i <<endl;
00283 return (float)0;
00284
00285 }
00286 return m_afVector[i];
00287 }
00288
00289
00290
00291
00292 inline void K3dVector2::SetScalar(const float fScalar, const int i)
00293 {
00294 if(!(0 <= i) && (i < 2) )
00295 {
00296 cerr << "Error index -- Index must be from 0 to 1" << i <<endl;
00297 return;
00298
00299 }
00300 m_afVector[i] = fScalar;
00301 }
00302
00303
00304
00305
00306 inline void K3dVector2::Translate(const K3dVector2 &_rkVertex, const K3dVector2 &_rkDirectVec, const float distance)
00307 {
00308 m_afVector[0] = _rkVertex.m_afVector[0] + (_rkDirectVec.m_afVector[0] * distance);
00309 m_afVector[1] = _rkVertex.m_afVector[1] + (_rkDirectVec.m_afVector[1] * distance);
00310 }
00311
00312
00313
00314
00315 inline void K3dVector2::Invert()
00316 {
00317 m_afVector[0] = -m_afVector[0];
00318 m_afVector[1] = -m_afVector[1];
00319 }
00320
00321
00322
00323
00324
00325 inline float K3dVector2::Dot(const K3dVector2 &_rkVector)
00326 {
00327 float fX,fY, fResult;
00328 fX = m_afVector[0] * _rkVector.m_afVector[0];
00329 fY = m_afVector[1] * _rkVector.m_afVector[1];
00330 fResult = fX + fY;
00331 return fResult;
00332 }
00333
00334
00335
00336
00337 inline void K3dVector2::Reset()
00338 {
00339 m_afVector[0] = m_afVector[1] = 0;
00340 }
00341
00342
00343
00344
00345 inline void K3dVector2::Set( K3dVector2 vec )
00346 {
00347 m_afVector[0] = vec.m_afVector[0];
00348 m_afVector[1] = vec.m_afVector[1];
00349 }
00350
00351
00352
00353
00354 inline void K3dVector2::Set(float vector[2] )
00355 {
00356 m_afVector[0] = vector[0];
00357 m_afVector[1] = vector[1];
00358 }
00359
00360
00361
00362
00363 inline void K3dVector2::Set( float x, float y )
00364 {
00365 m_afVector[0] = x;
00366 m_afVector[1] = y;
00367 }
00368
00369
00370
00371
00372 inline void K3dVector2::SetX(const float fX)
00373 {
00374 m_afVector[0] = fX;
00375 }
00376
00377
00378 inline void K3dVector2::SetY(const float fY)
00379 {
00380 m_afVector[1] = fY;
00381 }
00382
00383
00384
00385 inline void K3dVector2::Add( const K3dVector2& vec )
00386 {
00387 m_afVector[0] += vec.m_afVector[0];
00388 m_afVector[1] += vec.m_afVector[1];
00389 }
00390
00391
00392
00393
00394 inline void K3dVector2::AddScalar( const float fScalar, const int i )
00395 {
00396 if(!(0 <= i) && (i < 2) )
00397 {
00398 cerr << "Error index -- Index must be from 0 to 1" << i <<endl;
00399 return;
00400 }
00401 m_afVector[i] += fScalar;
00402 }
00403
00404
00405
00406
00407 inline void K3dVector2::SubtractScalar( const float fScalar, const int i )
00408 {
00409 if(!(0 <= i) && (i < 2) )
00410 {
00411 cerr << "Error index -- Index must be from 0 to 1" << i <<endl;
00412 return;
00413 }
00414 m_afVector[i] -= fScalar;
00415 }
00416
00417
00418
00419
00420
00421 inline void K3dVector2::Normalize()
00422 {
00423
00424 float fLength0, fLength1;
00425
00426
00427 fLength0 = Length();
00428
00429 if (fLength0)
00430 {
00431 fLength1 = 1.0f/fLength0;
00432 m_afVector[0] *= fLength1;
00433 m_afVector[1] *= fLength1;
00434 }
00435 }
00436
00437
00438
00439
00440 inline float K3dVector2::Length()
00441 {
00442 return (float) sqrt( m_afVector[0] * m_afVector[0] + m_afVector[1] * m_afVector[1] );
00443 }
00444
00445
00446
00447
00448 inline K3dVector2 K3dVector2::Sqr()
00449 {
00450 K3dVector2 kV;
00451
00452 kV = *this;
00453
00454 kV *= kV;
00455
00456 return kV;
00457 }
00458
00459
00460
00461
00462
00463 inline void K3dVector2::VectorBetween(K3dVector2 &_rkVec_1,K3dVector2 &_rkVec_2)
00464 {
00465 m_afVector[0] = _rkVec_1[0] - _rkVec_2[0];
00466 m_afVector[1] = _rkVec_1[1] - _rkVec_2[1];
00467 }
00468
00469
00470
00471
00472 inline void K3dVector2::AddVector(K3dVector2 &_rkVec_1, K3dVector2 &_rkVec_2)
00473 {
00474 m_afVector[0] = _rkVec_1[0] + _rkVec_2[0];
00475 m_afVector[1] = _rkVec_1[1] + _rkVec_2[1];
00476 }
00477
00478
00479
00480
00481 inline void K3dVector2::DivideVector(K3dVector2 &_rkVec, float divider)
00482 {
00483 if(divider!=0)
00484 {
00485 m_afVector[0] = _rkVec[0] / divider;
00486 m_afVector[1] = _rkVec[1] / divider;
00487 }
00488 else
00489 {
00490 cerr << "inline void K3dVector2::DivideVector() -- Error - Divide by zero" << endl;
00491 }
00492 }
00493
00494
00495
00496
00497 inline void K3dVector2::Angle()
00498 {
00499
00500 K3dVector2 kV;
00501
00502 float fLength;
00503
00504
00505
00506 fLength = this->Length();
00507
00508
00509
00510 if(fLength)
00511 {
00512
00513 fLength = 1/fLength;
00514
00515 kV.m_afVector[0] = acos(this->m_afVector[0] * fLength);
00516
00517 kV.m_afVector[1] = acos(this->m_afVector[1] * fLength);
00518 *this = kV;
00519 }
00520 }
00521