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)
46 struct CGPoint;
47 #endif
48 
49 QT_BEGIN_NAMESPACE
50 
51 
52 class Q_CORE_EXPORT QPoint
53 {
54 public:
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 
transposed()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 
dotProduct(const QPoint & p1,const QPoint & p2)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 
102 private:
103     friend class QTransform;
104     int xp;
105     int yp;
106 };
107 
108 Q_DECLARE_TYPEINFO(QPoint, Q_MOVABLE_TYPE);
109 
110 /*****************************************************************************
111   QPoint stream functions
112  *****************************************************************************/
113 #ifndef QT_NO_DATASTREAM
114 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPoint &);
115 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPoint &);
116 #endif
117 
118 /*****************************************************************************
119   QPoint inline functions
120  *****************************************************************************/
121 
QPoint()122 Q_DECL_CONSTEXPR inline QPoint::QPoint() : xp(0), yp(0) {}
123 
QPoint(int xpos,int ypos)124 Q_DECL_CONSTEXPR inline QPoint::QPoint(int xpos, int ypos) : xp(xpos), yp(ypos) {}
125 
isNull()126 Q_DECL_CONSTEXPR inline bool QPoint::isNull() const
127 { return xp == 0 && yp == 0; }
128 
x()129 Q_DECL_CONSTEXPR inline int QPoint::x() const
130 { return xp; }
131 
y()132 Q_DECL_CONSTEXPR inline int QPoint::y() const
133 { return yp; }
134 
setX(int xpos)135 Q_DECL_RELAXED_CONSTEXPR inline void QPoint::setX(int xpos)
136 { xp = xpos; }
137 
setY(int ypos)138 Q_DECL_RELAXED_CONSTEXPR inline void QPoint::setY(int ypos)
139 { yp = ypos; }
140 
manhattanLength()141 inline int Q_DECL_CONSTEXPR QPoint::manhattanLength() const
142 { return qAbs(x())+qAbs(y()); }
143 
rx()144 Q_DECL_RELAXED_CONSTEXPR inline int &QPoint::rx()
145 { return xp; }
146 
ry()147 Q_DECL_RELAXED_CONSTEXPR inline int &QPoint::ry()
148 { return yp; }
149 
150 Q_DECL_RELAXED_CONSTEXPR inline QPoint &QPoint::operator+=(const QPoint &p)
151 { xp+=p.xp; yp+=p.yp; return *this; }
152 
153 Q_DECL_RELAXED_CONSTEXPR inline QPoint &QPoint::operator-=(const QPoint &p)
154 { xp-=p.xp; yp-=p.yp; return *this; }
155 
156 Q_DECL_RELAXED_CONSTEXPR inline QPoint &QPoint::operator*=(float factor)
157 { xp = qRound(xp*factor); yp = qRound(yp*factor); return *this; }
158 
159 Q_DECL_RELAXED_CONSTEXPR inline QPoint &QPoint::operator*=(double factor)
160 { xp = qRound(xp*factor); yp = qRound(yp*factor); return *this; }
161 
162 Q_DECL_RELAXED_CONSTEXPR inline QPoint &QPoint::operator*=(int factor)
163 { xp = xp*factor; yp = yp*factor; return *this; }
164 
165 Q_DECL_CONSTEXPR inline bool operator==(const QPoint &p1, const QPoint &p2)
166 { return p1.xp == p2.xp && p1.yp == p2.yp; }
167 
168 Q_DECL_CONSTEXPR inline bool operator!=(const QPoint &p1, const QPoint &p2)
169 { return p1.xp != p2.xp || p1.yp != p2.yp; }
170 
171 Q_DECL_CONSTEXPR inline const QPoint operator+(const QPoint &p1, const QPoint &p2)
172 { return QPoint(p1.xp+p2.xp, p1.yp+p2.yp); }
173 
174 Q_DECL_CONSTEXPR inline const QPoint operator-(const QPoint &p1, const QPoint &p2)
175 { return QPoint(p1.xp-p2.xp, p1.yp-p2.yp); }
176 
177 Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &p, float factor)
178 { return QPoint(qRound(p.xp*factor), qRound(p.yp*factor)); }
179 
180 Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &p, double factor)
181 { return QPoint(qRound(p.xp*factor), qRound(p.yp*factor)); }
182 
183 Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &p, int factor)
184 { return QPoint(p.xp*factor, p.yp*factor); }
185 
186 Q_DECL_CONSTEXPR inline const QPoint operator*(float factor, const QPoint &p)
187 { return QPoint(qRound(p.xp*factor), qRound(p.yp*factor)); }
188 
189 Q_DECL_CONSTEXPR inline const QPoint operator*(double factor, const QPoint &p)
190 { return QPoint(qRound(p.xp*factor), qRound(p.yp*factor)); }
191 
192 Q_DECL_CONSTEXPR inline const QPoint operator*(int factor, const QPoint &p)
193 { return QPoint(p.xp*factor, p.yp*factor); }
194 
195 Q_DECL_CONSTEXPR inline const QPoint operator+(const QPoint &p)
196 { return p; }
197 
198 Q_DECL_CONSTEXPR inline const QPoint operator-(const QPoint &p)
199 { return QPoint(-p.xp, -p.yp); }
200 
201 Q_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 
208 Q_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
214 Q_CORE_EXPORT QDebug operator<<(QDebug, const QPoint &);
215 #endif
216 
217 
218 
219 
220 
221 class Q_CORE_EXPORT QPointF
222 {
223 public:
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 
transposed()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 
dotProduct(const QPointF & p1,const QPointF & p2)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 
267 private:
268     friend class QMatrix;
269     friend class QTransform;
270 
271     qreal xp;
272     qreal yp;
273 };
274 
275 Q_DECLARE_TYPEINFO(QPointF, Q_MOVABLE_TYPE);
276 
277 /*****************************************************************************
278   QPointF stream functions
279  *****************************************************************************/
280 #ifndef QT_NO_DATASTREAM
281 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPointF &);
282 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPointF &);
283 #endif
284 
285 /*****************************************************************************
286   QPointF inline functions
287  *****************************************************************************/
288 
QPointF()289 Q_DECL_CONSTEXPR inline QPointF::QPointF() : xp(0), yp(0) { }
290 
QPointF(qreal xpos,qreal ypos)291 Q_DECL_CONSTEXPR inline QPointF::QPointF(qreal xpos, qreal ypos) : xp(xpos), yp(ypos) { }
292 
QPointF(const QPoint & p)293 Q_DECL_CONSTEXPR inline QPointF::QPointF(const QPoint &p) : xp(p.x()), yp(p.y()) { }
294 
manhattanLength()295 Q_DECL_CONSTEXPR inline qreal QPointF::manhattanLength() const
296 {
297     return qAbs(x())+qAbs(y());
298 }
299 
isNull()300 inline bool QPointF::isNull() const
301 {
302     return qIsNull(xp) && qIsNull(yp);
303 }
304 
x()305 Q_DECL_CONSTEXPR inline qreal QPointF::x() const
306 {
307     return xp;
308 }
309 
y()310 Q_DECL_CONSTEXPR inline qreal QPointF::y() const
311 {
312     return yp;
313 }
314 
setX(qreal xpos)315 Q_DECL_RELAXED_CONSTEXPR inline void QPointF::setX(qreal xpos)
316 {
317     xp = xpos;
318 }
319 
setY(qreal ypos)320 Q_DECL_RELAXED_CONSTEXPR inline void QPointF::setY(qreal ypos)
321 {
322     yp = ypos;
323 }
324 
rx()325 Q_DECL_RELAXED_CONSTEXPR inline qreal &QPointF::rx()
326 {
327     return xp;
328 }
329 
ry()330 Q_DECL_RELAXED_CONSTEXPR inline qreal &QPointF::ry()
331 {
332     return yp;
333 }
334 
335 Q_DECL_RELAXED_CONSTEXPR inline QPointF &QPointF::operator+=(const QPointF &p)
336 {
337     xp+=p.xp;
338     yp+=p.yp;
339     return *this;
340 }
341 
342 Q_DECL_RELAXED_CONSTEXPR inline QPointF &QPointF::operator-=(const QPointF &p)
343 {
344     xp-=p.xp; yp-=p.yp; return *this;
345 }
346 
347 Q_DECL_RELAXED_CONSTEXPR inline QPointF &QPointF::operator*=(qreal c)
348 {
349     xp*=c; yp*=c; return *this;
350 }
351 
352 QT_WARNING_PUSH
353 QT_WARNING_DISABLE_CLANG("-Wfloat-equal")
354 QT_WARNING_DISABLE_GCC("-Wfloat-equal")
355 QT_WARNING_DISABLE_INTEL(1572)
356 
357 Q_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 
363 Q_DECL_CONSTEXPR inline bool operator!=(const QPointF &p1, const QPointF &p2)
364 {
365     return !(p1 == p2);
366 }
367 
368 QT_WARNING_POP
369 
370 Q_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 
375 Q_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 
380 Q_DECL_CONSTEXPR inline const QPointF operator*(const QPointF &p, qreal c)
381 {
382     return QPointF(p.xp*c, p.yp*c);
383 }
384 
385 Q_DECL_CONSTEXPR inline const QPointF operator*(qreal c, const QPointF &p)
386 {
387     return QPointF(p.xp*c, p.yp*c);
388 }
389 
390 Q_DECL_CONSTEXPR inline const QPointF operator+(const QPointF &p)
391 {
392     return p;
393 }
394 
395 Q_DECL_CONSTEXPR inline const QPointF operator-(const QPointF &p)
396 {
397     return QPointF(-p.xp, -p.yp);
398 }
399 
400 Q_DECL_RELAXED_CONSTEXPR inline QPointF &QPointF::operator/=(qreal divisor)
401 {
402     xp/=divisor;
403     yp/=divisor;
404     return *this;
405 }
406 
407 Q_DECL_CONSTEXPR inline const QPointF operator/(const QPointF &p, qreal divisor)
408 {
409     return QPointF(p.xp/divisor, p.yp/divisor);
410 }
411 
toPoint()412 Q_DECL_CONSTEXPR inline QPoint QPointF::toPoint() const
413 {
414     return QPoint(qRound(xp), qRound(yp));
415 }
416 
417 #ifndef QT_NO_DEBUG_STREAM
418 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QPointF &p);
419 #endif
420 
421 QT_END_NAMESPACE
422 
423 #endif // QPOINT_H
424