Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qpoint.h
Go to the documentation of this file.
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)
15struct CGPoint;
16#endif
17
19
20QT_ENABLE_P0846_SEMANTICS_FOR(get)
21
22class QPointF;
23
24class QPoint
25{
26public:
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
56private:
57 friend constexpr bool comparesEqual(const QPoint &p1, const QPoint &p2) noexcept
58 { return p1.xp == p2.xp && p1.yp == p2.yp; }
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(p.xp * factor), qRound(p.yp * factor)); }
66 friend constexpr inline QPoint operator*(const QPoint &p, double factor)
67 { return QPoint(qRound(p.xp * factor), qRound(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(p.xp * factor), qRound(p.yp * factor)); }
72 friend constexpr inline QPoint operator*(double factor, const QPoint &p)
73 { return QPoint(qRound(p.xp * factor), qRound(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(p.xp / c), qRound(p.yp / c)); }
82
83public:
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
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
108
109/*****************************************************************************
110 QPoint stream functions
111 *****************************************************************************/
112#ifndef QT_NO_DATASTREAM
113Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPoint &);
114Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPoint &);
115#endif
116
117/*****************************************************************************
118 QPoint inline functions
119 *****************************************************************************/
120
121constexpr inline QPoint::QPoint() noexcept : xp(0), yp(0) {}
122
123constexpr inline QPoint::QPoint(int xpos, int ypos) noexcept : xp(xpos), yp(ypos) {}
124
125constexpr inline bool QPoint::isNull() const noexcept
126{
127 return xp == 0 && yp == 0;
128}
129
130constexpr inline int QPoint::x() const noexcept
131{
132 return xp;
133}
134
135constexpr inline int QPoint::y() const noexcept
136{
137 return yp;
138}
139
140constexpr inline void QPoint::setX(int xpos) noexcept
141{
142 xp = xpos;
143}
144
145constexpr inline void QPoint::setY(int ypos) noexcept
146{
147 yp = ypos;
148}
149
150inline int constexpr QPoint::manhattanLength() const
151{
152 return qAbs(x()) + qAbs(y());
153}
154
155constexpr inline int &QPoint::rx() noexcept
156{
157 return xp;
158}
159
160constexpr inline int &QPoint::ry() noexcept
161{
162 return yp;
163}
164
165constexpr inline QPoint &QPoint::operator+=(const QPoint &p)
166{
167 xp += p.xp;
168 yp += p.yp;
169 return *this;
170}
171
172constexpr inline QPoint &QPoint::operator-=(const QPoint &p)
173{
174 xp -= p.xp;
175 yp -= p.yp;
176 return *this;
177}
178
179constexpr inline QPoint &QPoint::operator*=(float factor)
180{
181 xp = qRound(xp * factor);
182 yp = qRound(yp * factor);
183 return *this;
184}
185
186constexpr inline QPoint &QPoint::operator*=(double factor)
187{
188 xp = qRound(xp * factor);
189 yp = qRound(yp * factor);
190 return *this;
191}
192
193constexpr inline QPoint &QPoint::operator*=(int factor)
194{
195 xp = xp * factor;
196 yp = yp * factor;
197 return *this;
198}
199
201{
202 xp = qRound(xp / c);
203 yp = qRound(yp / c);
204 return *this;
205}
206
207#ifndef QT_NO_DEBUG_STREAM
208Q_CORE_EXPORT QDebug operator<<(QDebug, const QPoint &);
209#endif
210
211Q_CORE_EXPORT size_t qHash(QPoint key, size_t seed = 0) noexcept;
212
213
214
215
217{
218public:
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
247private:
250 friend constexpr bool qFuzzyCompare(const QPointF &p1, const QPointF &p2) noexcept
251 {
252 return ((!p1.xp || !p2.xp) ? qFuzzyIsNull(p1.xp - p2.xp) : qFuzzyCompare(p1.xp, p2.xp))
253 && ((!p1.yp || !p2.yp) ? qFuzzyIsNull(p1.yp - p2.yp) : qFuzzyCompare(p1.yp, p2.yp));
254 }
256 friend constexpr bool qFuzzyIsNull(const QPointF &point) noexcept
257 {
258 return qFuzzyIsNull(point.xp) && qFuzzyIsNull(point.yp);
259 }
260 friend constexpr bool comparesEqual(const QPointF &p1, const QPointF &p2) noexcept
261 { return qFuzzyCompare(p1, p2); }
263 friend constexpr bool comparesEqual(const QPointF &p1, const QPoint &p2) noexcept
264 { return comparesEqual(p1, p2.toPointF()); }
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
284public:
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
292private:
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
312
313size_t qHash(QPointF, size_t seed = 0) = delete;
314
315/*****************************************************************************
316 QPointF stream functions
317 *****************************************************************************/
318#ifndef QT_NO_DATASTREAM
319Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPointF &);
320Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPointF &);
321#endif
322
323/*****************************************************************************
324 QPointF inline functions
325 *****************************************************************************/
326
327constexpr inline QPointF::QPointF() noexcept : xp(0), yp(0) { }
328
329constexpr inline QPointF::QPointF(qreal xpos, qreal ypos) noexcept : xp(xpos), yp(ypos) { }
330
331constexpr inline QPointF::QPointF(const QPoint &p) noexcept : xp(p.x()), yp(p.y()) { }
332
333constexpr inline qreal QPointF::manhattanLength() const
334{
335 return qAbs(x()) + qAbs(y());
336}
337
338inline bool QPointF::isNull() const noexcept
339{
340 return qIsNull(xp) && qIsNull(yp);
341}
342
343constexpr inline qreal QPointF::x() const noexcept
344{
345 return xp;
346}
347
348constexpr inline qreal QPointF::y() const noexcept
349{
350 return yp;
351}
352
353constexpr inline void QPointF::setX(qreal xpos) noexcept
354{
355 xp = xpos;
356}
357
358constexpr inline void QPointF::setY(qreal ypos) noexcept
359{
360 yp = ypos;
361}
362
363constexpr inline qreal &QPointF::rx() noexcept
364{
365 return xp;
366}
367
368constexpr inline qreal &QPointF::ry() noexcept
369{
370 return yp;
371}
372
373constexpr inline QPointF &QPointF::operator+=(const QPointF &p)
374{
375 xp += p.xp;
376 yp += p.yp;
377 return *this;
378}
379
380constexpr inline QPointF &QPointF::operator-=(const QPointF &p)
381{
382 xp -= p.xp;
383 yp -= p.yp;
384 return *this;
385}
386
388{
389 xp *= c;
390 yp *= c;
391 return *this;
392}
393
395{
396 Q_ASSERT(divisor > 0 || divisor < 0);
397 xp /= divisor;
398 yp /= divisor;
399 return *this;
400}
401
402constexpr QPointF QPoint::toPointF() const noexcept { return *this; }
403
404constexpr inline QPoint QPointF::toPoint() const
405{
406 return QPoint(qRound(xp), qRound(yp));
407}
408
409#ifndef QT_NO_DEBUG_STREAM
410Q_CORE_EXPORT QDebug operator<<(QDebug d, const QPointF &p);
411#endif
412
414
415/*****************************************************************************
416 QPoint/QPointF tuple protocol
417 *****************************************************************************/
418
419namespace 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
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore\reentrant
Definition qpoint.h:217
QT_WARNING_PUSH QT_WARNING_DISABLE_FLOAT_COMPARE friend constexpr bool qFuzzyCompare(const QPointF &p1, const QPointF &p2) noexcept
Definition qpoint.h:250
friend constexpr QPointF operator+(const QPointF &p)
Definition qpoint.h:274
constexpr qreal & ry() noexcept
Returns a reference to the y coordinate of this point.
Definition qpoint.h:368
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:343
constexpr qreal manhattanLength() const
Definition qpoint.h:333
friend constexpr bool comparesEqual(const QPointF &p1, const QPointF &p2) noexcept
Definition qpoint.h:260
friend constexpr QPointF operator/(const QPointF &p, qreal divisor)
Returns the QPointF object formed by dividing each component of the given point by the given divisor.
Definition qpoint.h:278
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:348
friend constexpr QPointF operator-(const QPointF &p1, const QPointF &p2)
Returns a QPointF object that is formed by subtracting p2 from p1; each component is subtracted separ...
Definition qpoint.h:268
QT_WARNING_POP friend constexpr bool qFuzzyIsNull(const QPointF &point) noexcept
Definition qpoint.h:256
constexpr QPointF & operator+=(const QPointF &p)
Adds the given point to this point and returns a reference to this point.
Definition qpoint.h:373
constexpr qreal & rx() noexcept
Returns a reference to the x coordinate of this point.
Definition qpoint.h:363
constexpr QPointF & operator*=(qreal c)
Multiplies this point's coordinates by the given finite factor, and returns a reference to this point...
Definition qpoint.h:387
friend constexpr QPointF operator-(const QPointF &p)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qpoint.h:276
friend constexpr QPointF operator*(const QPointF &p, qreal c)
Returns a copy of the given point, multiplied by the given finite factor.
Definition qpoint.h:270
friend constexpr QPointF operator*(qreal c, const QPointF &p)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qpoint.h:272
constexpr void setY(qreal y) noexcept
Sets the y coordinate of this point to the given finite y coordinate.
Definition qpoint.h:358
constexpr QPointF & operator-=(const QPointF &p)
Subtracts the given point from this point and returns a reference to this point.
Definition qpoint.h:380
constexpr QPointF() noexcept
Constructs a null point, i.e.
Definition qpoint.h:327
constexpr QPoint toPoint() const
Rounds the coordinates of this point to the nearest integer, and returns a QPoint object with the rou...
Definition qpoint.h:404
friend constexpr decltype(auto) get(P &&p) noexcept
Definition qpoint.h:302
constexpr void setX(qreal x) noexcept
Sets the x coordinate of this point to the given finite x coordinate.
Definition qpoint.h:353
constexpr QPointF & operator/=(qreal c)
Divides both x and y by the given divisor, and returns a reference to this point.
Definition qpoint.h:394
constexpr QPointF transposed() const noexcept
Definition qpoint.h:232
bool isNull() const noexcept
Returns true if both the x and y coordinates are set to 0.0 (ignoring the sign); otherwise returns fa...
Definition qpoint.h:338
\inmodule QtCore\reentrant
Definition qpoint.h:25
friend constexpr QPoint operator+(const QPoint &p) noexcept
Definition qpoint.h:76
constexpr bool isNull() const noexcept
Returns true if both the x and y coordinates are set to 0, otherwise returns false.
Definition qpoint.h:125
constexpr int & ry() noexcept
Returns a reference to the y coordinate of this point.
Definition qpoint.h:160
constexpr QPoint transposed() const noexcept
Definition qpoint.h:39
constexpr QPointF toPointF() const noexcept
Definition qpoint.h:402
friend constexpr QPoint operator*(const QPoint &p, double factor)
Returns a copy of the given point multiplied by the given factor.
Definition qpoint.h:66
friend constexpr QPoint operator*(double factor, const QPoint &p)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qpoint.h:72
friend constexpr QPoint operator/(const QPoint &p, qreal c)
Returns the QPoint formed by dividing both components of the given point by the given divisor.
Definition qpoint.h:80
constexpr int & rx() noexcept
Returns a reference to the x coordinate of this point.
Definition qpoint.h:155
friend constexpr QPoint operator*(const QPoint &p, int factor) noexcept
Returns a copy of the given point multiplied by the given factor.
Definition qpoint.h:68
constexpr int x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:130
constexpr void setY(int y) noexcept
Sets the y coordinate of this point to the given y coordinate.
Definition qpoint.h:145
constexpr QPoint & operator+=(const QPoint &p)
Adds the given point to this point and returns a reference to this point.
Definition qpoint.h:165
constexpr int manhattanLength() const
Returns the sum of the absolute values of x() and y(), traditionally known as the "Manhattan length" ...
Definition qpoint.h:150
friend constexpr QPoint operator*(const QPoint &p, float factor)
Returns a copy of the given point multiplied by the given factor.
Definition qpoint.h:64
constexpr int y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:135
friend constexpr QPoint operator-(const QPoint &p1, const QPoint &p2) noexcept
Returns a QPoint object that is formed by subtracting p2 from p1; each component is subtracted separa...
Definition qpoint.h:62
friend constexpr QPoint operator*(float factor, const QPoint &p)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qpoint.h:70
friend constexpr QPoint operator-(const QPoint &p) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qpoint.h:78
friend constexpr QPoint operator*(int factor, const QPoint &p) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qpoint.h:74
constexpr void setX(int x) noexcept
Sets the x coordinate of this point to the given x coordinate.
Definition qpoint.h:140
friend constexpr decltype(auto) get(P &&p) noexcept
Definition qpoint.h:98
constexpr QPoint & operator*=(float factor)
Multiplies this point's coordinates by the given factor, and returns a reference to this point.
Definition qpoint.h:179
constexpr QPoint & operator-=(const QPoint &p)
Subtracts the given point from this point and returns a reference to this point.
Definition qpoint.h:172
constexpr QPoint & operator/=(qreal divisor)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qpoint.h:200
constexpr QPoint() noexcept
Constructs a null point, i.e.
Definition qpoint.h:121
friend constexpr bool comparesEqual(const QPoint &p1, const QPoint &p2) noexcept
Definition qpoint.h:57
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
QPixmap p2
QPixmap p1
[0]
Combined button and popup list for selecting options.
#define Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(...)
#define QT_WARNING_POP
#define QT_WARNING_DISABLE_FLOAT_COMPARE
#define QT_WARNING_PUSH
static QDBusError::ErrorType get(const char *name)
bool comparesEqual(const QDir &lhs, const QDir &rhs)
Definition qdir.cpp:1819
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
bool qFuzzyIsNull(qfloat16 f) noexcept
Definition qfloat16.h:349
bool qIsNull(qfloat16 f) noexcept
Definition qfloat16.h:354
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:327
constexpr T qAbs(const T &t)
Definition qnumeric.h:328
GLint GLint GLint GLint GLint x
[0]
GLuint64 key
GLuint divisor
GLint y
const GLubyte * c
GLfloat GLfloat p
[1]
Q_CORE_EXPORT QDataStream & operator<<(QDataStream &, const QPoint &)
Definition qpoint.cpp:376
Q_CORE_EXPORT size_t qHash(QPoint key, size_t seed=0) noexcept
Definition qpoint.cpp:460
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QPoint &)
Definition qpoint.cpp:395
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
@ Q_PRIMITIVE_TYPE
Definition qtypeinfo.h:157
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:180
double qreal
Definition qtypes.h:187
p setX(p.x()+1)
p ry()++
p rx()++