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 QtCore 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 QPOINT_H
41#define QPOINT_H
42
43#include <QtCore/qnamespace.h>
44
45#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
46struct CGPoint;
47#endif
48
49QT_BEGIN_NAMESPACE
50
51
52class Q_CORE_EXPORT QPoint
53{
54public:
55 Q_DECL_CONSTEXPR QPoint();
56 Q_DECL_CONSTEXPR QPoint(int xpos, int ypos);
57
58 Q_DECL_CONSTEXPR inline bool isNull() const;
59
60 Q_DECL_CONSTEXPR inline int x() const;
61 Q_DECL_CONSTEXPR inline int y() const;
62 Q_DECL_RELAXED_CONSTEXPR inline void setX(int x);
63 Q_DECL_RELAXED_CONSTEXPR inline void setY(int y);
64
65 Q_DECL_CONSTEXPR inline int manhattanLength() const;
66
67 Q_DECL_CONSTEXPR QPoint transposed() const noexcept { return {yp, xp}; }
68
69 Q_DECL_RELAXED_CONSTEXPR inline int &rx();
70 Q_DECL_RELAXED_CONSTEXPR inline int &ry();
71
72 Q_DECL_RELAXED_CONSTEXPR inline QPoint &operator+=(const QPoint &p);
73 Q_DECL_RELAXED_CONSTEXPR inline QPoint &operator-=(const QPoint &p);
74
75 Q_DECL_RELAXED_CONSTEXPR inline QPoint &operator*=(float factor);
76 Q_DECL_RELAXED_CONSTEXPR inline QPoint &operator*=(double factor);
77 Q_DECL_RELAXED_CONSTEXPR inline QPoint &operator*=(int factor);
78
79 Q_DECL_RELAXED_CONSTEXPR inline QPoint &operator/=(qreal divisor);
80
81 Q_DECL_CONSTEXPR static inline int dotProduct(const QPoint &p1, const QPoint &p2)
82 { return p1.xp * p2.xp + p1.yp * p2.yp; }
83
84 friend Q_DECL_CONSTEXPR inline bool operator==(const QPoint &, const QPoint &);
85 friend Q_DECL_CONSTEXPR inline bool operator!=(const QPoint &, const QPoint &);
86 friend Q_DECL_CONSTEXPR inline const QPoint operator+(const QPoint &, const QPoint &);
87 friend Q_DECL_CONSTEXPR inline const QPoint operator-(const QPoint &, const QPoint &);
88 friend Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &, float);
89 friend Q_DECL_CONSTEXPR inline const QPoint operator*(float, const QPoint &);
90 friend Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &, double);
91 friend Q_DECL_CONSTEXPR inline const QPoint operator*(double, const QPoint &);
92 friend Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &, int);
93 friend Q_DECL_CONSTEXPR inline const QPoint operator*(int, const QPoint &);
94 friend Q_DECL_CONSTEXPR inline const QPoint operator+(const QPoint &);
95 friend Q_DECL_CONSTEXPR inline const QPoint operator-(const QPoint &);
96 friend Q_DECL_CONSTEXPR inline const QPoint operator/(const QPoint &, qreal);
97
98#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
99 Q_REQUIRED_RESULT CGPoint toCGPoint() const noexcept;
100#endif
101
102private:
103 friend class QTransform;
104 int xp;
105 int yp;
106};
107
108Q_DECLARE_TYPEINFO(QPoint, Q_MOVABLE_TYPE);
109
110/*****************************************************************************
111 QPoint stream functions
112 *****************************************************************************/
113#ifndef QT_NO_DATASTREAM
114Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPoint &);
115Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPoint &);
116#endif
117
118/*****************************************************************************
119 QPoint inline functions
120 *****************************************************************************/
121
122Q_DECL_CONSTEXPR inline QPoint::QPoint() : xp(0), yp(0) {}
123
124Q_DECL_CONSTEXPR inline QPoint::QPoint(int xpos, int ypos) : xp(xpos), yp(ypos) {}
125
126Q_DECL_CONSTEXPR inline bool QPoint::isNull() const
127{ return xp == 0 && yp == 0; }
128
129Q_DECL_CONSTEXPR inline int QPoint::x() const
130{ return xp; }
131
132Q_DECL_CONSTEXPR inline int QPoint::y() const
133{ return yp; }
134
135Q_DECL_RELAXED_CONSTEXPR inline void QPoint::setX(int xpos)
136{ xp = xpos; }
137
138Q_DECL_RELAXED_CONSTEXPR inline void QPoint::setY(int ypos)
139{ yp = ypos; }
140
141inline int Q_DECL_CONSTEXPR QPoint::manhattanLength() const
142{ return qAbs(x())+qAbs(y()); }
143
144Q_DECL_RELAXED_CONSTEXPR inline int &QPoint::rx()
145{ return xp; }
146
147Q_DECL_RELAXED_CONSTEXPR inline int &QPoint::ry()
148{ return yp; }
149
150Q_DECL_RELAXED_CONSTEXPR inline QPoint &QPoint::operator+=(const QPoint &p)
151{ xp+=p.xp; yp+=p.yp; return *this; }
152
153Q_DECL_RELAXED_CONSTEXPR inline QPoint &QPoint::operator-=(const QPoint &p)
154{ xp-=p.xp; yp-=p.yp; return *this; }
155
156Q_DECL_RELAXED_CONSTEXPR inline QPoint &QPoint::operator*=(float factor)
157{ xp = qRound(xp*factor); yp = qRound(yp*factor); return *this; }
158
159Q_DECL_RELAXED_CONSTEXPR inline QPoint &QPoint::operator*=(double factor)
160{ xp = qRound(xp*factor); yp = qRound(yp*factor); return *this; }
161
162Q_DECL_RELAXED_CONSTEXPR inline QPoint &QPoint::operator*=(int factor)
163{ xp = xp*factor; yp = yp*factor; return *this; }
164
165Q_DECL_CONSTEXPR inline bool operator==(const QPoint &p1, const QPoint &p2)
166{ return p1.xp == p2.xp && p1.yp == p2.yp; }
167
168Q_DECL_CONSTEXPR inline bool operator!=(const QPoint &p1, const QPoint &p2)
169{ return p1.xp != p2.xp || p1.yp != p2.yp; }
170
171Q_DECL_CONSTEXPR inline const QPoint operator+(const QPoint &p1, const QPoint &p2)
172{ return QPoint(p1.xp+p2.xp, p1.yp+p2.yp); }
173
174Q_DECL_CONSTEXPR inline const QPoint operator-(const QPoint &p1, const QPoint &p2)
175{ return QPoint(p1.xp-p2.xp, p1.yp-p2.yp); }
176
177Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &p, float factor)
178{ return QPoint(qRound(p.xp*factor), qRound(p.yp*factor)); }
179
180Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &p, double factor)
181{ return QPoint(qRound(p.xp*factor), qRound(p.yp*factor)); }
182
183Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &p, int factor)
184{ return QPoint(p.xp*factor, p.yp*factor); }
185
186Q_DECL_CONSTEXPR inline const QPoint operator*(float factor, const QPoint &p)
187{ return QPoint(qRound(p.xp*factor), qRound(p.yp*factor)); }
188
189Q_DECL_CONSTEXPR inline const QPoint operator*(double factor, const QPoint &p)
190{ return QPoint(qRound(p.xp*factor), qRound(p.yp*factor)); }
191
192Q_DECL_CONSTEXPR inline const QPoint operator*(int factor, const QPoint &p)
193{ return QPoint(p.xp*factor, p.yp*factor); }
194
195Q_DECL_CONSTEXPR inline const QPoint operator+(const QPoint &p)
196{ return p; }
197
198Q_DECL_CONSTEXPR inline const QPoint operator-(const QPoint &p)
199{ return QPoint(-p.xp, -p.yp); }
200
201Q_DECL_RELAXED_CONSTEXPR inline QPoint &QPoint::operator/=(qreal c)
202{
203 xp = qRound(xp/c);
204 yp = qRound(yp/c);
205 return *this;
206}
207
208Q_DECL_CONSTEXPR inline const QPoint operator/(const QPoint &p, qreal c)
209{
210 return QPoint(qRound(p.xp/c), qRound(p.yp/c));
211}
212
213#ifndef QT_NO_DEBUG_STREAM
214Q_CORE_EXPORT QDebug operator<<(QDebug, const QPoint &);
215#endif
216
217
218
219
220
221class Q_CORE_EXPORT QPointF
222{
223public:
224 Q_DECL_CONSTEXPR QPointF();
225 Q_DECL_CONSTEXPR QPointF(const QPoint &p);
226 Q_DECL_CONSTEXPR QPointF(qreal xpos, qreal ypos);
227
228 Q_DECL_CONSTEXPR inline qreal manhattanLength() const;
229
230 inline bool isNull() const;
231
232 Q_DECL_CONSTEXPR inline qreal x() const;
233 Q_DECL_CONSTEXPR inline qreal y() const;
234 Q_DECL_RELAXED_CONSTEXPR inline void setX(qreal x);
235 Q_DECL_RELAXED_CONSTEXPR inline void setY(qreal y);
236
237 Q_DECL_CONSTEXPR QPointF transposed() const noexcept { return {yp, xp}; }
238
239 Q_DECL_RELAXED_CONSTEXPR inline qreal &rx();
240 Q_DECL_RELAXED_CONSTEXPR inline qreal &ry();
241
242 Q_DECL_RELAXED_CONSTEXPR inline QPointF &operator+=(const QPointF &p);
243 Q_DECL_RELAXED_CONSTEXPR inline QPointF &operator-=(const QPointF &p);
244 Q_DECL_RELAXED_CONSTEXPR inline QPointF &operator*=(qreal c);
245 Q_DECL_RELAXED_CONSTEXPR inline QPointF &operator/=(qreal c);
246
247 Q_DECL_CONSTEXPR static inline qreal dotProduct(const QPointF &p1, const QPointF &p2)
248 { return p1.xp * p2.xp + p1.yp * p2.yp; }
249
250 friend Q_DECL_CONSTEXPR inline bool operator==(const QPointF &, const QPointF &);
251 friend Q_DECL_CONSTEXPR inline bool operator!=(const QPointF &, const QPointF &);
252 friend Q_DECL_CONSTEXPR inline const QPointF operator+(const QPointF &, const QPointF &);
253 friend Q_DECL_CONSTEXPR inline const QPointF operator-(const QPointF &, const QPointF &);
254 friend Q_DECL_CONSTEXPR inline const QPointF operator*(qreal, const QPointF &);
255 friend Q_DECL_CONSTEXPR inline const QPointF operator*(const QPointF &, qreal);
256 friend Q_DECL_CONSTEXPR inline const QPointF operator+(const QPointF &);
257 friend Q_DECL_CONSTEXPR inline const QPointF operator-(const QPointF &);
258 friend Q_DECL_CONSTEXPR inline const QPointF operator/(const QPointF &, qreal);
259
260 Q_DECL_CONSTEXPR QPoint toPoint() const;
261
262#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
263 Q_REQUIRED_RESULT static QPointF fromCGPoint(CGPoint point) noexcept;
264 Q_REQUIRED_RESULT CGPoint toCGPoint() const noexcept;
265#endif
266
267private:
268 friend class QMatrix;
269 friend class QTransform;
270
271 qreal xp;
272 qreal yp;
273};
274
275Q_DECLARE_TYPEINFO(QPointF, Q_MOVABLE_TYPE);
276
277/*****************************************************************************
278 QPointF stream functions
279 *****************************************************************************/
280#ifndef QT_NO_DATASTREAM
281Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPointF &);
282Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPointF &);
283#endif
284
285/*****************************************************************************
286 QPointF inline functions
287 *****************************************************************************/
288
289Q_DECL_CONSTEXPR inline QPointF::QPointF() : xp(0), yp(0) { }
290
291Q_DECL_CONSTEXPR inline QPointF::QPointF(qreal xpos, qreal ypos) : xp(xpos), yp(ypos) { }
292
293Q_DECL_CONSTEXPR inline QPointF::QPointF(const QPoint &p) : xp(p.x()), yp(p.y()) { }
294
295Q_DECL_CONSTEXPR inline qreal QPointF::manhattanLength() const
296{
297 return qAbs(x())+qAbs(y());
298}
299
300inline bool QPointF::isNull() const
301{
302 return qIsNull(xp) && qIsNull(yp);
303}
304
305Q_DECL_CONSTEXPR inline qreal QPointF::x() const
306{
307 return xp;
308}
309
310Q_DECL_CONSTEXPR inline qreal QPointF::y() const
311{
312 return yp;
313}
314
315Q_DECL_RELAXED_CONSTEXPR inline void QPointF::setX(qreal xpos)
316{
317 xp = xpos;
318}
319
320Q_DECL_RELAXED_CONSTEXPR inline void QPointF::setY(qreal ypos)
321{
322 yp = ypos;
323}
324
325Q_DECL_RELAXED_CONSTEXPR inline qreal &QPointF::rx()
326{
327 return xp;
328}
329
330Q_DECL_RELAXED_CONSTEXPR inline qreal &QPointF::ry()
331{
332 return yp;
333}
334
335Q_DECL_RELAXED_CONSTEXPR inline QPointF &QPointF::operator+=(const QPointF &p)
336{
337 xp+=p.xp;
338 yp+=p.yp;
339 return *this;
340}
341
342Q_DECL_RELAXED_CONSTEXPR inline QPointF &QPointF::operator-=(const QPointF &p)
343{
344 xp-=p.xp; yp-=p.yp; return *this;
345}
346
347Q_DECL_RELAXED_CONSTEXPR inline QPointF &QPointF::operator*=(qreal c)
348{
349 xp*=c; yp*=c; return *this;
350}
351
352QT_WARNING_PUSH
353QT_WARNING_DISABLE_CLANG("-Wfloat-equal")
354QT_WARNING_DISABLE_GCC("-Wfloat-equal")
355QT_WARNING_DISABLE_INTEL(1572)
356
357Q_DECL_CONSTEXPR inline bool operator==(const QPointF &p1, const QPointF &p2)
358{
359 return ((!p1.xp || !p2.xp) ? qFuzzyIsNull(p1.xp - p2.xp) : qFuzzyCompare(p1.xp, p2.xp))
360 && ((!p1.yp || !p2.yp) ? qFuzzyIsNull(p1.yp - p2.yp) : qFuzzyCompare(p1.yp, p2.yp));
361}
362
363Q_DECL_CONSTEXPR inline bool operator!=(const QPointF &p1, const QPointF &p2)
364{
365 return !(p1 == p2);
366}
367
368QT_WARNING_POP
369
370Q_DECL_CONSTEXPR inline const QPointF operator+(const QPointF &p1, const QPointF &p2)
371{
372 return QPointF(p1.xp+p2.xp, p1.yp+p2.yp);
373}
374
375Q_DECL_CONSTEXPR inline const QPointF operator-(const QPointF &p1, const QPointF &p2)
376{
377 return QPointF(p1.xp-p2.xp, p1.yp-p2.yp);
378}
379
380Q_DECL_CONSTEXPR inline const QPointF operator*(const QPointF &p, qreal c)
381{
382 return QPointF(p.xp*c, p.yp*c);
383}
384
385Q_DECL_CONSTEXPR inline const QPointF operator*(qreal c, const QPointF &p)
386{
387 return QPointF(p.xp*c, p.yp*c);
388}
389
390Q_DECL_CONSTEXPR inline const QPointF operator+(const QPointF &p)
391{
392 return p;
393}
394
395Q_DECL_CONSTEXPR inline const QPointF operator-(const QPointF &p)
396{
397 return QPointF(-p.xp, -p.yp);
398}
399
400Q_DECL_RELAXED_CONSTEXPR inline QPointF &QPointF::operator/=(qreal divisor)
401{
402 xp/=divisor;
403 yp/=divisor;
404 return *this;
405}
406
407Q_DECL_CONSTEXPR inline const QPointF operator/(const QPointF &p, qreal divisor)
408{
409 return QPointF(p.xp/divisor, p.yp/divisor);
410}
411
412Q_DECL_CONSTEXPR inline QPoint QPointF::toPoint() const
413{
414 return QPoint(qRound(xp), qRound(yp));
415}
416
417#ifndef QT_NO_DEBUG_STREAM
418Q_CORE_EXPORT QDebug operator<<(QDebug d, const QPointF &p);
419#endif
420
421QT_END_NAMESPACE
422
423#endif // QPOINT_H
424

source code of qtbase/src/corelib/tools/qpoint.h