Qt 4.8
qvector2d.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 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/qdebug.h>
46 #include <QtCore/qvariant.h>
47 #include <QtCore/qmath.h>
48 
50 
51 #ifndef QT_NO_VECTOR2D
52 
110 #ifndef QT_NO_VECTOR3D
111 
119 {
120  xp = vector.xp;
121  yp = vector.yp;
122 }
123 
124 #endif
125 
126 #ifndef QT_NO_VECTOR4D
127 
135 {
136  xp = vector.xp;
137  yp = vector.yp;
138 }
139 
140 #endif
141 
202 {
203  return qSqrt(xp * xp + yp * yp);
204 }
205 
213 {
214  return xp * xp + yp * yp;
215 }
216 
227 {
228  // Need some extra precision if the length is very small.
229  double len = double(xp) * double(xp) +
230  double(yp) * double(yp);
231  if (qFuzzyIsNull(len - 1.0f))
232  return *this;
233  else if (!qFuzzyIsNull(len))
234  return *this / qSqrt(len);
235  else
236  return QVector2D();
237 }
238 
246 {
247  // Need some extra precision if the length is very small.
248  double len = double(xp) * double(xp) +
249  double(yp) * double(yp);
250  if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
251  return;
252 
253  len = qSqrt(len);
254 
255  xp /= len;
256  yp /= len;
257 }
258 
312 {
313  return v1.xp * v2.xp + v1.yp * v2.yp;
314 }
315 
428 #ifndef QT_NO_VECTOR3D
429 
436 {
437  return QVector3D(xp, yp, 0.0f, 1);
438 }
439 
440 #endif
441 
442 #ifndef QT_NO_VECTOR4D
443 
450 {
451  return QVector4D(xp, yp, 0.0f, 0.0f, 1);
452 }
453 
454 #endif
455 
481 QVector2D::operator QVariant() const
482 {
483  return QVariant(QVariant::Vector2D, this);
484 }
485 
486 #ifndef QT_NO_DEBUG_STREAM
487 
488 QDebug operator<<(QDebug dbg, const QVector2D &vector)
489 {
490  dbg.nospace() << "QVector2D(" << vector.x() << ", " << vector.y() << ')';
491  return dbg.space();
492 }
493 
494 #endif
495 
496 #ifndef QT_NO_DATASTREAM
497 
512 {
513  stream << double(vector.x()) << double(vector.y());
514  return stream;
515 }
516 
531 {
532  double x, y;
533  stream >> x;
534  stream >> y;
535  vector.setX(qreal(x));
536  vector.setY(qreal(y));
537  return stream;
538 }
539 
540 #endif // QT_NO_DATASTREAM
541 
542 #endif // QT_NO_VECTOR2D
543 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
The QVector3D class represents a vector or vertex in 3D space.
Definition: qvector3d.h:60
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QVector2D normalized() const
Returns the normalized unit vector form of this vector.
Definition: qvector2d.cpp:226
void normalize()
Normalizes the currect vector in place.
Definition: qvector2d.cpp:245
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
float xp
Definition: qvector2d.h:121
The QVector4D class represents a vector or vertex in 4D space.
Definition: qvector4d.h:60
static qreal dotProduct(const QVector2D &v1, const QVector2D &v2)
Returns the dot product of v1 and v2.
Definition: qvector2d.cpp:311
friend class QVector4D
Definition: qvector2d.h:126
float yp
Definition: qvector4d.h:129
void setX(qreal x)
Sets the x coordinate of this point to the given x coordinate.
Definition: qvector2d.h:149
float yp
Definition: qvector3d.h:132
qreal length() const
Returns the length of the vector from the origin.
Definition: qvector2d.cpp:201
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static FILE * stream
QVector3D toVector3D() const
Returns the 3D form of this 2D vector, with the z coordinate set to zero.
Definition: qvector2d.cpp:435
QDataStream & operator>>(QDataStream &stream, QVector2D &vector)
Reads a 2D vector from the given stream into the given vector and returns a reference to the stream...
Definition: qvector2d.cpp:530
The QVector2D class represents a vector or vertex in 2D space.
Definition: qvector2d.h:60
qreal x() const
Returns the x coordinate of this point.
Definition: qvector2d.h:146
void setY(qreal y)
Sets the y coordinate of this point to the given y coordinate.
Definition: qvector2d.h:150
friend class QVector3D
Definition: qvector2d.h:125
qreal lengthSquared() const
Returns the squared length of the vector from the origin.
Definition: qvector2d.cpp:212
QDataStream & operator<<(QDataStream &stream, const QVector2D &vector)
Writes the given vector to the given stream and returns a reference to the stream.
Definition: qvector2d.cpp:511
qreal y() const
Returns the y coordinate of this point.
Definition: qvector2d.h:147
float xp
Definition: qvector3d.h:132
QVector4D toVector4D() const
Returns the 4D form of this 2D vector, with the z and w coordinates set to zero.
Definition: qvector2d.cpp:449
static Q_DECL_CONSTEXPR bool qFuzzyIsNull(double d)
Definition: qglobal.h:2043
float yp
Definition: qvector2d.h:121
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
float xp
Definition: qvector4d.h:129
qreal qSqrt(qreal v)
Definition: qmath.h:205
QVector2D()
Constructs a null vector, i.
Definition: qvector2d.h:131