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