1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
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 | /*!
|
---|
|
---|