Merge "Merge branch 'newdocs'" into refs/staging/master
[profile/ivi/qtbase.git] / src / gui / math3d / qvector2d.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 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.
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 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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QVECTOR2D_H
43 #define QVECTOR2D_H
44
45 #include <QtCore/qpoint.h>
46 #include <QtCore/qmetatype.h>
47
48 QT_BEGIN_HEADER
49
50 QT_BEGIN_NAMESPACE
51
52
53 class QVector3D;
54 class QVector4D;
55 class QVariant;
56
57 #ifndef QT_NO_VECTOR2D
58
59 class Q_GUI_EXPORT QVector2D
60 {
61 public:
62     QVector2D();
63     QVector2D(float xpos, float ypos);
64     explicit QVector2D(const QPoint& point);
65     explicit QVector2D(const QPointF& point);
66 #ifndef QT_NO_VECTOR3D
67     explicit QVector2D(const QVector3D& vector);
68 #endif
69 #ifndef QT_NO_VECTOR4D
70     explicit QVector2D(const QVector4D& vector);
71 #endif
72
73     bool isNull() const;
74
75     float x() const;
76     float y() const;
77
78     void setX(float x);
79     void setY(float y);
80
81     float length() const;
82     float lengthSquared() const;
83
84     QVector2D normalized() const;
85     void normalize();
86
87     QVector2D &operator+=(const QVector2D &vector);
88     QVector2D &operator-=(const QVector2D &vector);
89     QVector2D &operator*=(float factor);
90     QVector2D &operator*=(const QVector2D &vector);
91     QVector2D &operator/=(float divisor);
92
93     static float dotProduct(const QVector2D& v1, const QVector2D& v2);
94
95     friend inline bool operator==(const QVector2D &v1, const QVector2D &v2);
96     friend inline bool operator!=(const QVector2D &v1, const QVector2D &v2);
97     friend inline const QVector2D operator+(const QVector2D &v1, const QVector2D &v2);
98     friend inline const QVector2D operator-(const QVector2D &v1, const QVector2D &v2);
99     friend inline const QVector2D operator*(float factor, const QVector2D &vector);
100     friend inline const QVector2D operator*(const QVector2D &vector, float factor);
101     friend inline const QVector2D operator*(const QVector2D &v1, const QVector2D &v2);
102     friend inline const QVector2D operator-(const QVector2D &vector);
103     friend inline const QVector2D operator/(const QVector2D &vector, float divisor);
104
105     friend inline bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2);
106
107 #ifndef QT_NO_VECTOR3D
108     QVector3D toVector3D() const;
109 #endif
110 #ifndef QT_NO_VECTOR4D
111     QVector4D toVector4D() const;
112 #endif
113
114     QPoint toPoint() const;
115     QPointF toPointF() const;
116
117     operator QVariant() const;
118
119 private:
120     float xp, yp;
121
122     friend class QVector3D;
123     friend class QVector4D;
124 };
125
126 Q_DECLARE_TYPEINFO(QVector2D, Q_MOVABLE_TYPE);
127
128 inline QVector2D::QVector2D() : xp(0.0f), yp(0.0f) {}
129
130 inline QVector2D::QVector2D(float xpos, float ypos) : xp(xpos), yp(ypos) {}
131
132 inline QVector2D::QVector2D(const QPoint& point) : xp(point.x()), yp(point.y()) {}
133
134 inline QVector2D::QVector2D(const QPointF& point) : xp(point.x()), yp(point.y()) {}
135
136 inline bool QVector2D::isNull() const
137 {
138     return qIsNull(xp) && qIsNull(yp);
139 }
140
141 inline float QVector2D::x() const { return xp; }
142 inline float QVector2D::y() const { return yp; }
143
144 inline void QVector2D::setX(float aX) { xp = aX; }
145 inline void QVector2D::setY(float aY) { yp = aY; }
146
147 inline QVector2D &QVector2D::operator+=(const QVector2D &vector)
148 {
149     xp += vector.xp;
150     yp += vector.yp;
151     return *this;
152 }
153
154 inline QVector2D &QVector2D::operator-=(const QVector2D &vector)
155 {
156     xp -= vector.xp;
157     yp -= vector.yp;
158     return *this;
159 }
160
161 inline QVector2D &QVector2D::operator*=(float factor)
162 {
163     xp *= factor;
164     yp *= factor;
165     return *this;
166 }
167
168 inline QVector2D &QVector2D::operator*=(const QVector2D &vector)
169 {
170     xp *= vector.xp;
171     yp *= vector.yp;
172     return *this;
173 }
174
175 inline QVector2D &QVector2D::operator/=(float divisor)
176 {
177     xp /= divisor;
178     yp /= divisor;
179     return *this;
180 }
181
182 inline bool operator==(const QVector2D &v1, const QVector2D &v2)
183 {
184     return v1.xp == v2.xp && v1.yp == v2.yp;
185 }
186
187 inline bool operator!=(const QVector2D &v1, const QVector2D &v2)
188 {
189     return v1.xp != v2.xp || v1.yp != v2.yp;
190 }
191
192 inline const QVector2D operator+(const QVector2D &v1, const QVector2D &v2)
193 {
194     return QVector2D(v1.xp + v2.xp, v1.yp + v2.yp);
195 }
196
197 inline const QVector2D operator-(const QVector2D &v1, const QVector2D &v2)
198 {
199     return QVector2D(v1.xp - v2.xp, v1.yp - v2.yp);
200 }
201
202 inline const QVector2D operator*(float factor, const QVector2D &vector)
203 {
204     return QVector2D(vector.xp * factor, vector.yp * factor);
205 }
206
207 inline const QVector2D operator*(const QVector2D &vector, float factor)
208 {
209     return QVector2D(vector.xp * factor, vector.yp * factor);
210 }
211
212 inline const QVector2D operator*(const QVector2D &v1, const QVector2D &v2)
213 {
214     return QVector2D(v1.xp * v2.xp, v1.yp * v2.yp);
215 }
216
217 inline const QVector2D operator-(const QVector2D &vector)
218 {
219     return QVector2D(-vector.xp, -vector.yp);
220 }
221
222 inline const QVector2D operator/(const QVector2D &vector, float divisor)
223 {
224     return QVector2D(vector.xp / divisor, vector.yp / divisor);
225 }
226
227 inline bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2)
228 {
229     return qFuzzyCompare(v1.xp, v2.xp) && qFuzzyCompare(v1.yp, v2.yp);
230 }
231
232 inline QPoint QVector2D::toPoint() const
233 {
234     return QPoint(qRound(xp), qRound(yp));
235 }
236
237 inline QPointF QVector2D::toPointF() const
238 {
239     return QPointF(qreal(xp), qreal(yp));
240 }
241
242 #ifndef QT_NO_DEBUG_STREAM
243 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector2D &vector);
244 #endif
245
246 #ifndef QT_NO_DATASTREAM
247 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector2D &);
248 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector2D &);
249 #endif
250
251 #endif
252
253 QT_END_NAMESPACE
254
255 QT_END_HEADER
256
257 #endif