Qt 6.x
The Qt SDK
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/qnamespace.h>
8
9#include <QtCore/q20type_traits.h>
10#include <QtCore/q23utility.h>
11
12#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
13struct CGPoint;
14#endif
15
17
18QT_ENABLE_P0846_SEMANTICS_FOR(get)
19
20class QPointF;
21
22class QPoint
23{
24public:
25 constexpr QPoint() noexcept;
26 constexpr QPoint(int xpos, int ypos) noexcept;
27
28 constexpr inline bool isNull() const noexcept;
29
30 constexpr inline int x() const noexcept;
31 constexpr inline int y() const noexcept;
32 constexpr inline void setX(int x) noexcept;
33 constexpr inline void setY(int y) noexcept;
34
35 constexpr inline int manhattanLength() const;
36
37 constexpr QPoint transposed() const noexcept { return {yp, xp}; }
38
39 constexpr inline int &rx() noexcept;
40 constexpr inline int &ry() noexcept;
41
42 constexpr inline QPoint &operator+=(const QPoint &p);
43 constexpr inline QPoint &operator-=(const QPoint &p);
44
45 constexpr inline QPoint &operator*=(float factor);
46 constexpr inline QPoint &operator*=(double factor);
47 constexpr inline QPoint &operator*=(int factor);
48
49 constexpr inline QPoint &operator/=(qreal divisor);
50
51 constexpr static inline int dotProduct(const QPoint &p1, const QPoint &p2)
52 { return p1.xp * p2.xp + p1.yp * p2.yp; }
53
54 friend constexpr inline bool operator==(const QPoint &p1, const QPoint &p2) noexcept
55 { return p1.xp == p2.xp && p1.yp == p2.yp; }
56 friend constexpr inline bool operator!=(const QPoint &p1, const QPoint &p2) noexcept
57 { return p1.xp != p2.xp || p1.yp != p2.yp; }
58 friend constexpr inline QPoint operator+(const QPoint &p1, const QPoint &p2) noexcept
59 { return QPoint(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 &p, float factor)
63 { return QPoint(qRound(p.xp * factor), qRound(p.yp * factor)); }
64 friend constexpr inline QPoint operator*(const QPoint &p, double factor)
65 { return QPoint(qRound(p.xp * factor), qRound(p.yp * factor)); }
66 friend constexpr inline QPoint operator*(const QPoint &p, int factor) noexcept
67 { return QPoint(p.xp * factor, p.yp * factor); }
68 friend constexpr inline QPoint operator*(float factor, const QPoint &p)
69 { return QPoint(qRound(p.xp * factor), qRound(p.yp * factor)); }
70 friend constexpr inline QPoint operator*(double factor, const QPoint &p)
71 { return QPoint(qRound(p.xp * factor), qRound(p.yp * factor)); }
72 friend constexpr inline QPoint operator*(int factor, const QPoint &p) noexcept
73 { return QPoint(p.xp * factor, p.yp * factor); }
74 friend constexpr inline QPoint operator+(const QPoint &p) noexcept
75 { return p; }
76 friend constexpr inline QPoint operator-(const QPoint &p) noexcept
77 { return QPoint(-p.xp, -p.yp); }
78 friend constexpr inline QPoint operator/(const QPoint &p, qreal c)
79 { return QPoint(qRound(p.xp / c), qRound(p.yp / c)); }
80
81#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
82 [[nodiscard]] Q_CORE_EXPORT CGPoint toCGPoint() const noexcept;
83#endif
84 [[nodiscard]] constexpr inline QPointF toPointF() const noexcept;
85
87 friend class QTransform;
88 int xp;
89 int yp;
90
91 template <std::size_t I,
92 typename P,
93 std::enable_if_t<(I < 2), bool> = true,
94 std::enable_if_t<std::is_same_v<q20::remove_cvref_t<P>, QPoint>, bool> = true>
95 friend constexpr decltype(auto) get(P &&p) noexcept
96 {
97 if constexpr (I == 0)
98 return q23::forward_like<P>(p.xp);
99 else if constexpr (I == 1)
100 return q23::forward_like<P>(p.yp);
101 }
102};
103
105
106/*****************************************************************************
107 QPoint stream functions
108 *****************************************************************************/
109#ifndef QT_NO_DATASTREAM
110Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPoint &);
111Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPoint &);
112#endif
113
114/*****************************************************************************
115 QPoint inline functions
116 *****************************************************************************/
117
118constexpr inline QPoint::QPoint() noexcept : xp(0), yp(0) {}
119
120constexpr inline QPoint::QPoint(int xpos, int ypos) noexcept : xp(xpos), yp(ypos) {}
121
122constexpr inline bool QPoint::isNull() const noexcept
123{
124 return xp == 0 && yp == 0;
125}
126
127constexpr inline int QPoint::x() const noexcept
128{
129 return xp;
130}
131
132constexpr inline int QPoint::y() const noexcept
133{
134 return yp;
135}
136
137constexpr inline void QPoint::setX(int xpos) noexcept
138{
139 xp = xpos;
140}
141
142constexpr inline void QPoint::setY(int ypos) noexcept
143{
144 yp = ypos;
145}
146
147inline int constexpr QPoint::manhattanLength() const
148{
149 return qAbs(x()) + qAbs(y());
150}
151
152constexpr inline int &QPoint::rx() noexcept
153{
154 return xp;
155}
156
157constexpr inline int &QPoint::ry() noexcept
158{
159 return yp;
160}
161
162constexpr inline QPoint &QPoint::operator+=(const QPoint &p)
163{
164 xp += p.xp;
165 yp += p.yp;
166 return *this;
167}
168
169constexpr inline QPoint &QPoint::operator-=(const QPoint &p)
170{
171 xp -= p.xp;
172 yp -= p.yp;
173 return *this;
174}
175
176constexpr inline QPoint &QPoint::operator*=(float factor)
177{
178 xp = qRound(xp * factor);
179 yp = qRound(yp * factor);
180 return *this;
181}
182
183constexpr inline QPoint &QPoint::operator*=(double factor)
184{
185 xp = qRound(xp * factor);
186 yp = qRound(yp * factor);
187 return *this;
188}
189
190constexpr inline QPoint &QPoint::operator*=(int factor)
191{
192 xp = xp * factor;
193 yp = yp * factor;
194 return *this;
195}
196
198{
199 xp = qRound(xp / c);
200 yp = qRound(yp / c);
201 return *this;
202}
203
204#ifndef QT_NO_DEBUG_STREAM
205Q_CORE_EXPORT QDebug operator<<(QDebug, const QPoint &);
206#endif
207
208Q_CORE_EXPORT size_t qHash(QPoint key, size_t seed = 0) noexcept;
209
210
211
212
214{
215public:
216 constexpr QPointF() noexcept;
217 constexpr QPointF(const QPoint &p) noexcept;
218 constexpr QPointF(qreal xpos, qreal ypos) noexcept;
219
220 constexpr inline qreal manhattanLength() const;
221
222 inline bool isNull() const noexcept;
223
224 constexpr inline qreal x() const noexcept;
225 constexpr inline qreal y() const noexcept;
226 constexpr inline void setX(qreal x) noexcept;
227 constexpr inline void setY(qreal y) noexcept;
228
229 constexpr QPointF transposed() const noexcept { return {yp, xp}; }
230
231 constexpr inline qreal &rx() noexcept;
232 constexpr inline qreal &ry() noexcept;
233
234 constexpr inline QPointF &operator+=(const QPointF &p);
235 constexpr inline QPointF &operator-=(const QPointF &p);
236 constexpr inline QPointF &operator*=(qreal c);
237 constexpr inline QPointF &operator/=(qreal c);
238
239 constexpr static inline qreal dotProduct(const QPointF &p1, const QPointF &p2)
240 {
241 return p1.xp * p2.xp + p1.yp * p2.yp;
242 }
243
246 friend constexpr inline bool operator==(const QPointF &p1, const QPointF &p2)
247 {
248 return ((!p1.xp || !p2.xp) ? qFuzzyIsNull(p1.xp - p2.xp) : qFuzzyCompare(p1.xp, p2.xp))
249 && ((!p1.yp || !p2.yp) ? qFuzzyIsNull(p1.yp - p2.yp) : qFuzzyCompare(p1.yp, p2.yp));
250 }
251 friend constexpr inline bool operator!=(const QPointF &p1, const QPointF &p2)
252 {
253 return !(p1 == p2);
254 }
256
257 friend constexpr inline QPointF operator+(const QPointF &p1, const QPointF &p2)
258 { return QPointF(p1.xp + p2.xp, p1.yp + p2.yp); }
259 friend constexpr inline QPointF operator-(const QPointF &p1, const QPointF &p2)
260 { return QPointF(p1.xp - p2.xp, p1.yp - p2.yp); }
261 friend constexpr inline QPointF operator*(const QPointF &p, qreal c)
262 { return QPointF(p.xp * c, p.yp * c); }
263 friend constexpr inline QPointF operator*(qreal c, const QPointF &p)
264 { return QPointF(p.xp * c, p.yp * c); }
265 friend constexpr inline QPointF operator+(const QPointF &p)
266 { return p; }
267 friend constexpr inline QPointF operator-(const QPointF &p)
268 { return QPointF(-p.xp, -p.yp); }
269 friend constexpr inline QPointF operator/(const QPointF &p, qreal divisor)
270 {
271 Q_ASSERT(divisor < 0 || divisor > 0);
272 return QPointF(p.xp / divisor, p.yp / divisor);
273 }
274
275 constexpr QPoint toPoint() const;
276
277#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
278 [[nodiscard]] Q_CORE_EXPORT static QPointF fromCGPoint(CGPoint point) noexcept;
279 [[nodiscard]] Q_CORE_EXPORT CGPoint toCGPoint() const noexcept;
280#endif
281
282private:
283 friend class QTransform;
284
285 qreal xp;
286 qreal yp;
287
288 template <std::size_t I,
289 typename P,
290 std::enable_if_t<(I < 2), bool> = true,
291 std::enable_if_t<std::is_same_v<q20::remove_cvref_t<P>, QPointF>, bool> = true>
292 friend constexpr decltype(auto) get(P &&p) noexcept
293 {
294 if constexpr (I == 0)
295 return q23::forward_like<P>(p.xp);
296 else if constexpr (I == 1)
297 return q23::forward_like<P>(p.yp);
298 }
299};
300
302
303size_t qHash(QPointF, size_t seed = 0) = delete;
304
305/*****************************************************************************
306 QPointF stream functions
307 *****************************************************************************/
308#ifndef QT_NO_DATASTREAM
309Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPointF &);
310Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPointF &);
311#endif
312
313/*****************************************************************************
314 QPointF inline functions
315 *****************************************************************************/
316
317constexpr inline QPointF::QPointF() noexcept : xp(0), yp(0) { }
318
319constexpr inline QPointF::QPointF(qreal xpos, qreal ypos) noexcept : xp(xpos), yp(ypos) { }
320
321constexpr inline QPointF::QPointF(const QPoint &p) noexcept : xp(p.x()), yp(p.y()) { }
322
323constexpr inline qreal QPointF::manhattanLength() const
324{
325 return qAbs(x()) + qAbs(y());
326}
327
328inline bool QPointF::isNull() const noexcept
329{
330 return qIsNull(xp) && qIsNull(yp);
331}
332
333constexpr inline qreal QPointF::x() const noexcept
334{
335 return xp;
336}
337
338constexpr inline qreal QPointF::y() const noexcept
339{
340 return yp;
341}
342
343constexpr inline void QPointF::setX(qreal xpos) noexcept
344{
345 xp = xpos;
346}
347
348constexpr inline void QPointF::setY(qreal ypos) noexcept
349{
350 yp = ypos;
351}
352
353constexpr inline qreal &QPointF::rx() noexcept
354{
355 return xp;
356}
357
358constexpr inline qreal &QPointF::ry() noexcept
359{
360 return yp;
361}
362
363constexpr inline QPointF &QPointF::operator+=(const QPointF &p)
364{
365 xp += p.xp;
366 yp += p.yp;
367 return *this;
368}
369
370constexpr inline QPointF &QPointF::operator-=(const QPointF &p)
371{
372 xp -= p.xp;
373 yp -= p.yp;
374 return *this;
375}
376
378{
379 xp *= c;
380 yp *= c;
381 return *this;
382}
383
385{
386 Q_ASSERT(divisor > 0 || divisor < 0);
387 xp /= divisor;
388 yp /= divisor;
389 return *this;
390}
391
392constexpr QPointF QPoint::toPointF() const noexcept { return *this; }
393
394constexpr inline QPoint QPointF::toPoint() const
395{
396 return QPoint(qRound(xp), qRound(yp));
397}
398
399#ifndef QT_NO_DEBUG_STREAM
400Q_CORE_EXPORT QDebug operator<<(QDebug d, const QPointF &p);
401#endif
402
404
405/*****************************************************************************
406 QPoint/QPointF tuple protocol
407 *****************************************************************************/
408
409namespace std {
410 template <>
411 class tuple_size<QT_PREPEND_NAMESPACE(QPoint)> : public integral_constant<size_t, 2> {};
412 template <>
413 class tuple_element<0, QT_PREPEND_NAMESPACE(QPoint)> { public: using type = int; };
414 template <>
415 class tuple_element<1, QT_PREPEND_NAMESPACE(QPoint)> { public: using type = int; };
416
417 template <>
418 class tuple_size<QT_PREPEND_NAMESPACE(QPointF)> : public integral_constant<size_t, 2> {};
419 template <>
420 class tuple_element<0, QT_PREPEND_NAMESPACE(QPointF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); };
421 template <>
422 class tuple_element<1, QT_PREPEND_NAMESPACE(QPointF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); };
423}
424
425#endif // QPOINT_H
\inmodule QtCore\reentrant
Definition qdatastream.h:30
\inmodule QtCore
\inmodule QtCore\reentrant
Definition qpoint.h:214
friend constexpr QPointF operator+(const QPointF &p)
Definition qpoint.h:265
QT_WARNING_PUSH QT_WARNING_DISABLE_FLOAT_COMPARE friend constexpr bool operator==(const QPointF &p1, const QPointF &p2)
Returns true if p1 is approximately equal to p2; otherwise returns false.
Definition qpoint.h:246
constexpr qreal & ry() noexcept
Returns a reference to the y coordinate of this point.
Definition qpoint.h:358
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:333
constexpr qreal manhattanLength() const
Definition qpoint.h:323
QT_WARNING_POP friend constexpr QPointF operator+(const QPointF &p1, const QPointF &p2)
Returns a QPointF object that is the sum of the given points, p1 and p2; each component is added sepa...
Definition qpoint.h:257
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:269
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:338
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:259
constexpr QPointF & operator+=(const QPointF &p)
Adds the given point to this point and returns a reference to this point.
Definition qpoint.h:363
constexpr qreal & rx() noexcept
Returns a reference to the x coordinate of this point.
Definition qpoint.h:353
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:377
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:267
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:261
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:263
constexpr void setY(qreal y) noexcept
Sets the y coordinate of this point to the given finite y coordinate.
Definition qpoint.h:348
constexpr QPointF & operator-=(const QPointF &p)
Subtracts the given point from this point and returns a reference to this point.
Definition qpoint.h:370
constexpr QPointF() noexcept
Constructs a null point, i.e.
Definition qpoint.h:317
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:394
friend constexpr decltype(auto) get(P &&p) noexcept
Definition qpoint.h:292
constexpr void setX(qreal x) noexcept
Sets the x coordinate of this point to the given finite x coordinate.
Definition qpoint.h:343
constexpr QPointF & operator/=(qreal c)
Divides both x and y by the given divisor, and returns a reference to this point.
Definition qpoint.h:384
constexpr QPointF transposed() const noexcept
Definition qpoint.h:229
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:328
friend constexpr bool operator!=(const QPointF &p1, const QPointF &p2)
Returns true if p1 is sufficiently different from p2; otherwise returns false.
Definition qpoint.h:251
\inmodule QtCore\reentrant
Definition qpoint.h:23
friend constexpr QPoint operator+(const QPoint &p) noexcept
Definition qpoint.h:74
constexpr bool isNull() const noexcept
Returns true if both the x and y coordinates are set to 0, otherwise returns false.
Definition qpoint.h:122
friend constexpr QPoint operator+(const QPoint &p1, const QPoint &p2) noexcept
Returns a QPoint object that is the sum of the given points, p1 and p2; each component is added separ...
Definition qpoint.h:58
constexpr int & ry() noexcept
Returns a reference to the y coordinate of this point.
Definition qpoint.h:157
constexpr QPoint transposed() const noexcept
Definition qpoint.h:37
constexpr QPointF toPointF() const noexcept
Definition qpoint.h:392
friend constexpr QPoint operator*(const QPoint &p, double factor)
Returns a copy of the given point multiplied by the given factor.
Definition qpoint.h:64
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:70
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:78
constexpr int & rx() noexcept
Returns a reference to the x coordinate of this point.
Definition qpoint.h:152
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:66
constexpr int x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:127
constexpr void setY(int y) noexcept
Sets the y coordinate of this point to the given y coordinate.
Definition qpoint.h:142
constexpr QPoint & operator+=(const QPoint &p)
Adds the given point to this point and returns a reference to this point.
Definition qpoint.h:162
constexpr int manhattanLength() const
Returns the sum of the absolute values of x() and y(), traditionally known as the "Manhattan length" ...
Definition qpoint.h:147
friend constexpr QPoint operator*(const QPoint &p, float factor)
Returns a copy of the given point multiplied by the given factor.
Definition qpoint.h:62
constexpr int y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:132
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:60
friend constexpr bool operator!=(const QPoint &p1, const QPoint &p2) noexcept
Returns true if p1 and p2 are not equal; otherwise returns false.
Definition qpoint.h:56
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:68
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:76
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:72
constexpr void setX(int x) noexcept
Sets the x coordinate of this point to the given x coordinate.
Definition qpoint.h:137
friend constexpr decltype(auto) get(P &&p) noexcept
Definition qpoint.h:95
constexpr QPoint & operator*=(float factor)
Multiplies this point's coordinates by the given factor, and returns a reference to this point.
Definition qpoint.h:176
constexpr QPoint & operator-=(const QPoint &p)
Subtracts the given point from this point and returns a reference to this point.
Definition qpoint.h:169
constexpr QPoint & operator/=(qreal divisor)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qpoint.h:197
friend constexpr bool operator==(const QPoint &p1, const QPoint &p2) noexcept
Returns true if p1 and p2 are equal; otherwise returns false.
Definition qpoint.h:54
constexpr QPoint() noexcept
Constructs a null point, i.e.
Definition qpoint.h:118
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 QT_WARNING_POP
#define QT_WARNING_DISABLE_FLOAT_COMPARE
#define QT_WARNING_PUSH
static QDBusError::ErrorType get(const char *name)
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:287
bool qFuzzyIsNull(qfloat16 f) noexcept
Definition qfloat16.h:303
bool qIsNull(qfloat16 f) noexcept
Definition qfloat16.h:308
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:281
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:371
Q_CORE_EXPORT size_t qHash(QPoint key, size_t seed=0) noexcept
Definition qpoint.cpp:455
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QPoint &)
Definition qpoint.cpp:390
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
@ Q_PRIMITIVE_TYPE
Definition qtypeinfo.h:144
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:163
double qreal
Definition qtypes.h:92
QT_END_NAMESPACE typedef QT_PREPEND_NAMESPACE(quintptr) WId
p setX(p.x()+1)
p ry()++
p rx()++