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 <QPointF>
00030 
00031 #include "glc_utils_maths.h"
00032 #include "glc_vector2df.h"
00033 
00034 #include "../glc_config.h"
00035 
00037 // definition global
00039 
00042 
00046 
00047 
00048 class GLC_LIB_EXPORT GLC_Vector2d
00049 {
00050         friend class GLC_Vector4d;
00051         friend class GLC_Vector3d;
00052 
00054         inline friend GLC_Vector2d operator - (const GLC_Vector2d &Vect)
00055         {
00056                 return GLC_Vector2d(-Vect.m_Vector[0], -Vect.m_Vector[1]);
00057         }
00058 
00059 
00061 
00063 
00064 public:
00070         inline GLC_Vector2d()
00071         {
00072                 m_Vector[0]= 0.0;
00073                 m_Vector[1]= 0.0;
00074         }
00075 
00077         inline GLC_Vector2d(const double &dX, const double &dY)
00078         {
00079                 m_Vector[0]= dX;
00080                 m_Vector[1]= dY;
00081         }
00082 
00089         inline GLC_Vector2d(const GLC_Vector2d &Vect)
00090         {
00091                 m_Vector[0]= Vect.m_Vector[0];
00092                 m_Vector[1]= Vect.m_Vector[1];
00093         }
00094 
00096         inline QPointF toQPointF() const
00097         {return QPointF(m_Vector[0], m_Vector[1]);}
00098 
00100 
00101 
00103 
00104 public:
00105 
00107         inline GLC_Vector2d operator + (const GLC_Vector2d &Vect) const
00108         {
00109                 GLC_Vector2d VectResult(m_Vector[0] + Vect.m_Vector[0], m_Vector[1] + Vect.m_Vector[1]);
00110 
00111                 return VectResult;
00112         }
00113 
00115         inline GLC_Vector2d& operator = (const GLC_Vector2d &Vect)
00116         {
00117                 m_Vector[0]= Vect.m_Vector[0];
00118                 m_Vector[1]= Vect.m_Vector[1];
00119 
00120                 return *this;
00121         }
00122 
00124         inline GLC_Vector2d& operator = (const GLC_Vector2df &Vect)
00125         {
00126                 m_Vector[0]= static_cast<double>(Vect.vector[0]);
00127                 m_Vector[1]= static_cast<double>(Vect.vector[1]);
00128 
00129                 return *this;
00130         }
00131 
00132 
00134         inline GLC_Vector2d* operator += (const GLC_Vector2d &Vect)
00135         {
00136                 *this= *this + Vect;
00137                 return this;
00138         }
00139 
00140 
00142         inline GLC_Vector2d operator - (const GLC_Vector2d &Vect) const
00143         {
00144                 GLC_Vector2d VectResult(m_Vector[0] - Vect.m_Vector[0], m_Vector[1] - Vect.m_Vector[1]);
00145 
00146                 return VectResult;
00147         }
00148 
00150         inline GLC_Vector2d* operator -= (const GLC_Vector2d &Vect)
00151         {
00152                 *this= *this - Vect;
00153                 return this;
00154         }
00155 
00157         inline double operator ^ (const GLC_Vector2d &Vect) const
00158         {
00159                 return m_Vector[0] * Vect.m_Vector[1] - m_Vector[1] * Vect.m_Vector[0];
00160         }
00161 
00163         inline double operator * (const GLC_Vector2d &Vect) const
00164         {
00165                 return m_Vector[0] * Vect.m_Vector[0] + m_Vector[1] * Vect.m_Vector[1];
00166         }
00167 
00169         inline GLC_Vector2d operator * (double Scalaire) const
00170         {
00171                 return GLC_Vector2d(m_Vector[0] * Scalaire, m_Vector[1] * Scalaire);;
00172         }
00173 
00174 
00176         inline bool operator == (const GLC_Vector2d &Vect) const
00177         {
00178                 bool bResult= qFuzzyCompare(m_Vector[0], Vect.m_Vector[0]);
00179                 bResult= bResult && qFuzzyCompare(m_Vector[1], Vect.m_Vector[1]);
00180 
00181                 return bResult;
00182         }
00183 
00185         inline bool operator != (const GLC_Vector2d &Vect) const
00186         {
00187                 return !(*this == Vect);
00188         }
00189 
00191 
00193 
00195 
00196 public:
00198         inline GLC_Vector2d& setX(const double &dX)
00199         {
00200                 m_Vector[0]= dX;
00201                 return *this;
00202         }
00203 
00205         inline GLC_Vector2d& setY(const double &dY)
00206         {
00207                 m_Vector[1]= dY;
00208                 return *this;
00209         }
00210 
00212         inline GLC_Vector2d& setVect(const double &dX, const double &dY)
00213         {
00214                 m_Vector[0]= dX;
00215                 m_Vector[1]= dY;
00216                 return *this;
00217         }
00218 
00220         inline GLC_Vector2d& setVect(const GLC_Vector2d &Vect)
00221         {
00222                 m_Vector[0]= Vect.m_Vector[0];
00223                 m_Vector[1]= Vect.m_Vector[1];
00224                 return *this;
00225         }
00226 
00228         inline GLC_Vector2d& setLength(double);
00229 
00231         inline GLC_Vector2d& normalize()
00232         {return setLength(1.0);}
00233 
00235 
00237 
00239 
00240 public:
00242         inline double getX(void) const
00243         {
00244                 return m_Vector[0];
00245         }
00247         inline double getY(void) const
00248         {
00249                 return m_Vector[1];
00250         }
00252         inline const double *return_dVect(void) const
00253         {
00254                 return m_Vector;
00255         }
00257         inline bool isNull(void) const
00258         {
00259                 return qFuzzyCompare(m_Vector[0], 0.0) && qFuzzyCompare(m_Vector[1], 0.0);
00260         }
00262         inline QString toString() const
00263         {
00264                 return QString("[") + QString::number(m_Vector[0]) + QString(" , ") + QString::number(m_Vector[1]) + QString("]");
00265         }
00267         inline GLC_Vector2d perp() const
00268         {
00269                 return GLC_Vector2d(-m_Vector[1], m_Vector[0]);
00270         }
00272 
00274 //name Private attributes
00276 private:
00281         double m_Vector[2];
00282 
00283 }; //class GLC_Vector2d
00284 
00286 typedef GLC_Vector2d GLC_Point2d;
00287 
00288 inline GLC_Vector2d& GLC_Vector2d::setLength(double norme)
00289 {
00290         const double normCur= sqrt( m_Vector[0] * m_Vector[0] + m_Vector[1] * m_Vector[1]);
00291 
00292         if (normCur != 0.0f)
00293         {
00294                 const double Coef = norme / normCur;
00295 
00296                 m_Vector[0] = m_Vector[0] * Coef;
00297                 m_Vector[1] = m_Vector[1] * Coef;
00298         }
00299         return *this;
00300 }
00301 
00302 #endif /*GLC_VECTOR2D_H_*/

SourceForge.net Logo

©2005-2011 Laurent Ribon