glc_vector2d.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  http://glc-lib.sourceforge.net
00006 
00007  GLC-lib is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU Lesser General Public License as published by
00009  the Free Software Foundation; either version 3 of the License, or
00010  (at your option) any later version.
00011 
00012  GLC-lib is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU Lesser General Public License for more details.
00016 
00017  You should have received a copy of the GNU Lesser General Public License
00018  along with GLC-lib; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 *****************************************************************************/
00022 
00024 
00025 #ifndef GLC_VECTOR2D_H_
00026 #define GLC_VECTOR2D_H_
00027 
00028 #include <QString>
00029 #include "glc_utils_maths.h"
00030 #include "glc_vector2df.h"
00031 
00032 #include "../glc_config.h"
00033 
00035 // definition global
00037 
00040 
00044 
00045 
00046 class GLC_LIB_EXPORT GLC_Vector2d
00047 {
00048         friend class GLC_Vector4d;
00049         friend class GLC_Vector3d;
00050 
00052         inline friend GLC_Vector2d operator - (const GLC_Vector2d &Vect)
00053         {
00054                 return GLC_Vector2d(-Vect.m_Vector[0], -Vect.m_Vector[1]);
00055         }
00056 
00057 
00059 
00061 
00062 public:
00068         inline GLC_Vector2d()
00069         {
00070                 m_Vector[0]= 0.0;
00071                 m_Vector[1]= 0.0;
00072         }
00073 
00075         inline GLC_Vector2d(const double &dX, const double &dY)
00076         {
00077                 m_Vector[0]= dX;
00078                 m_Vector[1]= dY;
00079         }
00080 
00087         inline GLC_Vector2d(const GLC_Vector2d &Vect)
00088         {
00089                 m_Vector[0]= Vect.m_Vector[0];
00090                 m_Vector[1]= Vect.m_Vector[1];
00091         }
00093 
00094 
00096 
00097 public:
00098 
00100         inline GLC_Vector2d operator + (const GLC_Vector2d &Vect) const
00101         {
00102                 GLC_Vector2d VectResult(m_Vector[0] + Vect.m_Vector[0], m_Vector[1] + Vect.m_Vector[1]);
00103 
00104                 return VectResult;
00105         }
00106 
00108         inline GLC_Vector2d& operator = (const GLC_Vector2d &Vect)
00109         {
00110                 m_Vector[0]= Vect.m_Vector[0];
00111                 m_Vector[1]= Vect.m_Vector[1];
00112 
00113                 return *this;
00114         }
00115 
00117         inline GLC_Vector2d& operator = (const GLC_Vector2df &Vect)
00118         {
00119                 m_Vector[0]= static_cast<double>(Vect.vector[0]);
00120                 m_Vector[1]= static_cast<double>(Vect.vector[1]);
00121 
00122                 return *this;
00123         }
00124 
00125 
00127         inline GLC_Vector2d* operator += (const GLC_Vector2d &Vect)
00128         {
00129                 *this= *this + Vect;
00130                 return this;
00131         }
00132 
00133 
00135         inline GLC_Vector2d operator - (const GLC_Vector2d &Vect) const
00136         {
00137                 GLC_Vector2d VectResult(m_Vector[0] - Vect.m_Vector[0], m_Vector[1] - Vect.m_Vector[1]);
00138 
00139                 return VectResult;
00140         }
00141 
00143         inline GLC_Vector2d* operator -= (const GLC_Vector2d &Vect)
00144         {
00145                 *this= *this - Vect;
00146                 return this;
00147         }
00148 
00150         inline double operator ^ (const GLC_Vector2d &Vect) const
00151         {
00152                 return m_Vector[0] * Vect.m_Vector[1] - m_Vector[1] * Vect.m_Vector[0];
00153         }
00154 
00156         inline double operator * (const GLC_Vector2d &Vect) const
00157         {
00158                 return m_Vector[0] * Vect.m_Vector[0] + m_Vector[1] * Vect.m_Vector[1];
00159         }
00160 
00162         inline GLC_Vector2d operator * (double Scalaire) const
00163         {
00164                 return GLC_Vector2d(m_Vector[0] * Scalaire, m_Vector[1] * Scalaire);;
00165         }
00166 
00167 
00169         inline bool operator == (const GLC_Vector2d &Vect) const
00170         {
00171                 bool bResult= qFuzzyCompare(m_Vector[0], Vect.m_Vector[0]);
00172                 bResult= bResult && qFuzzyCompare(m_Vector[1], Vect.m_Vector[1]);
00173 
00174                 return bResult;
00175         }
00176 
00178         inline bool operator != (const GLC_Vector2d &Vect) const
00179         {
00180                 return !(*this == Vect);
00181         }
00182 
00184 
00186 
00188 
00189 public:
00191         inline GLC_Vector2d& setX(const double &dX)
00192         {
00193                 m_Vector[0]= dX;
00194                 return *this;
00195         }
00196 
00198         inline GLC_Vector2d& setY(const double &dY)
00199         {
00200                 m_Vector[1]= dY;
00201                 return *this;
00202         }
00203 
00205         inline GLC_Vector2d& setVect(const double &dX, const double &dY)
00206         {
00207                 m_Vector[0]= dX;
00208                 m_Vector[1]= dY;
00209                 return *this;
00210         }
00211 
00213         inline GLC_Vector2d& setVect(const GLC_Vector2d &Vect)
00214         {
00215                 m_Vector[0]= Vect.m_Vector[0];
00216                 m_Vector[1]= Vect.m_Vector[1];
00217                 return *this;
00218         }
00219 
00221         inline GLC_Vector2d& setLength(double);
00222 
00224         inline GLC_Vector2d& normalize()
00225         {return setLength(1.0);}
00226 
00228 
00230 
00232 
00233 public:
00235         inline double getX(void) const
00236         {
00237                 return m_Vector[0];
00238         }
00240         inline double getY(void) const
00241         {
00242                 return m_Vector[1];
00243         }
00245         inline const double *return_dVect(void) const
00246         {
00247                 return m_Vector;
00248         }
00250         inline bool isNull(void) const
00251         {
00252                 return qFuzzyCompare(m_Vector[0], 0.0) && qFuzzyCompare(m_Vector[1], 0.0);
00253         }
00255         inline QString toString() const
00256         {
00257                 return QString("[") + QString::number(m_Vector[0]) + QString(" , ") + QString::number(m_Vector[1]) + QString("]");
00258         }
00260         inline GLC_Vector2d perp() const
00261         {
00262                 return GLC_Vector2d(-m_Vector[1], m_Vector[0]);
00263         }
00265 
00267 //name Private attributes
00269 private:
00274         double m_Vector[2];
00275 
00276 }; //class GLC_Vector2d
00277 
00279 typedef GLC_Vector2d GLC_Point2d;
00280 
00281 inline GLC_Vector2d& GLC_Vector2d::setLength(double norme)
00282 {
00283         const double normCur= sqrt( m_Vector[0] * m_Vector[0] + m_Vector[1] * m_Vector[1]);
00284 
00285         if (normCur != 0.0f)
00286         {
00287                 const double Coef = norme / normCur;
00288 
00289                 m_Vector[0] = m_Vector[0] * Coef;
00290                 m_Vector[1] = m_Vector[1] * Coef;
00291         }
00292         return *this;
00293 }
00294 
00295 #endif /*GLC_VECTOR2D_H_*/

SourceForge.net Logo

©2005-2011 Laurent Ribon