/src/qtbase/src/gui/math3d/qvector2d.h
Line | Count | Source (jump to first uncovered line) |
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 | | Q_DECL_CONSTEXPR QVector2D(); |
60 | 0 | 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 | | |
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 | 0 | Q_DECL_CONSTEXPR inline QVector2D::QVector2D() : v{0.0f, 0.0f} {} |
135 | | |
136 | 0 | Q_DECL_CONSTEXPR inline QVector2D::QVector2D(float xpos, float ypos) : v{xpos, ypos} {} |
137 | | |
138 | | Q_DECL_CONSTEXPR inline QVector2D::QVector2D(const QPoint& point) : v{float(point.x()), float(point.y())} {} |
139 | | |
140 | | Q_DECL_CONSTEXPR inline QVector2D::QVector2D(const QPointF& point) : v{float(point.x()), float(point.y())} {} |
141 | | |
142 | | inline bool QVector2D::isNull() const |
143 | 0 | { |
144 | 0 | return qIsNull(v[0]) && qIsNull(v[1]); |
145 | 0 | } |
146 | | |
147 | 0 | Q_DECL_CONSTEXPR inline float QVector2D::x() const { return v[0]; } |
148 | 0 | Q_DECL_CONSTEXPR inline float QVector2D::y() const { return v[1]; } |
149 | | |
150 | 0 | inline void QVector2D::setX(float aX) { v[0] = aX; } |
151 | 0 | inline void QVector2D::setY(float aY) { v[1] = aY; } |
152 | | |
153 | | inline float &QVector2D::operator[](int i) |
154 | 0 | { |
155 | 0 | Q_ASSERT(uint(i) < 2u); |
156 | 0 | return v[i]; |
157 | 0 | } |
158 | | |
159 | | inline float QVector2D::operator[](int i) const |
160 | 0 | { |
161 | 0 | Q_ASSERT(uint(i) < 2u); |
162 | 0 | return v[i]; |
163 | 0 | } |
164 | | |
165 | | inline QVector2D &QVector2D::operator+=(const QVector2D &vector) |
166 | 0 | { |
167 | 0 | v[0] += vector.v[0]; |
168 | 0 | v[1] += vector.v[1]; |
169 | 0 | return *this; |
170 | 0 | } |
171 | | |
172 | | inline QVector2D &QVector2D::operator-=(const QVector2D &vector) |
173 | 0 | { |
174 | 0 | v[0] -= vector.v[0]; |
175 | 0 | v[1] -= vector.v[1]; |
176 | 0 | return *this; |
177 | 0 | } |
178 | | |
179 | | inline QVector2D &QVector2D::operator*=(float factor) |
180 | 0 | { |
181 | 0 | v[0] *= factor; |
182 | 0 | v[1] *= factor; |
183 | 0 | return *this; |
184 | 0 | } |
185 | | |
186 | | inline QVector2D &QVector2D::operator*=(const QVector2D &vector) |
187 | 0 | { |
188 | 0 | v[0] *= vector.v[0]; |
189 | 0 | v[1] *= vector.v[1]; |
190 | 0 | return *this; |
191 | 0 | } |
192 | | |
193 | | inline QVector2D &QVector2D::operator/=(float divisor) |
194 | 0 | { |
195 | 0 | v[0] /= divisor; |
196 | 0 | v[1] /= divisor; |
197 | 0 | return *this; |
198 | 0 | } |
199 | | |
200 | | inline QVector2D &QVector2D::operator/=(const QVector2D &vector) |
201 | 0 | { |
202 | 0 | v[0] /= vector.v[0]; |
203 | 0 | v[1] /= vector.v[1]; |
204 | 0 | return *this; |
205 | 0 | } |
206 | | |
207 | | QT_WARNING_PUSH |
208 | | QT_WARNING_DISABLE_CLANG("-Wfloat-equal") |
209 | | QT_WARNING_DISABLE_GCC("-Wfloat-equal") |
210 | | QT_WARNING_DISABLE_INTEL(1572) |
211 | | Q_DECL_CONSTEXPR inline bool operator==(const QVector2D &v1, const QVector2D &v2) |
212 | 0 | { |
213 | 0 | return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1]; |
214 | 0 | } |
215 | | |
216 | | Q_DECL_CONSTEXPR inline bool operator!=(const QVector2D &v1, const QVector2D &v2) |
217 | 0 | { |
218 | 0 | return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1]; |
219 | 0 | } |
220 | | QT_WARNING_POP |
221 | | |
222 | | Q_DECL_CONSTEXPR inline const QVector2D operator+(const QVector2D &v1, const QVector2D &v2) |
223 | 0 | { |
224 | 0 | return QVector2D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1]); |
225 | 0 | } |
226 | | |
227 | | Q_DECL_CONSTEXPR inline const QVector2D operator-(const QVector2D &v1, const QVector2D &v2) |
228 | 0 | { |
229 | 0 | return QVector2D(v1.v[0] - v2.v[0], v1.v[1] - v2.v[1]); |
230 | 0 | } |
231 | | |
232 | | Q_DECL_CONSTEXPR inline const QVector2D operator*(float factor, const QVector2D &vector) |
233 | 0 | { |
234 | 0 | return QVector2D(vector.v[0] * factor, vector.v[1] * factor); |
235 | 0 | } |
236 | | |
237 | | Q_DECL_CONSTEXPR inline const QVector2D operator*(const QVector2D &vector, float factor) |
238 | 0 | { |
239 | 0 | return QVector2D(vector.v[0] * factor, vector.v[1] * factor); |
240 | 0 | } |
241 | | |
242 | | Q_DECL_CONSTEXPR inline const QVector2D operator*(const QVector2D &v1, const QVector2D &v2) |
243 | 0 | { |
244 | 0 | return QVector2D(v1.v[0] * v2.v[0], v1.v[1] * v2.v[1]); |
245 | 0 | } |
246 | | |
247 | | Q_DECL_CONSTEXPR inline const QVector2D operator-(const QVector2D &vector) |
248 | 0 | { |
249 | 0 | return QVector2D(-vector.v[0], -vector.v[1]); |
250 | 0 | } |
251 | | |
252 | | Q_DECL_CONSTEXPR inline const QVector2D operator/(const QVector2D &vector, float divisor) |
253 | 0 | { |
254 | 0 | return QVector2D(vector.v[0] / divisor, vector.v[1] / divisor); |
255 | 0 | } |
256 | | |
257 | | Q_DECL_CONSTEXPR inline const QVector2D operator/(const QVector2D &vector, const QVector2D &divisor) |
258 | 0 | { |
259 | 0 | return QVector2D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1]); |
260 | 0 | } |
261 | | |
262 | | Q_DECL_CONSTEXPR inline bool qFuzzyCompare(const QVector2D& v1, const QVector2D& v2) |
263 | 0 | { |
264 | 0 | return qFuzzyCompare(v1.v[0], v2.v[0]) && qFuzzyCompare(v1.v[1], v2.v[1]); |
265 | 0 | } |
266 | | |
267 | | Q_DECL_CONSTEXPR inline QPoint QVector2D::toPoint() const |
268 | 0 | { |
269 | 0 | return QPoint(qRound(v[0]), qRound(v[1])); |
270 | 0 | } |
271 | | |
272 | | Q_DECL_CONSTEXPR inline QPointF QVector2D::toPointF() const |
273 | 0 | { |
274 | 0 | return QPointF(qreal(v[0]), qreal(v[1])); |
275 | 0 | } |
276 | | |
277 | | #ifndef QT_NO_DEBUG_STREAM |
278 | | Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector2D &vector); |
279 | | #endif |
280 | | |
281 | | #ifndef QT_NO_DATASTREAM |
282 | | Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector2D &); |
283 | | Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector2D &); |
284 | | #endif |
285 | | |
286 | | #endif |
287 | | |
288 | | QT_END_NAMESPACE |
289 | | |
290 | | #endif |