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 | |
47 | QT_BEGIN_NAMESPACE |
48 | |
49 | |
50 | class QVector3D; |
51 | class QVector4D; |
52 | class QVariant; |
53 | |
54 | #ifndef QT_NO_VECTOR2D |
55 | |
56 | class Q_GUI_EXPORT QVector2D |
57 | { |
58 | public: |
59 | constexpr QVector2D(); |
60 | explicit QVector2D(Qt::Initialization) {} |
61 | constexpr QVector2D(float xpos, float ypos); |
62 | constexpr explicit QVector2D(const QPoint& point); |
63 | 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 | constexpr float x() const; |
74 | 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 | [[nodiscard]] 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 | constexpr friend inline bool operator==(const QVector2D &v1, const QVector2D &v2); |
101 | constexpr friend inline bool operator!=(const QVector2D &v1, const QVector2D &v2); |
102 | constexpr friend inline const QVector2D operator+(const QVector2D &v1, const QVector2D &v2); |
103 | constexpr friend inline const QVector2D operator-(const QVector2D &v1, const QVector2D &v2); |
104 | constexpr friend inline const QVector2D operator*(float factor, const QVector2D &vector); |
105 | constexpr friend inline const QVector2D operator*(const QVector2D &vector, float factor); |
106 | constexpr friend inline const QVector2D operator*(const QVector2D &v1, const QVector2D &v2); |
107 | constexpr friend inline const QVector2D operator-(const QVector2D &vector); |
108 | constexpr friend inline const QVector2D operator/(const QVector2D &vector, float divisor); |
109 | constexpr friend inline const QVector2D operator/(const QVector2D &vector, const QVector2D &divisor); |
110 | |
111 | 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 | constexpr QPoint toPoint() const; |
121 | constexpr QPointF toPointF() const; |
122 | |
123 | operator QVariant() const; |
124 | |
125 | private: |
126 | float v[2]; |
127 | |
128 | friend class QVector3D; |
129 | friend class QVector4D; |
130 | }; |
131 | |
132 | Q_DECLARE_TYPEINFO(QVector2D, Q_PRIMITIVE_TYPE); |
133 | |
134 | constexpr inline QVector2D::QVector2D() : v{0.0f, 0.0f} {} |
135 | |
136 | constexpr inline QVector2D::QVector2D(float xpos, float ypos) : v{xpos, ypos} {} |
137 | |
138 | constexpr inline QVector2D::QVector2D(const QPoint& point) : v{float(point.x()), float(point.y())} {} |
139 | |
140 | constexpr inline QVector2D::QVector2D(const QPointF& point) : v{float(point.x()), float(point.y())} {} |
141 | |
142 | inline bool QVector2D::isNull() const |
143 | { |
144 | return qIsNull(v[0]) && qIsNull(v[1]); |
145 | } |
146 | |
147 | constexpr inline float QVector2D::x() const { return v[0]; } |
148 | constexpr inline float QVector2D::y() const { return v[1]; } |
149 | |
150 | inline void QVector2D::setX(float aX) { v[0] = aX; } |
151 | inline void QVector2D::setY(float aY) { v[1] = aY; } |
152 | |
153 | inline float &QVector2D::operator[](int i) |
154 | { |
155 | Q_ASSERT(uint(i) < 2u); |
156 | return v[i]; |
157 | } |
158 | |
159 | inline float QVector2D::operator[](int i) const |
160 | { |
161 | Q_ASSERT(uint(i) < 2u); |
162 | return v[i]; |
163 | } |
164 | |
165 | inline QVector2D &QVector2D::operator+=(const QVector2D &vector) |
166 | { |
167 | v[0] += vector.v[0]; |
168 | v[1] += vector.v[1]; |
169 | return *this; |
170 | } |
171 | |
172 | inline QVector2D &QVector2D::operator-=(const QVector2D &vector) |
173 | { |
174 | v[0] -= vector.v[0]; |
175 | v[1] -= vector.v[1]; |
176 | return *this; |
177 | } |
178 | |
179 | inline QVector2D &QVector2D::operator*=(float factor) |
180 | { |
181 | v[0] *= factor; |
182 | v[1] *= factor; |
183 | return *this; |
184 | } |
185 | |
186 | inline QVector2D &QVector2D::operator*=(const QVector2D &vector) |
187 | { |
188 | v[0] *= vector.v[0]; |
189 | v[1] *= vector.v[1]; |
190 | return *this; |
191 | } |
192 | |
193 | inline QVector2D &QVector2D::operator/=(float divisor) |
194 | { |
195 | v[0] /= divisor; |
196 | v[1] /= divisor; |
197 | return *this; |
198 | } |
199 | |
200 | inline QVector2D &QVector2D::operator/=(const QVector2D &vector) |
201 | { |
202 | v[0] /= vector.v[0]; |
203 | v[1] /= vector.v[1]; |
204 | return *this; |
205 | } |
206 | |
207 | QT_WARNING_PUSH |
208 | QT_WARNING_DISABLE_FLOAT_COMPARE |
209 | |
210 | constexpr inline bool operator==(const QVector2D &v1, const QVector2D &v2) |
211 | { |
212 | return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1]; |
213 | } |
214 | |
215 | constexpr inline bool operator!=(const QVector2D &v1, const QVector2D &v2) |
216 | { |
217 | return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1]; |
218 | } |
219 | QT_WARNING_POP |
220 | |
221 | constexpr inline const QVector2D operator+(const QVector2D &v1, const QVector2D &v2) |
222 | { |
223 | return QVector2D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1]); |
224 | } |
225 | |
226 | constexpr inline const QVector2D operator-(const QVector2D &v1, const QVector2D &v2) |
227 | { |
228 | return QVector2D(v1.v[0] - v2.v[0], v1.v[1] - v2.v[1]); |
229 | } |
230 | |
231 | constexpr inline const QVector2D operator*(float factor, const QVector2D &vector) |
232 | { |
233 | return QVector2D(vector.v[0] * factor, vector.v[1] * factor); |
234 | } |
235 | |
236 | constexpr inline const QVector2D operator*(const QVector2D &vector, float factor) |
237 | { |
238 | return QVector2D(vector.v[0] * factor, vector.v[1] * factor); |
239 | } |
240 | |
241 | constexpr inline const QVector2D operator*(const QVector2D &v1, const QVector2D &v2) |
242 | { |
243 | return QVector2D(v1.v[0] * v2.v[0], v1.v[1] * v2.v[1]); |
244 | } |
245 | |
246 | constexpr inline const QVector2D operator-(const QVector2D &vector) |
247 | { |
248 | return QVector2D(-vector.v[0], -vector.v[1]); |
249 | } |
250 | |
251 | constexpr inline const QVector2D operator/(const QVector2D &vector, float divisor) |
252 | { |
253 | return QVector2D(vector.v[0] / divisor, vector.v[1] / divisor); |
254 | } |
255 | |
256 | constexpr inline const QVector2D operator/(const QVector2D &vector, const QVector2D &divisor) |
257 | { |
258 | return QVector2D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1]); |
259 | } |
260 | |
261 | constexpr inline bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2) |
262 | { |
263 | return qFuzzyCompare(v1.v[0], v2.v[0]) && qFuzzyCompare(v1.v[1], v2.v[1]); |
264 | } |
265 | |
266 | constexpr inline QPoint QVector2D::toPoint() const |
267 | { |
268 | return QPoint(qRound(v[0]), qRound(v[1])); |
269 | } |
270 | |
271 | constexpr inline QPointF QVector2D::toPointF() const |
272 | { |
273 | return QPointF(qreal(v[0]), qreal(v[1])); |
274 | } |
275 | |
276 | #ifndef QT_NO_DEBUG_STREAM |
277 | Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector2D &vector); |
278 | #endif |
279 | |
280 | #ifndef QT_NO_DATASTREAM |
281 | Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector2D &); |
282 | Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector2D &); |
283 | #endif |
284 | |
285 | #endif |
286 | |
287 | QT_END_NAMESPACE |
288 | |
289 | #endif |
290 | |