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#ifndef QVECTOR2D_H
41#define QVECTOR2D_H
42
43#include <QtGui/qtguiglobal.h>
44#include <QtCore/qpoint.h>
45#include <QtCore/qmetatype.h>
46
47QT_BEGIN_NAMESPACE
48
49
50class QVector3D;
51class QVector4D;
52class QVariant;
53
54#ifndef QT_NO_VECTOR2D
55
56class Q_GUI_EXPORT QVector2D
57{
58public:
59 Q_DECL_CONSTEXPR QVector2D();
60 explicit QVector2D(Qt::Initialization) {}
61 Q_DECL_CONSTEXPR QVector2D(float xpos, float ypos);
62 Q_DECL_CONSTEXPR explicit QVector2D(const QPoint& point);
63 Q_DECL_CONSTEXPR explicit QVector2D(const QPointF& point);
64#ifndef QT_NO_VECTOR3D
65 explicit QVector2D(const QVector3D& vector);
66#endif
67#ifndef QT_NO_VECTOR4D
68 explicit QVector2D(const QVector4D& vector);
69#endif
70
71 bool isNull() const;
72
73 Q_DECL_CONSTEXPR float x() const;
74 Q_DECL_CONSTEXPR float y() const;
75
76 void setX(float x);
77 void setY(float y);
78
79 float &operator[](int i);
80 float operator[](int i) const;
81
82 float length() const;
83 float lengthSquared() const; //In Qt 6 convert to inline and constexpr
84
85 Q_REQUIRED_RESULT QVector2D normalized() const;
86 void normalize();
87
88 float distanceToPoint(const QVector2D &point) const;
89 float distanceToLine(const QVector2D& point, const QVector2D& direction) const;
90
91 QVector2D &operator+=(const QVector2D &vector);
92 QVector2D &operator-=(const QVector2D &vector);
93 QVector2D &operator*=(float factor);
94 QVector2D &operator*=(const QVector2D &vector);
95 QVector2D &operator/=(float divisor);
96 inline QVector2D &operator/=(const QVector2D &vector);
97
98 static float dotProduct(const QVector2D& v1, const QVector2D& v2); //In Qt 6 convert to inline and constexpr
99
100 Q_DECL_CONSTEXPR friend inline bool operator==(const QVector2D &v1, const QVector2D &v2);
101 Q_DECL_CONSTEXPR friend inline bool operator!=(const QVector2D &v1, const QVector2D &v2);
102 Q_DECL_CONSTEXPR friend inline const QVector2D operator+(const QVector2D &v1, const QVector2D &v2);
103 Q_DECL_CONSTEXPR friend inline const QVector2D operator-(const QVector2D &v1, const QVector2D &v2);
104 Q_DECL_CONSTEXPR friend inline const QVector2D operator*(float factor, const QVector2D &vector);
105 Q_DECL_CONSTEXPR friend inline const QVector2D operator*(const QVector2D &vector, float factor);
106 Q_DECL_CONSTEXPR friend inline const QVector2D operator*(const QVector2D &v1, const QVector2D &v2);
107 Q_DECL_CONSTEXPR friend inline const QVector2D operator-(const QVector2D &vector);
108 Q_DECL_CONSTEXPR friend inline const QVector2D operator/(const QVector2D &vector, float divisor);
109 Q_DECL_CONSTEXPR friend inline const QVector2D operator/(const QVector2D &vector, const QVector2D &divisor);
110
111 Q_DECL_CONSTEXPR friend inline bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2);
112
113#ifndef QT_NO_VECTOR3D
114 QVector3D toVector3D() const;
115#endif
116#ifndef QT_NO_VECTOR4D
117 QVector4D toVector4D() const;
118#endif
119
120 Q_DECL_CONSTEXPR QPoint toPoint() const;
121 Q_DECL_CONSTEXPR QPointF toPointF() const;
122
123 operator QVariant() const;
124
125private:
126 float v[2];
127
128 friend class QVector3D;
129 friend class QVector4D;
130};
131
132Q_DECLARE_TYPEINFO(QVector2D, Q_PRIMITIVE_TYPE);
133
134Q_DECL_CONSTEXPR inline QVector2D::QVector2D() : v{0.0f, 0.0f} {}
135
136Q_DECL_CONSTEXPR inline QVector2D::QVector2D(float xpos, float ypos) : v{xpos, ypos} {}
137
138Q_DECL_CONSTEXPR inline QVector2D::QVector2D(const QPoint& point) : v{float(point.x()), float(point.y())} {}
139
140Q_DECL_CONSTEXPR inline QVector2D::QVector2D(const QPointF& point) : v{float(point.x()), float(point.y())} {}
141
142inline bool QVector2D::isNull() const
143{
144 return qIsNull(v[0]) && qIsNull(v[1]);
145}
146
147Q_DECL_CONSTEXPR inline float QVector2D::x() const { return v[0]; }
148Q_DECL_CONSTEXPR inline float QVector2D::y() const { return v[1]; }
149
150inline void QVector2D::setX(float aX) { v[0] = aX; }
151inline void QVector2D::setY(float aY) { v[1] = aY; }
152
153inline float &QVector2D::operator[](int i)
154{
155 Q_ASSERT(uint(i) < 2u);
156 return v[i];
157}
158
159inline float QVector2D::operator[](int i) const
160{
161 Q_ASSERT(uint(i) < 2u);
162 return v[i];
163}
164
165inline QVector2D &QVector2D::operator+=(const QVector2D &vector)
166{
167 v[0] += vector.v[0];
168 v[1] += vector.v[1];
169 return *this;
170}
171
172inline QVector2D &QVector2D::operator-=(const QVector2D &vector)
173{
174 v[0] -= vector.v[0];
175 v[1] -= vector.v[1];
176 return *this;
177}
178
179inline QVector2D &QVector2D::operator*=(float factor)
180{
181 v[0] *= factor;
182 v[1] *= factor;
183 return *this;
184}
185
186inline QVector2D &QVector2D::operator*=(const QVector2D &vector)
187{
188 v[0] *= vector.v[0];
189 v[1] *= vector.v[1];
190 return *this;
191}
192
193inline QVector2D &QVector2D::operator/=(float divisor)
194{
195 v[0] /= divisor;
196 v[1] /= divisor;
197 return *this;
198}
199
200inline QVector2D &QVector2D::operator/=(const QVector2D &vector)
201{
202 v[0] /= vector.v[0];
203 v[1] /= vector.v[1];
204 return *this;
205}
206
207QT_WARNING_PUSH
208QT_WARNING_DISABLE_CLANG("-Wfloat-equal")
209QT_WARNING_DISABLE_GCC("-Wfloat-equal")
210QT_WARNING_DISABLE_INTEL(1572)
211Q_DECL_CONSTEXPR inline bool operator==(const QVector2D &v1, const QVector2D &v2)
212{
213 return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1];
214}
215
216Q_DECL_CONSTEXPR inline bool operator!=(const QVector2D &v1, const QVector2D &v2)
217{
218 return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1];
219}
220QT_WARNING_POP
221
222Q_DECL_CONSTEXPR inline const QVector2D operator+(const QVector2D &v1, const QVector2D &v2)
223{
224 return QVector2D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1]);
225}
226
227Q_DECL_CONSTEXPR inline const QVector2D operator-(const QVector2D &v1, const QVector2D &v2)
228{
229 return QVector2D(v1.v[0] - v2.v[0], v1.v[1] - v2.v[1]);
230}
231
232Q_DECL_CONSTEXPR inline const QVector2D operator*(float factor, const QVector2D &vector)
233{
234 return QVector2D(vector.v[0] * factor, vector.v[1] * factor);
235}
236
237Q_DECL_CONSTEXPR inline const QVector2D operator*(const QVector2D &vector, float factor)
238{
239 return QVector2D(vector.v[0] * factor, vector.v[1] * factor);
240}
241
242Q_DECL_CONSTEXPR inline const QVector2D operator*(const QVector2D &v1, const QVector2D &v2)
243{
244 return QVector2D(v1.v[0] * v2.v[0], v1.v[1] * v2.v[1]);
245}
246
247Q_DECL_CONSTEXPR inline const QVector2D operator-(const QVector2D &vector)
248{
249 return QVector2D(-vector.v[0], -vector.v[1]);
250}
251
252Q_DECL_CONSTEXPR inline const QVector2D operator/(const QVector2D &vector, float divisor)
253{
254 return QVector2D(vector.v[0] / divisor, vector.v[1] / divisor);
255}
256
257Q_DECL_CONSTEXPR inline const QVector2D operator/(const QVector2D &vector, const QVector2D &divisor)
258{
259 return QVector2D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1]);
260}
261
262Q_DECL_CONSTEXPR inline bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2)
263{
264 return qFuzzyCompare(v1.v[0], v2.v[0]) && qFuzzyCompare(v1.v[1], v2.v[1]);
265}
266
267Q_DECL_CONSTEXPR inline QPoint QVector2D::toPoint() const
268{
269 return QPoint(qRound(v[0]), qRound(v[1]));
270}
271
272Q_DECL_CONSTEXPR inline QPointF QVector2D::toPointF() const
273{
274 return QPointF(qreal(v[0]), qreal(v[1]));
275}
276
277#ifndef QT_NO_DEBUG_STREAM
278Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector2D &vector);
279#endif
280
281#ifndef QT_NO_DATASTREAM
282Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector2D &);
283Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector2D &);
284#endif
285
286#endif
287
288QT_END_NAMESPACE
289
290#endif
291