1 | // Copyright (C) 2022 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QPOINT_H |
5 | #define QPOINT_H |
6 | |
7 | #include <QtCore/qcompare.h> |
8 | #include <QtCore/qnamespace.h> |
9 | #include <QtCore/qnumeric.h> |
10 | |
11 | #include <QtCore/q20type_traits.h> |
12 | #include <QtCore/q23utility.h> |
13 | |
14 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
15 | struct CGPoint; |
16 | #endif |
17 | |
18 | QT_BEGIN_NAMESPACE |
19 | |
20 | QT_ENABLE_P0846_SEMANTICS_FOR(get) |
21 | |
22 | class QPointF; |
23 | |
24 | class QPoint |
25 | { |
26 | public: |
27 | constexpr QPoint() noexcept; |
28 | constexpr QPoint(int xpos, int ypos) noexcept; |
29 | |
30 | constexpr inline bool isNull() const noexcept; |
31 | |
32 | constexpr inline int x() const noexcept; |
33 | constexpr inline int y() const noexcept; |
34 | constexpr inline void setX(int x) noexcept; |
35 | constexpr inline void setY(int y) noexcept; |
36 | |
37 | constexpr inline int manhattanLength() const; |
38 | |
39 | constexpr QPoint transposed() const noexcept { return {yp, xp}; } |
40 | |
41 | constexpr inline int &rx() noexcept; |
42 | constexpr inline int &ry() noexcept; |
43 | |
44 | constexpr inline QPoint &operator+=(const QPoint &p); |
45 | constexpr inline QPoint &operator-=(const QPoint &p); |
46 | |
47 | constexpr inline QPoint &operator*=(float factor); |
48 | constexpr inline QPoint &operator*=(double factor); |
49 | constexpr inline QPoint &operator*=(int factor); |
50 | |
51 | constexpr inline QPoint &operator/=(qreal divisor); |
52 | |
53 | constexpr static inline int dotProduct(const QPoint &p1, const QPoint &p2) |
54 | { return p1.xp * p2.xp + p1.yp * p2.yp; } |
55 | |
56 | private: |
57 | friend constexpr bool comparesEqual(const QPoint &p1, const QPoint &p2) noexcept |
58 | { return p1.xp == p2.xp && p1.yp == p2.yp; } |
59 | Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QPoint) |
60 | friend constexpr inline QPoint operator+(const QPoint &p1, const QPoint &p2) noexcept |
61 | { return QPoint(p1.xp + p2.xp, p1.yp + p2.yp); } |
62 | friend constexpr inline QPoint operator-(const QPoint &p1, const QPoint &p2) noexcept |
63 | { return QPoint(p1.xp - p2.xp, p1.yp - p2.yp); } |
64 | friend constexpr inline QPoint operator*(const QPoint &p, float factor) |
65 | { return QPoint(qRound(f: p.xp * factor), qRound(f: p.yp * factor)); } |
66 | friend constexpr inline QPoint operator*(const QPoint &p, double factor) |
67 | { return QPoint(qRound(d: p.xp * factor), qRound(d: p.yp * factor)); } |
68 | friend constexpr inline QPoint operator*(const QPoint &p, int factor) noexcept |
69 | { return QPoint(p.xp * factor, p.yp * factor); } |
70 | friend constexpr inline QPoint operator*(float factor, const QPoint &p) |
71 | { return QPoint(qRound(f: p.xp * factor), qRound(f: p.yp * factor)); } |
72 | friend constexpr inline QPoint operator*(double factor, const QPoint &p) |
73 | { return QPoint(qRound(d: p.xp * factor), qRound(d: p.yp * factor)); } |
74 | friend constexpr inline QPoint operator*(int factor, const QPoint &p) noexcept |
75 | { return QPoint(p.xp * factor, p.yp * factor); } |
76 | friend constexpr inline QPoint operator+(const QPoint &p) noexcept |
77 | { return p; } |
78 | friend constexpr inline QPoint operator-(const QPoint &p) noexcept |
79 | { return QPoint(-p.xp, -p.yp); } |
80 | friend constexpr inline QPoint operator/(const QPoint &p, qreal c) |
81 | { return QPoint(qRound(d: p.xp / c), qRound(d: p.yp / c)); } |
82 | |
83 | public: |
84 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
85 | [[nodiscard]] Q_CORE_EXPORT CGPoint toCGPoint() const noexcept; |
86 | #endif |
87 | [[nodiscard]] constexpr inline QPointF toPointF() const noexcept; |
88 | |
89 | private: |
90 | friend class QTransform; |
91 | int xp; |
92 | int yp; |
93 | |
94 | template <std::size_t I, |
95 | typename P, |
96 | std::enable_if_t<(I < 2), bool> = true, |
97 | std::enable_if_t<std::is_same_v<q20::remove_cvref_t<P>, QPoint>, bool> = true> |
98 | friend constexpr decltype(auto) get(P &&p) noexcept |
99 | { |
100 | if constexpr (I == 0) |
101 | return q23::forward_like<P>(p.xp); |
102 | else if constexpr (I == 1) |
103 | return q23::forward_like<P>(p.yp); |
104 | } |
105 | }; |
106 | |
107 | Q_DECLARE_TYPEINFO(QPoint, Q_PRIMITIVE_TYPE); |
108 | |
109 | /***************************************************************************** |
110 | QPoint stream functions |
111 | *****************************************************************************/ |
112 | #ifndef QT_NO_DATASTREAM |
113 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPoint &); |
114 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPoint &); |
115 | #endif |
116 | |
117 | /***************************************************************************** |
118 | QPoint inline functions |
119 | *****************************************************************************/ |
120 | |
121 | constexpr inline QPoint::QPoint() noexcept : xp(0), yp(0) {} |
122 | |
123 | constexpr inline QPoint::QPoint(int xpos, int ypos) noexcept : xp(xpos), yp(ypos) {} |
124 | |
125 | constexpr inline bool QPoint::isNull() const noexcept |
126 | { |
127 | return xp == 0 && yp == 0; |
128 | } |
129 | |
130 | constexpr inline int QPoint::x() const noexcept |
131 | { |
132 | return xp; |
133 | } |
134 | |
135 | constexpr inline int QPoint::y() const noexcept |
136 | { |
137 | return yp; |
138 | } |
139 | |
140 | constexpr inline void QPoint::setX(int xpos) noexcept |
141 | { |
142 | xp = xpos; |
143 | } |
144 | |
145 | constexpr inline void QPoint::setY(int ypos) noexcept |
146 | { |
147 | yp = ypos; |
148 | } |
149 | |
150 | inline int constexpr QPoint::manhattanLength() const |
151 | { |
152 | return qAbs(t: x()) + qAbs(t: y()); |
153 | } |
154 | |
155 | constexpr inline int &QPoint::rx() noexcept |
156 | { |
157 | return xp; |
158 | } |
159 | |
160 | constexpr inline int &QPoint::ry() noexcept |
161 | { |
162 | return yp; |
163 | } |
164 | |
165 | constexpr inline QPoint &QPoint::operator+=(const QPoint &p) |
166 | { |
167 | xp += p.xp; |
168 | yp += p.yp; |
169 | return *this; |
170 | } |
171 | |
172 | constexpr inline QPoint &QPoint::operator-=(const QPoint &p) |
173 | { |
174 | xp -= p.xp; |
175 | yp -= p.yp; |
176 | return *this; |
177 | } |
178 | |
179 | constexpr inline QPoint &QPoint::operator*=(float factor) |
180 | { |
181 | xp = qRound(f: xp * factor); |
182 | yp = qRound(f: yp * factor); |
183 | return *this; |
184 | } |
185 | |
186 | constexpr inline QPoint &QPoint::operator*=(double factor) |
187 | { |
188 | xp = qRound(d: xp * factor); |
189 | yp = qRound(d: yp * factor); |
190 | return *this; |
191 | } |
192 | |
193 | constexpr inline QPoint &QPoint::operator*=(int factor) |
194 | { |
195 | xp = xp * factor; |
196 | yp = yp * factor; |
197 | return *this; |
198 | } |
199 | |
200 | constexpr inline QPoint &QPoint::operator/=(qreal c) |
201 | { |
202 | xp = qRound(d: xp / c); |
203 | yp = qRound(d: yp / c); |
204 | return *this; |
205 | } |
206 | |
207 | #ifndef QT_NO_DEBUG_STREAM |
208 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QPoint &); |
209 | #endif |
210 | |
211 | Q_CORE_EXPORT size_t qHash(QPoint key, size_t seed = 0) noexcept; |
212 | |
213 | |
214 | |
215 | |
216 | class QPointF |
217 | { |
218 | public: |
219 | constexpr QPointF() noexcept; |
220 | constexpr QPointF(const QPoint &p) noexcept; |
221 | constexpr QPointF(qreal xpos, qreal ypos) noexcept; |
222 | |
223 | constexpr inline qreal manhattanLength() const; |
224 | |
225 | inline bool isNull() const noexcept; |
226 | |
227 | constexpr inline qreal x() const noexcept; |
228 | constexpr inline qreal y() const noexcept; |
229 | constexpr inline void setX(qreal x) noexcept; |
230 | constexpr inline void setY(qreal y) noexcept; |
231 | |
232 | constexpr QPointF transposed() const noexcept { return {yp, xp}; } |
233 | |
234 | constexpr inline qreal &rx() noexcept; |
235 | constexpr inline qreal &ry() noexcept; |
236 | |
237 | constexpr inline QPointF &operator+=(const QPointF &p); |
238 | constexpr inline QPointF &operator-=(const QPointF &p); |
239 | constexpr inline QPointF &operator*=(qreal c); |
240 | constexpr inline QPointF &operator/=(qreal c); |
241 | |
242 | constexpr static inline qreal dotProduct(const QPointF &p1, const QPointF &p2) |
243 | { |
244 | return p1.xp * p2.xp + p1.yp * p2.yp; |
245 | } |
246 | |
247 | private: |
248 | QT_WARNING_PUSH |
249 | QT_WARNING_DISABLE_FLOAT_COMPARE |
250 | friend constexpr bool qFuzzyCompare(const QPointF &p1, const QPointF &p2) noexcept |
251 | { |
252 | return ((!p1.xp || !p2.xp) ? qFuzzyIsNull(d: p1.xp - p2.xp) : qFuzzyCompare(p1: p1.xp, p2: p2.xp)) |
253 | && ((!p1.yp || !p2.yp) ? qFuzzyIsNull(d: p1.yp - p2.yp) : qFuzzyCompare(p1: p1.yp, p2: p2.yp)); |
254 | } |
255 | QT_WARNING_POP |
256 | friend constexpr bool qFuzzyIsNull(const QPointF &point) noexcept |
257 | { |
258 | return qFuzzyIsNull(d: point.xp) && qFuzzyIsNull(d: point.yp); |
259 | } |
260 | friend constexpr bool comparesEqual(const QPointF &p1, const QPointF &p2) noexcept |
261 | { return qFuzzyCompare(p1, p2); } |
262 | Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QPointF) |
263 | friend constexpr bool comparesEqual(const QPointF &p1, const QPoint &p2) noexcept |
264 | { return comparesEqual(p1, p2: p2.toPointF()); } |
265 | Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QPointF, QPoint) |
266 | friend constexpr inline QPointF operator+(const QPointF &p1, const QPointF &p2) |
267 | { return QPointF(p1.xp + p2.xp, p1.yp + p2.yp); } |
268 | friend constexpr inline QPointF operator-(const QPointF &p1, const QPointF &p2) |
269 | { return QPointF(p1.xp - p2.xp, p1.yp - p2.yp); } |
270 | friend constexpr inline QPointF operator*(const QPointF &p, qreal c) |
271 | { return QPointF(p.xp * c, p.yp * c); } |
272 | friend constexpr inline QPointF operator*(qreal c, const QPointF &p) |
273 | { return QPointF(p.xp * c, p.yp * c); } |
274 | friend constexpr inline QPointF operator+(const QPointF &p) |
275 | { return p; } |
276 | friend constexpr inline QPointF operator-(const QPointF &p) |
277 | { return QPointF(-p.xp, -p.yp); } |
278 | friend constexpr inline QPointF operator/(const QPointF &p, qreal divisor) |
279 | { |
280 | Q_ASSERT(divisor < 0 || divisor > 0); |
281 | return QPointF(p.xp / divisor, p.yp / divisor); |
282 | } |
283 | |
284 | public: |
285 | constexpr QPoint toPoint() const; |
286 | |
287 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
288 | [[nodiscard]] Q_CORE_EXPORT static QPointF fromCGPoint(CGPoint point) noexcept; |
289 | [[nodiscard]] Q_CORE_EXPORT CGPoint toCGPoint() const noexcept; |
290 | #endif |
291 | |
292 | private: |
293 | friend class QTransform; |
294 | |
295 | qreal xp; |
296 | qreal yp; |
297 | |
298 | template <std::size_t I, |
299 | typename P, |
300 | std::enable_if_t<(I < 2), bool> = true, |
301 | std::enable_if_t<std::is_same_v<q20::remove_cvref_t<P>, QPointF>, bool> = true> |
302 | friend constexpr decltype(auto) get(P &&p) noexcept |
303 | { |
304 | if constexpr (I == 0) |
305 | return q23::forward_like<P>(p.xp); |
306 | else if constexpr (I == 1) |
307 | return q23::forward_like<P>(p.yp); |
308 | } |
309 | }; |
310 | |
311 | Q_DECLARE_TYPEINFO(QPointF, Q_PRIMITIVE_TYPE); |
312 | |
313 | size_t qHash(QPointF, size_t seed = 0) = delete; |
314 | |
315 | /***************************************************************************** |
316 | QPointF stream functions |
317 | *****************************************************************************/ |
318 | #ifndef QT_NO_DATASTREAM |
319 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPointF &); |
320 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPointF &); |
321 | #endif |
322 | |
323 | /***************************************************************************** |
324 | QPointF inline functions |
325 | *****************************************************************************/ |
326 | |
327 | constexpr inline QPointF::QPointF() noexcept : xp(0), yp(0) { } |
328 | |
329 | constexpr inline QPointF::QPointF(qreal xpos, qreal ypos) noexcept : xp(xpos), yp(ypos) { } |
330 | |
331 | constexpr inline QPointF::QPointF(const QPoint &p) noexcept : xp(p.x()), yp(p.y()) { } |
332 | |
333 | constexpr inline qreal QPointF::manhattanLength() const |
334 | { |
335 | return qAbs(t: x()) + qAbs(t: y()); |
336 | } |
337 | |
338 | inline bool QPointF::isNull() const noexcept |
339 | { |
340 | return qIsNull(d: xp) && qIsNull(d: yp); |
341 | } |
342 | |
343 | constexpr inline qreal QPointF::x() const noexcept |
344 | { |
345 | return xp; |
346 | } |
347 | |
348 | constexpr inline qreal QPointF::y() const noexcept |
349 | { |
350 | return yp; |
351 | } |
352 | |
353 | constexpr inline void QPointF::setX(qreal xpos) noexcept |
354 | { |
355 | xp = xpos; |
356 | } |
357 | |
358 | constexpr inline void QPointF::setY(qreal ypos) noexcept |
359 | { |
360 | yp = ypos; |
361 | } |
362 | |
363 | constexpr inline qreal &QPointF::rx() noexcept |
364 | { |
365 | return xp; |
366 | } |
367 | |
368 | constexpr inline qreal &QPointF::ry() noexcept |
369 | { |
370 | return yp; |
371 | } |
372 | |
373 | constexpr inline QPointF &QPointF::operator+=(const QPointF &p) |
374 | { |
375 | xp += p.xp; |
376 | yp += p.yp; |
377 | return *this; |
378 | } |
379 | |
380 | constexpr inline QPointF &QPointF::operator-=(const QPointF &p) |
381 | { |
382 | xp -= p.xp; |
383 | yp -= p.yp; |
384 | return *this; |
385 | } |
386 | |
387 | constexpr inline QPointF &QPointF::operator*=(qreal c) |
388 | { |
389 | xp *= c; |
390 | yp *= c; |
391 | return *this; |
392 | } |
393 | |
394 | constexpr inline QPointF &QPointF::operator/=(qreal divisor) |
395 | { |
396 | Q_ASSERT(divisor > 0 || divisor < 0); |
397 | xp /= divisor; |
398 | yp /= divisor; |
399 | return *this; |
400 | } |
401 | |
402 | constexpr QPointF QPoint::toPointF() const noexcept { return *this; } |
403 | |
404 | constexpr inline QPoint QPointF::toPoint() const |
405 | { |
406 | return QPoint(qRound(d: xp), qRound(d: yp)); |
407 | } |
408 | |
409 | #ifndef QT_NO_DEBUG_STREAM |
410 | Q_CORE_EXPORT QDebug operator<<(QDebug d, const QPointF &p); |
411 | #endif |
412 | |
413 | QT_END_NAMESPACE |
414 | |
415 | /***************************************************************************** |
416 | QPoint/QPointF tuple protocol |
417 | *****************************************************************************/ |
418 | |
419 | namespace std { |
420 | template <> |
421 | class tuple_size<QT_PREPEND_NAMESPACE(QPoint)> : public integral_constant<size_t, 2> {}; |
422 | template <> |
423 | class tuple_element<0, QT_PREPEND_NAMESPACE(QPoint)> { public: using type = int; }; |
424 | template <> |
425 | class tuple_element<1, QT_PREPEND_NAMESPACE(QPoint)> { public: using type = int; }; |
426 | |
427 | template <> |
428 | class tuple_size<QT_PREPEND_NAMESPACE(QPointF)> : public integral_constant<size_t, 2> {}; |
429 | template <> |
430 | class tuple_element<0, QT_PREPEND_NAMESPACE(QPointF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); }; |
431 | template <> |
432 | class tuple_element<1, QT_PREPEND_NAMESPACE(QPointF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); }; |
433 | } |
434 | |
435 | #endif // QPOINT_H |
436 |
Definitions
- QPoint
- transposed
- dotProduct
- comparesEqual
- operator+
- operator-
- operator*
- operator*
- operator*
- operator*
- operator*
- operator*
- operator+
- operator-
- operator/
- get
- QPoint
- QPoint
- isNull
- x
- y
- setX
- setY
- manhattanLength
- rx
- ry
- operator+=
- operator-=
- operator*=
- operator*=
- operator*=
- operator/=
- QPointF
- transposed
- dotProduct
- qFuzzyCompare
- qFuzzyIsNull
- comparesEqual
- comparesEqual
- operator+
- operator-
- operator*
- operator*
- operator+
- operator-
- operator/
- get
- qHash
- QPointF
- QPointF
- QPointF
- manhattanLength
- isNull
- x
- y
- setX
- setY
- rx
- ry
- operator+=
- operator-=
- operator*=
- operator/=
- toPointF
- toPoint
- tuple_size
- tuple_element
- tuple_element
- tuple_size
- tuple_element
Learn to use CMake with our Intro Training
Find out more