Merge "Merge branch 'newdocs'" into refs/staging/master
[profile/ivi/qtbase.git] / src / gui / math3d / qvector2d.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qvector2d.h"
43 #include "qvector3d.h"
44 #include "qvector4d.h"
45 #include <QtCore/qdatastream.h>
46 #include <QtCore/qdebug.h>
47 #include <QtCore/qvariant.h>
48 #include <QtCore/qmath.h>
49
50 QT_BEGIN_NAMESPACE
51
52 #ifndef QT_NO_VECTOR2D
53
54 /*!
55     \class QVector2D
56     \brief The QVector2D class represents a vector or vertex in 2D space.
57     \since 4.6
58     \ingroup painting
59     \ingroup painting-3D
60     \inmodule QtGui
61
62     The QVector2D class can also be used to represent vertices in 2D space.
63     We therefore do not need to provide a separate vertex class.
64
65     \sa QVector3D, QVector4D, QQuaternion
66 */
67
68 /*!
69     \fn QVector2D::QVector2D()
70
71     Constructs a null vector, i.e. with coordinates (0, 0, 0).
72 */
73
74 /*!
75     \fn QVector2D::QVector2D(float xpos, float ypos)
76
77     Constructs a vector with coordinates (\a xpos, \a ypos).
78 */
79
80 /*!
81     \fn QVector2D::QVector2D(const QPoint& point)
82
83     Constructs a vector with x and y coordinates from a 2D \a point.
84 */
85
86 /*!
87     \fn QVector2D::QVector2D(const QPointF& point)
88
89     Constructs a vector with x and y coordinates from a 2D \a point.
90 */
91
92 #ifndef QT_NO_VECTOR3D
93
94 /*!
95     Constructs a vector with x and y coordinates from a 3D \a vector.
96     The z coordinate of \a vector is dropped.
97
98     \sa toVector3D()
99 */
100 QVector2D::QVector2D(const QVector3D& vector)
101 {
102     xp = vector.xp;
103     yp = vector.yp;
104 }
105
106 #endif
107
108 #ifndef QT_NO_VECTOR4D
109
110 /*!
111     Constructs a vector with x and y coordinates from a 3D \a vector.
112     The z and w coordinates of \a vector are dropped.
113
114     \sa toVector4D()
115 */
116 QVector2D::QVector2D(const QVector4D& vector)
117 {
118     xp = vector.xp;
119     yp = vector.yp;
120 }
121
122 #endif
123
124 /*!
125     \fn bool QVector2D::isNull() const
126
127     Returns true if the x and y coordinates are set to 0.0,
128     otherwise returns false.
129 */
130
131 /*!
132     \fn float QVector2D::x() const
133
134     Returns the x coordinate of this point.
135
136     \sa setX(), y()
137 */
138
139 /*!
140     \fn float QVector2D::y() const
141
142     Returns the y coordinate of this point.
143
144     \sa setY(), x()
145 */
146
147 /*!
148     \fn void QVector2D::setX(float x)
149
150     Sets the x coordinate of this point to the given \a x coordinate.
151
152     \sa x(), setY()
153 */
154
155 /*!
156     \fn void QVector2D::setY(float y)
157
158     Sets the y coordinate of this point to the given \a y coordinate.
159
160     \sa y(), setX()
161 */
162
163 /*!
164     Returns the length of the vector from the origin.
165
166     \sa lengthSquared(), normalized()
167 */
168 float QVector2D::length() const
169 {
170     // Need some extra precision if the length is very small.
171     double len = double(xp) * double(xp) +
172                  double(yp) * double(yp);
173     return float(sqrt(len));
174 }
175
176 /*!
177     Returns the squared length of the vector from the origin.
178     This is equivalent to the dot product of the vector with itself.
179
180     \sa length(), dotProduct()
181 */
182 float QVector2D::lengthSquared() const
183 {
184     return xp * xp + yp * yp;
185 }
186
187 /*!
188     Returns the normalized unit vector form of this vector.
189
190     If this vector is null, then a null vector is returned.  If the length
191     of the vector is very close to 1, then the vector will be returned as-is.
192     Otherwise the normalized form of the vector of length 1 will be returned.
193
194     \sa length(), normalize()
195 */
196 QVector2D QVector2D::normalized() const
197 {
198     // Need some extra precision if the length is very small.
199     double len = double(xp) * double(xp) +
200                  double(yp) * double(yp);
201     if (qFuzzyIsNull(len - 1.0f)) {
202         return *this;
203     } else if (!qFuzzyIsNull(len)) {
204         double sqrtLen = sqrt(len);
205         return QVector2D(float(double(xp) / sqrtLen), float(double(yp) / sqrtLen));
206     } else {
207         return QVector2D();
208     }
209 }
210
211 /*!
212     Normalizes the currect vector in place.  Nothing happens if this
213     vector is a null vector or the length of the vector is very close to 1.
214
215     \sa length(), normalized()
216 */
217 void QVector2D::normalize()
218 {
219     // Need some extra precision if the length is very small.
220     double len = double(xp) * double(xp) +
221                  double(yp) * double(yp);
222     if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
223         return;
224
225     len = sqrt(len);
226
227     xp = float(double(xp) / len);
228     yp = float(double(yp) / len);
229 }
230
231 /*!
232     \fn QVector2D &QVector2D::operator+=(const QVector2D &vector)
233
234     Adds the given \a vector to this vector and returns a reference to
235     this vector.
236
237     \sa operator-=()
238 */
239
240 /*!
241     \fn QVector2D &QVector2D::operator-=(const QVector2D &vector)
242
243     Subtracts the given \a vector from this vector and returns a reference to
244     this vector.
245
246     \sa operator+=()
247 */
248
249 /*!
250     \fn QVector2D &QVector2D::operator*=(float factor)
251
252     Multiplies this vector's coordinates by the given \a factor, and
253     returns a reference to this vector.
254
255     \sa operator/=()
256 */
257
258 /*!
259     \fn QVector2D &QVector2D::operator*=(const QVector2D &vector)
260
261     Multiplies the components of this vector by the corresponding
262     components in \a vector.
263 */
264
265 /*!
266     \fn QVector2D &QVector2D::operator/=(float divisor)
267
268     Divides this vector's coordinates by the given \a divisor, and
269     returns a reference to this vector.
270
271     \sa operator*=()
272 */
273
274 /*!
275     Returns the dot product of \a v1 and \a v2.
276 */
277 float QVector2D::dotProduct(const QVector2D& v1, const QVector2D& v2)
278 {
279     return v1.xp * v2.xp + v1.yp * v2.yp;
280 }
281
282 /*!
283     \fn bool operator==(const QVector2D &v1, const QVector2D &v2)
284     \relates QVector2D
285
286     Returns true if \a v1 is equal to \a v2; otherwise returns false.
287     This operator uses an exact floating-point comparison.
288 */
289
290 /*!
291     \fn bool operator!=(const QVector2D &v1, const QVector2D &v2)
292     \relates QVector2D
293
294     Returns true if \a v1 is not equal to \a v2; otherwise returns false.
295     This operator uses an exact floating-point comparison.
296 */
297
298 /*!
299     \fn const QVector2D operator+(const QVector2D &v1, const QVector2D &v2)
300     \relates QVector2D
301
302     Returns a QVector2D object that is the sum of the given vectors, \a v1
303     and \a v2; each component is added separately.
304
305     \sa QVector2D::operator+=()
306 */
307
308 /*!
309     \fn const QVector2D operator-(const QVector2D &v1, const QVector2D &v2)
310     \relates QVector2D
311
312     Returns a QVector2D object that is formed by subtracting \a v2 from \a v1;
313     each component is subtracted separately.
314
315     \sa QVector2D::operator-=()
316 */
317
318 /*!
319     \fn const QVector2D operator*(float factor, const QVector2D &vector)
320     \relates QVector2D
321
322     Returns a copy of the given \a vector,  multiplied by the given \a factor.
323
324     \sa QVector2D::operator*=()
325 */
326
327 /*!
328     \fn const QVector2D operator*(const QVector2D &vector, float factor)
329     \relates QVector2D
330
331     Returns a copy of the given \a vector,  multiplied by the given \a factor.
332
333     \sa QVector2D::operator*=()
334 */
335
336 /*!
337     \fn const QVector2D operator*(const QVector2D &v1, const QVector2D &v2)
338     \relates QVector2D
339
340     Multiplies the components of \a v1 by the corresponding
341     components in \a v2.
342 */
343
344 /*!
345     \fn const QVector2D operator-(const QVector2D &vector)
346     \relates QVector2D
347     \overload
348
349     Returns a QVector2D object that is formed by changing the sign of
350     the components of the given \a vector.
351
352     Equivalent to \c {QVector2D(0,0) - vector}.
353 */
354
355 /*!
356     \fn const QVector2D operator/(const QVector2D &vector, float divisor)
357     \relates QVector2D
358
359     Returns the QVector2D object formed by dividing all three components of
360     the given \a vector by the given \a divisor.
361
362     \sa QVector2D::operator/=()
363 */
364
365 /*!
366     \fn bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2)
367     \relates QVector2D
368
369     Returns true if \a v1 and \a v2 are equal, allowing for a small
370     fuzziness factor for floating-point comparisons; false otherwise.
371 */
372
373 #ifndef QT_NO_VECTOR3D
374
375 /*!
376     Returns the 3D form of this 2D vector, with the z coordinate set to zero.
377
378     \sa toVector4D(), toPoint()
379 */
380 QVector3D QVector2D::toVector3D() const
381 {
382     return QVector3D(xp, yp, 0.0f);
383 }
384
385 #endif
386
387 #ifndef QT_NO_VECTOR4D
388
389 /*!
390     Returns the 4D form of this 2D vector, with the z and w coordinates set to zero.
391
392     \sa toVector3D(), toPoint()
393 */
394 QVector4D QVector2D::toVector4D() const
395 {
396     return QVector4D(xp, yp, 0.0f, 0.0f);
397 }
398
399 #endif
400
401 /*!
402     \fn QPoint QVector2D::toPoint() const
403
404     Returns the QPoint form of this 2D vector.
405
406     \sa toPointF(), toVector3D()
407 */
408
409 /*!
410     \fn QPointF QVector2D::toPointF() const
411
412     Returns the QPointF form of this 2D vector.
413
414     \sa toPoint(), toVector3D()
415 */
416
417 /*!
418     Returns the 2D vector as a QVariant.
419 */
420 QVector2D::operator QVariant() const
421 {
422     return QVariant(QVariant::Vector2D, this);
423 }
424
425 #ifndef QT_NO_DEBUG_STREAM
426
427 QDebug operator<<(QDebug dbg, const QVector2D &vector)
428 {
429     dbg.nospace() << "QVector2D(" << vector.x() << ", " << vector.y() << ')';
430     return dbg.space();
431 }
432
433 #endif
434
435 #ifndef QT_NO_DATASTREAM
436
437 /*!
438     \fn QDataStream &operator<<(QDataStream &stream, const QVector2D &vector)
439     \relates QVector2D
440
441     Writes the given \a vector to the given \a stream and returns a
442     reference to the stream.
443
444     \sa {Serializing Qt Data Types}
445 */
446
447 QDataStream &operator<<(QDataStream &stream, const QVector2D &vector)
448 {
449     stream << vector.x() << vector.y();
450     return stream;
451 }
452
453 /*!
454     \fn QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
455     \relates QVector2D
456
457     Reads a 2D vector from the given \a stream into the given \a vector
458     and returns a reference to the stream.
459
460     \sa {Serializing Qt Data Types}
461 */
462
463 QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
464 {
465     float x, y;
466     stream >> x;
467     stream >> y;
468     vector.setX(x);
469     vector.setY(y);
470     return stream;
471 }
472
473 #endif // QT_NO_DATASTREAM
474
475 #endif // QT_NO_VECTOR2D
476
477 QT_END_NAMESPACE