00001 00012 /*************************************************************************** 00013 * Copyright (C) 2007 by Jan Koci * 00014 * honza.koci@email.cz * 00015 * http://kengine.sourceforge.net/tutorial/ 00016 * * 00017 * This program is free software; you can redistribute it and/or modify * 00018 * it under the terms of the GNU General Public License as published by * 00019 * the Free Software Foundation; either version 2 of the License, or * 00020 * (at your option) any later version. * 00021 * * 00022 * This program is distributed in the hope that it will be useful, * 00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00025 * GNU General Public License for more details. * 00026 * * 00027 * You should have received a copy of the GNU General Public License * 00028 * along with this program; if not, write to the * 00029 * Free Software Foundation, Inc., * 00030 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00031 ***************************************************************************/ 00032 00033 00034 #include "K3dVector2.h" 00035 00036 // Konstruktor t�y 2d vektoru, kter resetuje vektor 00037 K3dVector2::K3dVector2() 00038 { 00039 } 00040 00041 00042 00043 // Constructor set vector from array 00044 K3dVector2::K3dVector2( const float vector[2] ) 00045 { 00046 m_afVector[0] = vector[0]; // Set x 00047 m_afVector[1] = vector[1]; // Set y 00048 } 00049 00050 00051 00052 // Constructor set vector from x, y values 00053 K3dVector2::K3dVector2( float x, float y ) 00054 { 00055 m_afVector[0] = x; // Set x 00056 m_afVector[1] = y; // Set y 00057 } 00058 00059 00060 00061 // Descructor 00062 K3dVector2::~K3dVector2() 00063 { 00064 } 00065 00066 00067 00068 // Assignment operator 00069 K3dVector2& K3dVector2::operator= (const K3dVector2& rkV) 00070 { 00071 00072 for(int i=0; i<2; i++) 00073 { 00074 m_afVector[i] = rkV.m_afVector[i]; 00075 } 00076 00077 return *this; 00078 } 00079 00080 00081 00082 // Overloaded operator+ allows add two vectors 00083 K3dVector2 K3dVector2::operator+(const K3dVector2& rkV ) 00084 { 00085 K3dVector2 kSum((float) 0, (float) 0 ); 00086 for (int i = 0; i < 2; i++) 00087 kSum.m_afVector[i] = m_afVector[i] + rkV.m_afVector[i]; 00088 return kSum; 00089 } 00090 00091 00092 00093 // Overloaded operator+ allows add vector and float value 00094 K3dVector2 K3dVector2::operator+( float fScalar ) 00095 { 00096 00097 float afValue[2]; // Slave array 00098 for (int i=0; i<2; i++) // Go through x, y, z values 00099 { 00100 afValue[i] = m_afVector[i] + fScalar; // Hold to slave array 00101 } 00102 00103 return K3dVector2(afValue); // Return slave array 00104 00105 } 00106 00107 00108 00109 // Overloaded operator+= 00110 K3dVector2& K3dVector2::operator+=(const K3dVector2 &rkVector) 00111 { 00112 for (int i=0; i<2; i++) // Go through x, y, z values 00113 { 00114 this->m_afVector[i] += rkVector.m_afVector[i]; 00115 } 00116 00117 return *this; 00118 } 00119 00120 00121 00122 // Overloaded scalar operator+= 00123 K3dVector2& K3dVector2::operator+=(float fScalar ) 00124 { 00125 for (int i=0; i<2; i++) 00126 { 00127 this->m_afVector[i] += fScalar; 00128 } 00129 00130 return *this; 00131 } 00132 00133 00134 00135 // Overloaded scalar operator-= 00136 K3dVector2& K3dVector2::operator-=(float fScalar ) 00137 { 00138 for (int i=0; i<2; i++) 00139 { 00140 this->m_afVector[i] -= fScalar; 00141 } 00142 00143 return *this; 00144 } 00145 00146 00147 00148 // Overloaded operator- allows subtract two vectors 00149 K3dVector2 K3dVector2::operator-(K3dVector2 &rkVector ) 00150 { 00151 float f_value[2]; 00152 for (int i=0; i<2; i++) 00153 { 00154 f_value[i] = m_afVector[i] - rkVector[i]; // Hold to slave array 00155 } 00156 00157 return K3dVector2(f_value); // Return slave array 00158 } 00159 00160 00161 00162 // Overloaded operator- allows add vector and float value 00163 K3dVector2 K3dVector2::operator-( float f ) 00164 { 00165 float afValue[2]; // Slave array 00166 for (int i=0; i<2; i++) // Go through x, y, z values 00167 { 00168 afValue[i] = m_afVector[i] - f; // Hold to slave array 00169 } 00170 00171 return K3dVector2(afValue); // Return slave array 00172 } 00173 00174 00175 00176 K3dVector2 K3dVector2::operator- () 00177 { 00178 K3dVector2 kNeg((float)0, (float)0); 00179 for (int i = 0; i < 2; i++) 00180 kNeg.m_afVector[i] = -m_afVector[i]; 00181 return kNeg; 00182 } 00183 00184 00185 00186 00187 00188 // Overloaded operator* allows multiply vector and float value 00189 K3dVector2 K3dVector2::operator*( float num ) 00190 { 00191 float f_value[2]; // Slave array 00192 for (int i=0; i<2; i++) // Go through x, y, z values 00193 { 00194 f_value[i] = m_afVector[i] * num; // Hold to slave array 00195 00196 } 00197 00198 return K3dVector2(f_value); // Return slave array 00199 } 00200 00201 00202 00203 // Overloaded operator*= allows multiply two vectors 00204 K3dVector2 K3dVector2::operator*=( const K3dVector2 &rkVector ) 00205 { 00206 for (int i=0; i<2; i++) // Go through x, y, z values 00207 { 00208 m_afVector[i] *= rkVector.m_afVector[i]; // Hold to slave array 00209 } 00210 00211 return *this; 00212 } 00213 00214 00215 00216 // Overloaded operator*= allows multiply vector and float value 00217 K3dVector2 K3dVector2::operator*=( float fScalar ) 00218 { 00219 for (int i=0; i<2; i++) // Go through x, y, z values 00220 { 00221 m_afVector[i] *= fScalar; // Hold to slave array 00222 } 00223 00224 return *this; 00225 } 00226 00227 00228 00229 // Overloaded operator/= allows divide two vectors 00230 K3dVector2 K3dVector2::operator/=( const K3dVector2 &rkVector ) 00231 { 00232 for (int i=0; i<2; i++) // Go through x, y, z values 00233 { 00234 if(rkVector.m_afVector[i]) 00235 { 00236 m_afVector[i] /= rkVector.m_afVector[i]; // Hold to slave array 00237 } 00238 else 00239 { 00240 m_afVector[i] = 0; 00241 } 00242 } 00243 00244 return *this; 00245 } 00246 00247 00248 00249 // Overloaded operator/= allows divide vector and float value 00250 K3dVector2 K3dVector2::operator/=( float fScalar ) 00251 { 00252 float fInvScalar; 00253 00254 if(fScalar) 00255 { 00256 fInvScalar = 1.0f / fScalar; 00257 for (int i=0; i<2; i++) 00258 { 00259 m_afVector[i] *= fInvScalar; 00260 } 00261 } 00262 else 00263 { 00264 this->Set(0,0); 00265 } 00266 00267 00268 return *this; 00269 } 00270 00271 00272 00273 00274 // Overloaded operator* allows multiply two vectors 00275 K3dVector2 K3dVector2::operator*( K3dVector2 &rkVector ) 00276 { 00277 float af[2]; 00278 00279 for (int i=0; i<2; i++) // Go through x, y, z values 00280 { 00281 af[i] = m_afVector[i] * rkVector.m_afVector[i]; 00282 } 00283 00284 return K3dVector2(af); 00285 } 00286 00287 00288 00289 // Overloaded operator* allows divide two vectors 00290 K3dVector2 K3dVector2::operator/( K3dVector2 &rkVector ) 00291 { 00292 K3dVector2 kV(0,0); 00293 00294 00295 for (int i=0; i<2; i++) // Go through x, y, z values 00296 { 00297 if(rkVector.m_afVector[i]) 00298 { 00299 kV.m_afVector[i] = m_afVector[i] / rkVector.m_afVector[i]; 00300 } 00301 } 00302 00303 return kV; 00304 00305 00306 } 00307 00308 00309 00310 // Overloaded operator* allows divide vector by scalar 00311 K3dVector2 K3dVector2::operator/( float fScalar ) 00312 { 00313 K3dVector2 kV(0,0); 00314 00315 if(fScalar) 00316 { 00317 for (int i=0; i<2; i++) // Go through x, y, z values 00318 { 00319 kV.m_afVector[i] = m_afVector[i] / fScalar; 00320 } 00321 } 00322 return kV; 00323 } 00324 00325 00326 00327 // Compare arrays for comparison operators 00328 int K3dVector2::CompareArrays(const K3dVector2& rkV) 00329 { 00330 unsigned int uiTest0; 00331 unsigned int uiTest1; 00332 00333 for (int i = 0; i < 2; i++) 00334 { 00335 uiTest0 = *(unsigned int*)&m_afVector[i]; 00336 uiTest1 = *(unsigned int*)&rkV.m_afVector[i]; 00337 if ( uiTest0 < uiTest1 ) 00338 return -1; 00339 if ( uiTest0 > uiTest1 ) 00340 return +1; 00341 } 00342 return 0; 00343 } 00344 00345 00346 00347 // Calc square vector length 00348 float K3dVector2::SquaredLength () 00349 { 00350 float fSqrLen = 0.0f; 00351 for (int i = 0; i < 2; i++) 00352 fSqrLen += m_afVector[i]*m_afVector[i]; 00353 return fSqrLen; 00354 } 00355 00356 00357 00358 00359 00360