1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtGui module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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>
52 #ifndef QT_NO_VECTOR2D
56 \brief The QVector2D class represents a vector or vertex in 2D space.
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.
65 \sa QVector3D, QVector4D, QQuaternion
69 \fn QVector2D::QVector2D()
71 Constructs a null vector, i.e. with coordinates (0, 0, 0).
75 \fn QVector2D::QVector2D(float xpos, float ypos)
77 Constructs a vector with coordinates (\a xpos, \a ypos).
81 \fn QVector2D::QVector2D(const QPoint& point)
83 Constructs a vector with x and y coordinates from a 2D \a point.
87 \fn QVector2D::QVector2D(const QPointF& point)
89 Constructs a vector with x and y coordinates from a 2D \a point.
92 #ifndef QT_NO_VECTOR3D
95 Constructs a vector with x and y coordinates from a 3D \a vector.
96 The z coordinate of \a vector is dropped.
100 QVector2D::QVector2D(const QVector3D& vector)
108 #ifndef QT_NO_VECTOR4D
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.
116 QVector2D::QVector2D(const QVector4D& vector)
125 \fn bool QVector2D::isNull() const
127 Returns true if the x and y coordinates are set to 0.0,
128 otherwise returns false.
132 \fn float QVector2D::x() const
134 Returns the x coordinate of this point.
140 \fn float QVector2D::y() const
142 Returns the y coordinate of this point.
148 \fn void QVector2D::setX(float x)
150 Sets the x coordinate of this point to the given \a x coordinate.
156 \fn void QVector2D::setY(float y)
158 Sets the y coordinate of this point to the given \a y coordinate.
164 Returns the length of the vector from the origin.
166 \sa lengthSquared(), normalized()
168 float QVector2D::length() const
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));
177 Returns the squared length of the vector from the origin.
178 This is equivalent to the dot product of the vector with itself.
180 \sa length(), dotProduct()
182 float QVector2D::lengthSquared() const
184 return xp * xp + yp * yp;
188 Returns the normalized unit vector form of this vector.
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.
194 \sa length(), normalize()
196 QVector2D QVector2D::normalized() const
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)) {
203 } else if (!qFuzzyIsNull(len)) {
204 double sqrtLen = sqrt(len);
205 return QVector2D(float(double(xp) / sqrtLen), float(double(yp) / sqrtLen));
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.
215 \sa length(), normalized()
217 void QVector2D::normalize()
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))
227 xp = float(double(xp) / len);
228 yp = float(double(yp) / len);
232 \fn QVector2D &QVector2D::operator+=(const QVector2D &vector)
234 Adds the given \a vector to this vector and returns a reference to
241 \fn QVector2D &QVector2D::operator-=(const QVector2D &vector)
243 Subtracts the given \a vector from this vector and returns a reference to
250 \fn QVector2D &QVector2D::operator*=(float factor)
252 Multiplies this vector's coordinates by the given \a factor, and
253 returns a reference to this vector.
259 \fn QVector2D &QVector2D::operator*=(const QVector2D &vector)
261 Multiplies the components of this vector by the corresponding
262 components in \a vector.
266 \fn QVector2D &QVector2D::operator/=(float divisor)
268 Divides this vector's coordinates by the given \a divisor, and
269 returns a reference to this vector.
275 Returns the dot product of \a v1 and \a v2.
277 float QVector2D::dotProduct(const QVector2D& v1, const QVector2D& v2)
279 return v1.xp * v2.xp + v1.yp * v2.yp;
283 \fn bool operator==(const QVector2D &v1, const QVector2D &v2)
286 Returns true if \a v1 is equal to \a v2; otherwise returns false.
287 This operator uses an exact floating-point comparison.
291 \fn bool operator!=(const QVector2D &v1, const QVector2D &v2)
294 Returns true if \a v1 is not equal to \a v2; otherwise returns false.
295 This operator uses an exact floating-point comparison.
299 \fn const QVector2D operator+(const QVector2D &v1, const QVector2D &v2)
302 Returns a QVector2D object that is the sum of the given vectors, \a v1
303 and \a v2; each component is added separately.
305 \sa QVector2D::operator+=()
309 \fn const QVector2D operator-(const QVector2D &v1, const QVector2D &v2)
312 Returns a QVector2D object that is formed by subtracting \a v2 from \a v1;
313 each component is subtracted separately.
315 \sa QVector2D::operator-=()
319 \fn const QVector2D operator*(float factor, const QVector2D &vector)
322 Returns a copy of the given \a vector, multiplied by the given \a factor.
324 \sa QVector2D::operator*=()
328 \fn const QVector2D operator*(const QVector2D &vector, float factor)
331 Returns a copy of the given \a vector, multiplied by the given \a factor.
333 \sa QVector2D::operator*=()
337 \fn const QVector2D operator*(const QVector2D &v1, const QVector2D &v2)
340 Multiplies the components of \a v1 by the corresponding
345 \fn const QVector2D operator-(const QVector2D &vector)
349 Returns a QVector2D object that is formed by changing the sign of
350 the components of the given \a vector.
352 Equivalent to \c {QVector2D(0,0) - vector}.
356 \fn const QVector2D operator/(const QVector2D &vector, float divisor)
359 Returns the QVector2D object formed by dividing all three components of
360 the given \a vector by the given \a divisor.
362 \sa QVector2D::operator/=()
366 \fn bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2)
369 Returns true if \a v1 and \a v2 are equal, allowing for a small
370 fuzziness factor for floating-point comparisons; false otherwise.
373 #ifndef QT_NO_VECTOR3D
376 Returns the 3D form of this 2D vector, with the z coordinate set to zero.
378 \sa toVector4D(), toPoint()
380 QVector3D QVector2D::toVector3D() const
382 return QVector3D(xp, yp, 0.0f);
387 #ifndef QT_NO_VECTOR4D
390 Returns the 4D form of this 2D vector, with the z and w coordinates set to zero.
392 \sa toVector3D(), toPoint()
394 QVector4D QVector2D::toVector4D() const
396 return QVector4D(xp, yp, 0.0f, 0.0f);
402 \fn QPoint QVector2D::toPoint() const
404 Returns the QPoint form of this 2D vector.
406 \sa toPointF(), toVector3D()
410 \fn QPointF QVector2D::toPointF() const
412 Returns the QPointF form of this 2D vector.
414 \sa toPoint(), toVector3D()
418 Returns the 2D vector as a QVariant.
420 QVector2D::operator QVariant() const
422 return QVariant(QVariant::Vector2D, this);
425 #ifndef QT_NO_DEBUG_STREAM
427 QDebug operator<<(QDebug dbg, const QVector2D &vector)
429 dbg.nospace() << "QVector2D(" << vector.x() << ", " << vector.y() << ')';
435 #ifndef QT_NO_DATASTREAM
438 \fn QDataStream &operator<<(QDataStream &stream, const QVector2D &vector)
441 Writes the given \a vector to the given \a stream and returns a
442 reference to the stream.
444 \sa {Serializing Qt Data Types}
447 QDataStream &operator<<(QDataStream &stream, const QVector2D &vector)
449 stream << vector.x() << vector.y();
454 \fn QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
457 Reads a 2D vector from the given \a stream into the given \a vector
458 and returns a reference to the stream.
460 \sa {Serializing Qt Data Types}
463 QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
473 #endif // QT_NO_DATASTREAM
475 #endif // QT_NO_VECTOR2D