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