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