qvector2d.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/math3d/qvector2d.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
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 The Qt Company. For licensing terms-
14** and conditions see https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39-
40#include "qvector2d.h"-
41#include "qvector3d.h"-
42#include "qvector4d.h"-
43#include <QtCore/qdatastream.h>-
44#include <QtCore/qdebug.h>-
45#include <QtCore/qvariant.h>-
46#include <QtCore/qmath.h>-
47-
48QT_BEGIN_NAMESPACE-
49-
50#ifndef QT_NO_VECTOR2D-
51-
52/*!-
53 \class QVector2D-
54 \brief The QVector2D class represents a vector or vertex in 2D space.-
55 \since 4.6-
56 \ingroup painting-
57 \ingroup painting-3D-
58 \inmodule QtGui-
59-
60 The QVector2D class can also be used to represent vertices in 2D space.-
61 We therefore do not need to provide a separate vertex class.-
62-
63 \sa QVector3D, QVector4D, QQuaternion-
64*/-
65-
66/*!-
67 \fn QVector2D::QVector2D()-
68-
69 Constructs a null vector, i.e. with coordinates (0, 0).-
70*/-
71-
72/*!-
73 \fn QVector2D::QVector2D(Qt::Initialization)-
74 \since 5.5-
75 \internal-
76-
77 Constructs a vector without initializing the contents.-
78*/-
79-
80/*!-
81 \fn QVector2D::QVector2D(float xpos, float ypos)-
82-
83 Constructs a vector with coordinates (\a xpos, \a ypos).-
84*/-
85-
86/*!-
87 \fn QVector2D::QVector2D(const QPoint& point)-
88-
89 Constructs a vector with x and y coordinates from a 2D \a point.-
90*/-
91-
92/*!-
93 \fn QVector2D::QVector2D(const QPointF& point)-
94-
95 Constructs a vector with x and y coordinates from a 2D \a point.-
96*/-
97-
98#ifndef QT_NO_VECTOR3D-
99-
100/*!-
101 Constructs a vector with x and y coordinates from a 3D \a vector.-
102 The z coordinate of \a vector is dropped.-
103-
104 \sa toVector3D()-
105*/-
106QVector2D::QVector2D(const QVector3D& vector)-
107{-
108 xp = vector.xp;-
109 yp = vector.yp;-
110}
never executed: end of block
0
111-
112#endif-
113-
114#ifndef QT_NO_VECTOR4D-
115-
116/*!-
117 Constructs a vector with x and y coordinates from a 3D \a vector.-
118 The z and w coordinates of \a vector are dropped.-
119-
120 \sa toVector4D()-
121*/-
122QVector2D::QVector2D(const QVector4D& vector)-
123{-
124 xp = vector.xp;-
125 yp = vector.yp;-
126}
never executed: end of block
0
127-
128#endif-
129-
130/*!-
131 \fn bool QVector2D::isNull() const-
132-
133 Returns \c true if the x and y coordinates are set to 0.0,-
134 otherwise returns \c false.-
135*/-
136-
137/*!-
138 \fn float QVector2D::x() const-
139-
140 Returns the x coordinate of this point.-
141-
142 \sa setX(), y()-
143*/-
144-
145/*!-
146 \fn float QVector2D::y() const-
147-
148 Returns the y coordinate of this point.-
149-
150 \sa setY(), x()-
151*/-
152-
153/*!-
154 \fn void QVector2D::setX(float x)-
155-
156 Sets the x coordinate of this point to the given \a x coordinate.-
157-
158 \sa x(), setY()-
159*/-
160-
161/*!-
162 \fn void QVector2D::setY(float y)-
163-
164 Sets the y coordinate of this point to the given \a y coordinate.-
165-
166 \sa y(), setX()-
167*/-
168-
169/*! \fn float &QVector2D::operator[](int i)-
170 \since 5.2-
171-
172 Returns the component of the vector at index position \a i-
173 as a modifiable reference.-
174-
175 \a i must be a valid index position in the vector (i.e., 0 <= \a i-
176 < 2).-
177*/-
178-
179/*! \fn float QVector2D::operator[](int i) const-
180 \since 5.2-
181-
182 Returns the component of the vector at index position \a i.-
183-
184 \a i must be a valid index position in the vector (i.e., 0 <= \a i-
185 < 2).-
186*/-
187-
188/*!-
189 Returns the length of the vector from the origin.-
190-
191 \sa lengthSquared(), normalized()-
192*/-
193float QVector2D::length() const-
194{-
195 // Need some extra precision if the length is very small.-
196 double len = double(xp) * double(xp) +-
197 double(yp) * double(yp);-
198 return float(std::sqrt(len));
never executed: return float(std::sqrt(len));
0
199}-
200-
201/*!-
202 Returns the squared length of the vector from the origin.-
203 This is equivalent to the dot product of the vector with itself.-
204-
205 \sa length(), dotProduct()-
206*/-
207float QVector2D::lengthSquared() const-
208{-
209 return xp * xp + yp * yp;
never executed: return xp * xp + yp * yp;
0
210}-
211-
212/*!-
213 Returns the normalized unit vector form of this vector.-
214-
215 If this vector is null, then a null vector is returned. If the length-
216 of the vector is very close to 1, then the vector will be returned as-is.-
217 Otherwise the normalized form of the vector of length 1 will be returned.-
218-
219 \sa length(), normalize()-
220*/-
221QVector2D QVector2D::normalized() const-
222{-
223 // Need some extra precision if the length is very small.-
224 double len = double(xp) * double(xp) +-
225 double(yp) * double(yp);-
226 if (qFuzzyIsNull(len - 1.0f)) {
qFuzzyIsNull(len - 1.0f)Description
TRUEnever evaluated
FALSEnever evaluated
0
227 return *this;
never executed: return *this;
0
228 } else if (!qFuzzyIsNull(len)) {
!qFuzzyIsNull(len)Description
TRUEnever evaluated
FALSEnever evaluated
0
229 double sqrtLen = std::sqrt(len);-
230 return QVector2D(float(double(xp) / sqrtLen), float(double(yp) / sqrtLen));
never executed: return QVector2D(float(double(xp) / sqrtLen), float(double(yp) / sqrtLen));
0
231 } else {-
232 return QVector2D();
never executed: return QVector2D();
0
233 }-
234}-
235-
236/*!-
237 Normalizes the currect vector in place. Nothing happens if this-
238 vector is a null vector or the length of the vector is very close to 1.-
239-
240 \sa length(), normalized()-
241*/-
242void QVector2D::normalize()-
243{-
244 // Need some extra precision if the length is very small.-
245 double len = double(xp) * double(xp) +-
246 double(yp) * double(yp);-
247 if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
qFuzzyIsNull(len - 1.0f)Description
TRUEnever evaluated
FALSEnever evaluated
qFuzzyIsNull(len)Description
TRUEnever evaluated
FALSEnever evaluated
0
248 return;
never executed: return;
0
249-
250 len = std::sqrt(len);-
251-
252 xp = float(double(xp) / len);-
253 yp = float(double(yp) / len);-
254}
never executed: end of block
0
255-
256/*!-
257 \since 5.1-
258-
259 Returns the distance from this vertex to a point defined by-
260 the vertex \a point.-
261-
262 \sa distanceToLine()-
263*/-
264float QVector2D::distanceToPoint(const QVector2D& point) const-
265{-
266 return (*this - point).length();
never executed: return (*this - point).length();
0
267}-
268-
269/*!-
270 \since 5.1-
271-
272 Returns the distance that this vertex is from a line defined-
273 by \a point and the unit vector \a direction.-
274-
275 If \a direction is a null vector, then it does not define a line.-
276 In that case, the distance from \a point to this vertex is returned.-
277-
278 \sa distanceToPoint()-
279*/-
280float QVector2D::distanceToLine-
281 (const QVector2D& point, const QVector2D& direction) const-
282{-
283 if (direction.isNull())
direction.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
0
284 return (*this - point).length();
never executed: return (*this - point).length();
0
285 QVector2D p = point + dotProduct(*this - point, direction) * direction;-
286 return (*this - p).length();
never executed: return (*this - p).length();
0
287}-
288-
289/*!-
290 \fn QVector2D &QVector2D::operator+=(const QVector2D &vector)-
291-
292 Adds the given \a vector to this vector and returns a reference to-
293 this vector.-
294-
295 \sa operator-=()-
296*/-
297-
298/*!-
299 \fn QVector2D &QVector2D::operator-=(const QVector2D &vector)-
300-
301 Subtracts the given \a vector from this vector and returns a reference to-
302 this vector.-
303-
304 \sa operator+=()-
305*/-
306-
307/*!-
308 \fn QVector2D &QVector2D::operator*=(float factor)-
309-
310 Multiplies this vector's coordinates by the given \a factor, and-
311 returns a reference to this vector.-
312-
313 \sa operator/=()-
314*/-
315-
316/*!-
317 \fn QVector2D &QVector2D::operator*=(const QVector2D &vector)-
318-
319 Multiplies the components of this vector by the corresponding-
320 components in \a vector.-
321*/-
322-
323/*!-
324 \fn QVector2D &QVector2D::operator/=(float divisor)-
325-
326 Divides this vector's coordinates by the given \a divisor, and-
327 returns a reference to this vector.-
328-
329 \sa operator*=()-
330*/-
331-
332/*!-
333 \fn QVector2D &QVector2D::operator/=(const QVector2D &vector)-
334 \since 5.5-
335-
336 Divides the components of this vector by the corresponding-
337 components in \a vector.-
338-
339 \sa operator*=()-
340*/-
341-
342/*!-
343 Returns the dot product of \a v1 and \a v2.-
344*/-
345float QVector2D::dotProduct(const QVector2D& v1, const QVector2D& v2)-
346{-
347 return v1.xp * v2.xp + v1.yp * v2.yp;
never executed: return v1.xp * v2.xp + v1.yp * v2.yp;
0
348}-
349-
350/*!-
351 \fn bool operator==(const QVector2D &v1, const QVector2D &v2)-
352 \relates QVector2D-
353-
354 Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false.-
355 This operator uses an exact floating-point comparison.-
356*/-
357-
358/*!-
359 \fn bool operator!=(const QVector2D &v1, const QVector2D &v2)-
360 \relates QVector2D-
361-
362 Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false.-
363 This operator uses an exact floating-point comparison.-
364*/-
365-
366/*!-
367 \fn const QVector2D operator+(const QVector2D &v1, const QVector2D &v2)-
368 \relates QVector2D-
369-
370 Returns a QVector2D object that is the sum of the given vectors, \a v1-
371 and \a v2; each component is added separately.-
372-
373 \sa QVector2D::operator+=()-
374*/-
375-
376/*!-
377 \fn const QVector2D operator-(const QVector2D &v1, const QVector2D &v2)-
378 \relates QVector2D-
379-
380 Returns a QVector2D object that is formed by subtracting \a v2 from \a v1;-
381 each component is subtracted separately.-
382-
383 \sa QVector2D::operator-=()-
384*/-
385-
386/*!-
387 \fn const QVector2D operator*(float factor, const QVector2D &vector)-
388 \relates QVector2D-
389-
390 Returns a copy of the given \a vector, multiplied by the given \a factor.-
391-
392 \sa QVector2D::operator*=()-
393*/-
394-
395/*!-
396 \fn const QVector2D operator*(const QVector2D &vector, float factor)-
397 \relates QVector2D-
398-
399 Returns a copy of the given \a vector, multiplied by the given \a factor.-
400-
401 \sa QVector2D::operator*=()-
402*/-
403-
404/*!-
405 \fn const QVector2D operator*(const QVector2D &v1, const QVector2D &v2)-
406 \relates QVector2D-
407-
408 Multiplies the components of \a v1 by the corresponding-
409 components in \a v2.-
410*/-
411-
412/*!-
413 \fn const QVector2D operator-(const QVector2D &vector)-
414 \relates QVector2D-
415 \overload-
416-
417 Returns a QVector2D object that is formed by changing the sign of-
418 the components of the given \a vector.-
419-
420 Equivalent to \c {QVector2D(0,0) - vector}.-
421*/-
422-
423/*!-
424 \fn const QVector2D operator/(const QVector2D &vector, float divisor)-
425 \relates QVector2D-
426-
427 Returns the QVector2D object formed by dividing all three components of-
428 the given \a vector by the given \a divisor.-
429-
430 \sa QVector2D::operator/=()-
431*/-
432-
433/*!-
434 \fn const QVector2D operator/(const QVector2D &vector, const QVector2D &divisor)-
435 \relates QVector2D-
436 \since 5.5-
437-
438 Returns the QVector2D object formed by dividing components of the given-
439 \a vector by a respective components of the given \a divisor.-
440-
441 \sa QVector2D::operator/=()-
442*/-
443-
444/*!-
445 \fn bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2)-
446 \relates QVector2D-
447-
448 Returns \c true if \a v1 and \a v2 are equal, allowing for a small-
449 fuzziness factor for floating-point comparisons; false otherwise.-
450*/-
451-
452#ifndef QT_NO_VECTOR3D-
453-
454/*!-
455 Returns the 3D form of this 2D vector, with the z coordinate set to zero.-
456-
457 \sa toVector4D(), toPoint()-
458*/-
459QVector3D QVector2D::toVector3D() const-
460{-
461 return QVector3D(xp, yp, 0.0f);
never executed: return QVector3D(xp, yp, 0.0f);
0
462}-
463-
464#endif-
465-
466#ifndef QT_NO_VECTOR4D-
467-
468/*!-
469 Returns the 4D form of this 2D vector, with the z and w coordinates set to zero.-
470-
471 \sa toVector3D(), toPoint()-
472*/-
473QVector4D QVector2D::toVector4D() const-
474{-
475 return QVector4D(xp, yp, 0.0f, 0.0f);
never executed: return QVector4D(xp, yp, 0.0f, 0.0f);
0
476}-
477-
478#endif-
479-
480/*!-
481 \fn QPoint QVector2D::toPoint() const-
482-
483 Returns the QPoint form of this 2D vector.-
484-
485 \sa toPointF(), toVector3D()-
486*/-
487-
488/*!-
489 \fn QPointF QVector2D::toPointF() const-
490-
491 Returns the QPointF form of this 2D vector.-
492-
493 \sa toPoint(), toVector3D()-
494*/-
495-
496/*!-
497 Returns the 2D vector as a QVariant.-
498*/-
499QVector2D::operator QVariant() const-
500{-
501 return QVariant(QVariant::Vector2D, this);
never executed: return QVariant(QVariant::Vector2D, this);
0
502}-
503-
504#ifndef QT_NO_DEBUG_STREAM-
505-
506QDebug operator<<(QDebug dbg, const QVector2D &vector)-
507{-
508 QDebugStateSaver saver(dbg);-
509 dbg.nospace() << "QVector2D(" << vector.x() << ", " << vector.y() << ')';-
510 return dbg;
never executed: return dbg;
0
511}-
512-
513#endif-
514-
515#ifndef QT_NO_DATASTREAM-
516-
517/*!-
518 \fn QDataStream &operator<<(QDataStream &stream, const QVector2D &vector)-
519 \relates QVector2D-
520-
521 Writes the given \a vector to the given \a stream and returns a-
522 reference to the stream.-
523-
524 \sa {Serializing Qt Data Types}-
525*/-
526-
527QDataStream &operator<<(QDataStream &stream, const QVector2D &vector)-
528{-
529 stream << vector.x() << vector.y();-
530 return stream;
never executed: return stream;
0
531}-
532-
533/*!-
534 \fn QDataStream &operator>>(QDataStream &stream, QVector2D &vector)-
535 \relates QVector2D-
536-
537 Reads a 2D vector from the given \a stream into the given \a vector-
538 and returns a reference to the stream.-
539-
540 \sa {Serializing Qt Data Types}-
541*/-
542-
543QDataStream &operator>>(QDataStream &stream, QVector2D &vector)-
544{-
545 float x, y;-
546 stream >> x;-
547 stream >> y;-
548 vector.setX(x);-
549 vector.setY(y);-
550 return stream;
never executed: return stream;
0
551}-
552-
553#endif // QT_NO_DATASTREAM-
554-
555#endif // QT_NO_VECTOR2D-
556-
557QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9