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

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