GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_vector2d.h
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21 *****************************************************************************/
22 
24 
25 #ifndef GLC_VECTOR2D_H_
26 #define GLC_VECTOR2D_H_
27 
28 #include <QString>
29 #include <QPointF>
30 
31 #include "glc_utils_maths.h"
32 #include "glc_vector2df.h"
33 
34 #include "../glc_config.h"
35 
37 // definition global
39 
42 
45 
46 
48 {
49  friend class GLC_Vector4d;
50  friend class GLC_Vector3d;
51 
53  inline friend GLC_Vector2d operator - (const GLC_Vector2d &Vect)
54  {
55  return GLC_Vector2d(-Vect.m_Vector[0], -Vect.m_Vector[1]);
56  }
57 
58 
60 
62 
63 public:
69  inline GLC_Vector2d()
70  {
71  m_Vector[0]= 0.0;
72  m_Vector[1]= 0.0;
73  }
74 
76  inline GLC_Vector2d(const double &dX, const double &dY)
77  {
78  m_Vector[0]= dX;
79  m_Vector[1]= dY;
80  }
81 
83  inline GLC_Vector2d(const GLC_Vector2d &Vect)
84  {
85  m_Vector[0]= Vect.m_Vector[0];
86  m_Vector[1]= Vect.m_Vector[1];
87  }
88 
90  inline QPointF toQPointF() const
91  {return QPointF(m_Vector[0], m_Vector[1]);}
92 
94 
95 
97 
98 public:
99 
101  inline GLC_Vector2d operator + (const GLC_Vector2d &Vect) const
102  {
103  GLC_Vector2d VectResult(m_Vector[0] + Vect.m_Vector[0], m_Vector[1] + Vect.m_Vector[1]);
104 
105  return VectResult;
106  }
107 
109  inline GLC_Vector2d& operator = (const GLC_Vector2d &Vect)
110  {
111  m_Vector[0]= Vect.m_Vector[0];
112  m_Vector[1]= Vect.m_Vector[1];
113 
114  return *this;
115  }
116 
119  {
120  m_Vector[0]= static_cast<double>(Vect.vector[0]);
121  m_Vector[1]= static_cast<double>(Vect.vector[1]);
122 
123  return *this;
124  }
125 
126 
129  {
130  *this= *this + Vect;
131  return this;
132  }
133 
134 
136  inline GLC_Vector2d operator - (const GLC_Vector2d &Vect) const
137  {
138  GLC_Vector2d VectResult(m_Vector[0] - Vect.m_Vector[0], m_Vector[1] - Vect.m_Vector[1]);
139 
140  return VectResult;
141  }
142 
145  {
146  *this= *this - Vect;
147  return this;
148  }
149 
151  inline double operator ^ (const GLC_Vector2d &Vect) const
152  {
153  return m_Vector[0] * Vect.m_Vector[1] - m_Vector[1] * Vect.m_Vector[0];
154  }
155 
157  inline double operator * (const GLC_Vector2d &Vect) const
158  {
159  return m_Vector[0] * Vect.m_Vector[0] + m_Vector[1] * Vect.m_Vector[1];
160  }
161 
163  inline double operator / (const GLC_Vector2d &Vect) const
164  {
165  return m_Vector[0] / Vect.m_Vector[0] + m_Vector[1] / Vect.m_Vector[1];
166  }
167 
169  inline GLC_Vector2d operator * (double Scalaire) const
170  {
171  return GLC_Vector2d(m_Vector[0] * Scalaire, m_Vector[1] * Scalaire);;
172  }
173 
175  inline GLC_Vector2d operator / (double Scalaire) const
176  {
177  return GLC_Vector2d(m_Vector[0] / Scalaire, m_Vector[1] / Scalaire);;
178  }
179 
181  inline bool operator == (const GLC_Vector2d &Vect) const
182  {
183  bool bResult= (qAbs(m_Vector[0]) - qAbs(Vect.m_Vector[0])) < glc::EPSILON;
184  bResult= bResult && ((qAbs(m_Vector[1]) - qAbs(Vect.m_Vector[1])) < glc::EPSILON);
185 
186  return bResult;
187  }
188 
190  inline bool operator != (const GLC_Vector2d &Vect) const
191  {
192  return !(*this == Vect);
193  }
194 
196 
198 
200 
201 public:
203  inline GLC_Vector2d& setX(const double &dX)
204  {
205  m_Vector[0]= dX;
206  return *this;
207  }
208 
210  inline GLC_Vector2d& setY(const double &dY)
211  {
212  m_Vector[1]= dY;
213  return *this;
214  }
215 
217  inline GLC_Vector2d& setVect(const double &dX, const double &dY)
218  {
219  m_Vector[0]= dX;
220  m_Vector[1]= dY;
221  return *this;
222  }
223 
225  inline GLC_Vector2d& setVect(const GLC_Vector2d &Vect)
226  {
227  m_Vector[0]= Vect.m_Vector[0];
228  m_Vector[1]= Vect.m_Vector[1];
229  return *this;
230  }
231 
233  inline GLC_Vector2d& setLength(double);
234 
237  {return setLength(1.0);}
238 
240 
242 
244 
245 public:
247  inline double x(void) const
248  {return m_Vector[0];}
249 
251  inline double y(void) const
252  {return m_Vector[1];}
253 
255  inline const double *return_dVect(void) const
256  {return m_Vector;}
257 
259  inline bool isNull(void) const
260  {return (qAbs(m_Vector[0]) < glc::EPSILON) && (qAbs(m_Vector[1]) < glc::EPSILON);}
261 
263  inline QString toString() const
264  {return QString("[") + QString::number(m_Vector[0]) + QString(" , ") + QString::number(m_Vector[1]) + QString("]");}
265 
267  inline GLC_Vector2d perp() const
268  {return GLC_Vector2d(-m_Vector[1], m_Vector[0]);}
269 
271  inline double length() const
272  {return sqrt(m_Vector[0] * m_Vector[0] + m_Vector[1] * m_Vector[1]);}
273 
275 
277 //name Private attributes
279 private:
284  double m_Vector[2];
285 
286 }; //class GLC_Vector2d
287 
290 
291 inline GLC_Vector2d& GLC_Vector2d::setLength(double lenght)
292 {
293  const double currentLenght= sqrt( m_Vector[0] * m_Vector[0] + m_Vector[1] * m_Vector[1]);
294 
295  if (currentLenght != 0.0f)
296  {
297  const double Coef = lenght / currentLenght;
298 
299  m_Vector[0] = m_Vector[0] * Coef;
300  m_Vector[1] = m_Vector[1] * Coef;
301  }
302  return *this;
303 }
304 
305 #endif /*GLC_VECTOR2D_H_*/

©2005-2013 Laurent Ribon