Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpoint.cpp
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// Qt-Security score:significant reason:default
4
5#include "qpoint.h"
6#include "qdatastream.h"
7
8#include <private/qdebug_p.h>
9#include <QtCore/qhashfunctions.h>
10
12
13/*!
14 \class QPoint
15 \inmodule QtCore
16 \ingroup painting
17 \reentrant
18
19 \compares equality
20 \compareswith equality QPointF
21 \endcompareswith
22
23 \brief The QPoint class defines a point in the plane using integer
24 precision.
25
26 A point is specified by a x coordinate and an y coordinate which
27 can be accessed using the x() and y() functions. The isNull()
28 function returns \c true if both x and y are set to 0. The
29 coordinates can be set (or altered) using the setX() and setY()
30 functions, or alternatively the rx() and ry() functions which
31 return references to the coordinates (allowing direct
32 manipulation).
33
34 Given a point \e p, the following statements are all equivalent:
35
36 \snippet code/src_corelib_tools_qpoint.cpp 0
37
38 A QPoint object can also be used as a vector: Addition and
39 subtraction are defined as for vectors (each component is added
40 separately). A QPoint object can also be divided or multiplied by
41 an \c int or a \c qreal.
42
43 In addition, the QPoint class provides the manhattanLength()
44 function which gives an inexpensive approximation of the length of
45 the QPoint object interpreted as a vector. Finally, QPoint objects
46 can be streamed as well as compared.
47
48 \sa QPointF, QPolygon
49*/
50
51
52/*****************************************************************************
53 QPoint member functions
54 *****************************************************************************/
55
56/*!
57 \fn QPoint::QPoint()
58
59 Constructs a null point, i.e. with coordinates (0, 0)
60
61 \sa isNull()
62*/
63
64/*!
65 \fn QPoint::QPoint(int xpos, int ypos)
66
67 Constructs a point with the given coordinates (\a xpos, \a ypos).
68
69 \sa setX(), setY()
70*/
71
72/*!
73 \fn bool QPoint::isNull() const
74
75 Returns \c true if both the x and y coordinates are set to 0,
76 otherwise returns \c false.
77*/
78
79/*!
80 \fn int QPoint::x() const
81
82 Returns the x coordinate of this point.
83
84 \sa setX(), rx()
85*/
86
87/*!
88 \fn int QPoint::y() const
89
90 Returns the y coordinate of this point.
91
92 \sa setY(), ry()
93*/
94
95/*!
96 \fn void QPoint::setX(int x)
97
98 Sets the x coordinate of this point to the given \a x coordinate.
99
100 \sa x(), setY()
101*/
102
103/*!
104 \fn void QPoint::setY(int y)
105
106 Sets the y coordinate of this point to the given \a y coordinate.
107
108 \sa y(), setX()
109*/
110
111/*!
112 \fn QPoint::transposed() const
113 \since 5.14
114
115 Returns a point with x and y coordinates exchanged:
116 \code
117 QPoint{1, 2}.transposed() // {2, 1}
118 \endcode
119
120 \sa x(), y(), setX(), setY()
121*/
122
123/*!
124 \fn int &QPoint::rx()
125
126 Returns a reference to the x coordinate of this point.
127
128 Using a reference makes it possible to directly manipulate x. For example:
129
130 \snippet code/src_corelib_tools_qpoint.cpp 1
131
132 \sa x(), setX()
133*/
134
135/*!
136 \fn int &QPoint::ry()
137
138 Returns a reference to the y coordinate of this point.
139
140 Using a reference makes it possible to directly manipulate y. For
141 example:
142
143 \snippet code/src_corelib_tools_qpoint.cpp 2
144
145 \sa y(), setY()
146*/
147
148
149/*!
150 \fn QPoint &QPoint::operator+=(const QPoint &point)
151
152 Adds the given \a point to this point and returns a reference to
153 this point. For example:
154
155 \snippet code/src_corelib_tools_qpoint.cpp 3
156
157 \sa operator-=()
158*/
159
160/*!
161 \fn QPoint &QPoint::operator-=(const QPoint &point)
162
163 Subtracts the given \a point from this point and returns a
164 reference to this point. For example:
165
166 \snippet code/src_corelib_tools_qpoint.cpp 4
167
168 \sa operator+=()
169*/
170
171/*!
172 \fn QPoint &QPoint::operator*=(float factor)
173
174 Multiplies this point's coordinates by the given \a factor, and
175 returns a reference to this point.
176
177 Note that the result is rounded to the nearest integer as points are held as
178 integers. Use QPointF for floating point accuracy.
179
180 \sa operator/=()
181*/
182
183/*!
184 \fn QPoint &QPoint::operator*=(double factor)
185
186 Multiplies this point's coordinates by the given \a factor, and
187 returns a reference to this point. For example:
188
189 \snippet code/src_corelib_tools_qpoint.cpp 5
190
191 Note that the result is rounded to the nearest integer as points are held as
192 integers. Use QPointF for floating point accuracy.
193
194 \sa operator/=()
195*/
196
197/*!
198 \fn QPoint &QPoint::operator*=(int factor)
199
200 Multiplies this point's coordinates by the given \a factor, and
201 returns a reference to this point.
202
203 \sa operator/=()
204*/
205
206/*!
207 \fn static int QPoint::dotProduct(const QPoint &p1, const QPoint &p2)
208 \since 5.1
209
210 \snippet code/src_corelib_tools_qpoint.cpp 16
211
212 Returns the dot product of \a p1 and \a p2.
213*/
214
215/*!
216 \fn bool QPoint::operator==(const QPoint &lhs, const QPoint &rhs)
217
218 Returns \c true if \a lhs and \a rhs are equal; otherwise returns
219 \c false.
220*/
221
222/*!
223 \fn bool QPoint::operator!=(const QPoint &lhs, const QPoint &rhs)
224
225 Returns \c true if \a lhs and \a rhs are not equal; otherwise returns
226 \c false.
227*/
228
229/*!
230 \fn QPoint QPoint::operator+(const QPoint &p1, const QPoint &p2)
231
232 Returns a QPoint object that is the sum of the given points, \a p1
233 and \a p2; each component is added separately.
234
235 \sa QPoint::operator+=()
236*/
237
238/*!
239 \fn Point QPoint::operator-(const QPoint &p1, const QPoint &p2)
240
241 Returns a QPoint object that is formed by subtracting \a p2 from
242 \a p1; each component is subtracted separately.
243
244 \sa QPoint::operator-=()
245*/
246
247/*!
248 \fn QPoint QPoint::operator*(const QPoint &point, float factor)
249
250 Returns a copy of the given \a point multiplied by the given \a factor.
251
252 Note that the result is rounded to the nearest integer as points
253 are held as integers. Use QPointF for floating point accuracy.
254
255 \sa QPoint::operator*=()
256*/
257
258/*!
259 \fn QPoint QPoint::operator*(const QPoint &point, double factor)
260
261 Returns a copy of the given \a point multiplied by the given \a factor.
262
263 Note that the result is rounded to the nearest integer as points
264 are held as integers. Use QPointF for floating point accuracy.
265
266 \sa QPoint::operator*=()
267*/
268
269/*!
270 \fn QPoint QPoint::operator*(const QPoint &point, int factor)
271
272 Returns a copy of the given \a point multiplied by the given \a factor.
273
274 \sa QPoint::operator*=()
275*/
276
277/*!
278 \fn QPoint QPoint::operator*(float factor, const QPoint &point)
279 \overload
280
281 Returns a copy of the given \a point multiplied by the given \a factor.
282
283 Note that the result is rounded to the nearest integer as points
284 are held as integers. Use QPointF for floating point accuracy.
285
286 \sa QPoint::operator*=()
287*/
288
289/*!
290 \fn QPoint QPoint::operator*(double factor, const QPoint &point)
291 \overload
292
293 Returns a copy of the given \a point multiplied by the given \a factor.
294
295 Note that the result is rounded to the nearest integer as points
296 are held as integers. Use QPointF for floating point accuracy.
297
298 \sa QPoint::operator*=()
299*/
300
301/*!
302 \fn QPoint QPoint::operator*(int factor, const QPoint &point)
303 \overload
304
305 Returns a copy of the given \a point multiplied by the given \a factor.
306
307 \sa QPoint::operator*=()
308*/
309
310/*!
311 \fn QPoint QPoint::operator+(const QPoint &point)
312 \since 5.0
313
314 Returns \a point unmodified.
315*/
316
317/*!
318 \fn QPoint QPoint::operator-(const QPoint &point)
319 \overload
320
321 Returns a QPoint object that is formed by changing the sign of
322 both components of the given \a point.
323
324 Equivalent to \c{QPoint(0,0) - point}.
325*/
326
327/*!
328 \fn QPoint &QPoint::operator/=(qreal divisor)
329 \overload
330
331 Divides both x and y by the given \a divisor, and returns a reference to this
332 point. For example:
333
334 \snippet code/src_corelib_tools_qpoint.cpp 6
335
336 Note that the result is rounded to the nearest integer as points are held as
337 integers. Use QPointF for floating point accuracy.
338
339 \sa operator*=()
340*/
341
342/*!
343 \fn const QPoint QPoint::operator/(const QPoint &point, qreal divisor)
344
345 Returns the QPoint formed by dividing both components of the given \a point
346 by the given \a divisor.
347
348 Note that the result is rounded to the nearest integer as points are held as
349 integers. Use QPointF for floating point accuracy.
350
351 \sa QPoint::operator/=()
352*/
353
354/*!
355 \fn QPoint::toPointF() const
356 \since 6.4
357
358 Returns this point as a point with floating point accuracy.
359
360 \sa QPointF::toPoint()
361*/
362
363/*****************************************************************************
364 QPoint stream functions
365 *****************************************************************************/
366#ifndef QT_NO_DATASTREAM
367/*!
368 \fn QDataStream &operator<<(QDataStream &stream, const QPoint &point)
369 \relates QPoint
370
371 Writes the given \a point to the given \a stream and returns a
372 reference to the stream.
373
374 \sa {Serializing Qt Data Types}
375*/
376
377QDataStream &operator<<(QDataStream &s, const QPoint &p)
378{
379 if (s.version() == 1)
380 s << (qint16)p.x() << (qint16)p.y();
381 else
382 s << (qint32)p.x() << (qint32)p.y();
383 return s;
384}
385
386/*!
387 \fn QDataStream &operator>>(QDataStream &stream, QPoint &point)
388 \relates QPoint
389
390 Reads a point from the given \a stream into the given \a point
391 and returns a reference to the stream.
392
393 \sa {Serializing Qt Data Types}
394*/
395
396QDataStream &operator>>(QDataStream &s, QPoint &p)
397{
398 if (s.version() == 1) {
399 qint16 x, y;
400 s >> x; p.rx() = x;
401 s >> y; p.ry() = y;
402 }
403 else {
404 qint32 x, y;
405 s >> x; p.rx() = x;
406 s >> y; p.ry() = y;
407 }
408 return s;
409}
410
411#endif // QT_NO_DATASTREAM
412/*!
413 \fn int QPoint::manhattanLength() const
414
415 Returns the sum of the absolute values of x() and y(),
416 traditionally known as the "Manhattan length" of the vector from
417 the origin to the point. For example:
418
419 \snippet code/src_corelib_tools_qpoint.cpp 7
420
421 This is a useful, and quick to calculate, approximation to the
422 true length:
423
424 \snippet code/src_corelib_tools_qpoint.cpp 8
425
426 The tradition of "Manhattan length" arises because such distances
427 apply to travelers who can only travel on a rectangular grid, like
428 the streets of Manhattan.
429*/
430
431#ifndef QT_NO_DEBUG_STREAM
432QDebug operator<<(QDebug dbg, const QPoint &p)
433{
434 QDebugStateSaver saver(dbg);
435 dbg.nospace();
436 dbg << "QPoint" << '(';
437 QtDebugUtils::formatQPoint(dbg, p);
438 dbg << ')';
439 return dbg;
440}
441
442QDebug operator<<(QDebug dbg, const QPointF &p)
443{
444 QDebugStateSaver saver(dbg);
445 dbg.nospace();
446 dbg << "QPointF" << '(';
447 QtDebugUtils::formatQPoint(dbg, p);
448 dbg << ')';
449 return dbg;
450}
451#endif
452
453/*!
454 \qhashold{QHash}
455 \since 6.0
456*/
457size_t qHash(QPoint key, size_t seed) noexcept
458{
459 return qHashMulti(seed, key.x(), key.y());
460}
461
462/*!
463 \class QPointF
464 \inmodule QtCore
465 \ingroup painting
466 \reentrant
467
468 \compares equality
469 \compareswith equality QPoint
470 \endcompareswith
471
472 \brief The QPointF class defines a point in the plane using
473 floating point precision.
474
475 A point is specified by a x coordinate and an y coordinate which
476 can be accessed using the x() and y() functions. The coordinates
477 of the point are specified using finite floating point numbers for
478 accuracy. The isNull() function returns \c true if both x and y are
479 set to 0.0. The coordinates can be set (or altered) using the setX()
480 and setY() functions, or alternatively the rx() and ry() functions which
481 return references to the coordinates (allowing direct
482 manipulation).
483
484 Given a point \e p, the following statements are all equivalent:
485
486 \snippet code/src_corelib_tools_qpoint.cpp 9
487
488 A QPointF object can also be used as a vector: Addition and
489 subtraction are defined as for vectors (each component is added
490 separately). A QPointF object can also be divided or multiplied by
491 an \c int or a \c qreal.
492
493 In addition, the QPointF class provides a constructor converting a
494 QPoint object into a QPointF object, and a corresponding toPoint()
495 function which returns a QPoint copy of \e this point. Finally,
496 QPointF objects can be streamed as well as compared.
497
498 \sa QPoint, QPolygonF
499*/
500
501/*!
502 \fn QPointF::QPointF()
503
504 Constructs a null point, i.e. with coordinates (0.0, 0.0)
505
506 \sa isNull()
507*/
508
509/*!
510 \fn QPointF::QPointF(const QPoint &point)
511
512 Constructs a copy of the given \a point.
513
514 \sa toPoint(), QPoint::toPointF()
515*/
516
517/*!
518 \fn QPointF::QPointF(qreal xpos, qreal ypos)
519
520 Constructs a point with the given coordinates (\a xpos, \a ypos).
521
522 \sa setX(), setY()
523*/
524
525/*!
526 \fn bool QPointF::isNull() const
527
528 Returns \c true if both the x and y coordinates are set to 0.0 (ignoring
529 the sign); otherwise returns \c false.
530*/
531
532
533/*!
534 \fn qreal QPointF::manhattanLength() const
535 \since 4.6
536
537 Returns the sum of the absolute values of x() and y(),
538 traditionally known as the "Manhattan length" of the vector from
539 the origin to the point.
540
541 \sa QPoint::manhattanLength()
542*/
543
544/*!
545 \fn qreal QPointF::x() const
546
547 Returns the x coordinate of this point.
548
549 \sa setX(), rx()
550*/
551
552/*!
553 \fn qreal QPointF::y() const
554
555 Returns the y coordinate of this point.
556
557 \sa setY(), ry()
558*/
559
560/*!
561 \fn void QPointF::setX(qreal x)
562
563 Sets the x coordinate of this point to the given finite \a x coordinate.
564
565 \sa x(), setY()
566*/
567
568/*!
569 \fn void QPointF::setY(qreal y)
570
571 Sets the y coordinate of this point to the given finite \a y coordinate.
572
573 \sa y(), setX()
574*/
575
576/*!
577 \fn QPointF::transposed() const
578 \since 5.14
579
580 Returns a point with x and y coordinates exchanged:
581 \code
582 QPointF{1.0, 2.0}.transposed() // {2.0, 1.0}
583 \endcode
584
585 \sa x(), y(), setX(), setY()
586*/
587
588/*!
589 \fn qreal& QPointF::rx()
590
591 Returns a reference to the x coordinate of this point.
592
593 Using a reference makes it possible to directly manipulate x. For example:
594
595 \snippet code/src_corelib_tools_qpoint.cpp 10
596
597 \sa x(), setX()
598*/
599
600/*!
601 \fn qreal& QPointF::ry()
602
603 Returns a reference to the y coordinate of this point.
604
605 Using a reference makes it possible to directly manipulate y. For example:
606
607 \snippet code/src_corelib_tools_qpoint.cpp 11
608
609 \sa y(), setY()
610*/
611
612/*!
613 \fn QPointF& QPointF::operator+=(const QPointF &point)
614
615 Adds the given \a point to this point and returns a reference to
616 this point. For example:
617
618 \snippet code/src_corelib_tools_qpoint.cpp 12
619
620 \sa operator-=()
621*/
622
623/*!
624 \fn QPointF& QPointF::operator-=(const QPointF &point)
625
626 Subtracts the given \a point from this point and returns a reference
627 to this point. For example:
628
629 \snippet code/src_corelib_tools_qpoint.cpp 13
630
631 \sa operator+=()
632*/
633
634/*!
635 \fn QPointF& QPointF::operator*=(qreal factor)
636
637 Multiplies this point's coordinates by the given finite \a factor, and
638 returns a reference to this point. For example:
639
640 \snippet code/src_corelib_tools_qpoint.cpp 14
641
642 \sa operator/=()
643*/
644
645/*!
646 \fn QPointF& QPointF::operator/=(qreal divisor)
647
648 Divides both x and y by the given \a divisor, and returns a reference
649 to this point. For example:
650
651 \snippet code/src_corelib_tools_qpoint.cpp 15
652
653 The \a divisor must not be zero or NaN.
654
655 \sa operator*=()
656*/
657
658/*!
659 \fn QPointF QPointF::operator+(const QPointF &p1, const QPointF &p2)
660
661 Returns a QPointF object that is the sum of the given points, \a p1
662 and \a p2; each component is added separately.
663
664 \sa QPointF::operator+=()
665*/
666
667/*!
668 \fn QPointF QPointF::operator-(const QPointF &p1, const QPointF &p2)
669
670 Returns a QPointF object that is formed by subtracting \a p2 from \a p1;
671 each component is subtracted separately.
672
673 \sa QPointF::operator-=()
674*/
675
676/*!
677 \fn QPointF QPointF::operator*(const QPointF &point, qreal factor)
678
679 Returns a copy of the given \a point, multiplied by the given finite \a factor.
680
681 \sa QPointF::operator*=()
682*/
683
684/*!
685 \fn QPointF QPointF::operator*(qreal factor, const QPointF &point)
686
687 \overload
688
689 Returns a copy of the given \a point, multiplied by the given finite \a factor.
690*/
691
692/*!
693 \fn QPointF QPointF::operator+(const QPointF &point)
694 \since 5.0
695
696 Returns \a point unmodified.
697*/
698
699/*!
700 \fn QPointF QPointF::operator-(const QPointF &point)
701 \overload
702
703 Returns a QPointF object that is formed by changing the sign of
704 each component of the given \a point.
705
706 Equivalent to \c {QPointF(0,0) - point}.
707*/
708
709/*!
710 \fn QPointF QPointF::operator/(const QPointF &point, qreal divisor)
711
712 Returns the QPointF object formed by dividing each component of
713 the given \a point by the given \a divisor.
714
715 The \a divisor must not be zero or NaN.
716
717 \sa QPointF::operator/=()
718*/
719
720/*!
721 \fn QPoint QPointF::toPoint() const
722
723 Rounds the coordinates of this point to the nearest integer, and
724 returns a QPoint object with the rounded coordinates.
725
726 \sa QPointF(), QPoint::toPointF()
727*/
728
729/*!
730 \fn static qreal QPointF::dotProduct(const QPointF &p1, const QPointF &p2)
731 \since 5.1
732
733 \snippet code/src_corelib_tools_qpoint.cpp 17
734
735 Returns the dot product of \a p1 and \a p2.
736*/
737
738/*!
739 \fn bool QPointF::operator==(const QPointF &lhs, const QPointF &rhs)
740
741 Returns \c true if \a lhs is approximately equal to \a rhs; otherwise
742 returns \c false.
743
744 \warning This function does not check for strict equality; instead,
745 it uses a fuzzy comparison to compare the points' coordinates.
746
747 \sa qFuzzyCompare
748*/
749
750/*!
751 \fn bool QPointF::operator!=(const QPointF &lhs, const QPointF &rhs)
752
753 Returns \c true if \a lhs is sufficiently different from \a rhs;
754 otherwise returns \c false.
755
756 \warning This function does not check for strict inequality; instead,
757 it uses a fuzzy comparison to compare the points' coordinates.
758
759 \sa qFuzzyCompare
760*/
761
762/*!
763 \fn bool QPointF::qFuzzyCompare(const QPointF &p1, const QPointF &p2)
764 \since 6.8
765
766 Returns \c true if \a p1 is approximately equal to \a p2; otherwise
767 returns \c false.
768
769 \sa qFuzzyIsNull
770*/
771
772/*!
773 \fn bool QPointF::qFuzzyIsNull(const QPointF &point)
774 \since 6.8
775
776 Returns \c true if \a point is approximately equal to a point
777 \c {(0.0, 0.0)}.
778
779 \sa qFuzzyCompare
780*/
781
782#ifndef QT_NO_DATASTREAM
783/*!
784 \fn QDataStream &operator<<(QDataStream &stream, const QPointF &point)
785 \relates QPointF
786
787 Writes the given \a point to the given \a stream and returns a
788 reference to the stream.
789
790 \sa {Serializing Qt Data Types}
791*/
792
793QDataStream &operator<<(QDataStream &s, const QPointF &p)
794{
795 s << double(p.x()) << double(p.y());
796 return s;
797}
798
799/*!
800 \fn QDataStream &operator>>(QDataStream &stream, QPointF &point)
801 \relates QPointF
802
803 Reads a point from the given \a stream into the given \a point
804 and returns a reference to the stream.
805
806 \sa {Serializing Qt Data Types}
807*/
808
809QDataStream &operator>>(QDataStream &s, QPointF &p)
810{
811 double x, y;
812 s >> x;
813 s >> y;
814 p.setX(qreal(x));
815 p.setY(qreal(y));
816 return s;
817}
818#endif // QT_NO_DATASTREAM
819
820QT_END_NAMESPACE
\inmodule QtCore\reentrant
Definition qpoint.h:232
\inmodule QtCore\reentrant
Definition qpoint.h:30
Combined button and popup list for selecting options.
QDataStream & operator>>(QDataStream &s, QPoint &p)
Definition qpoint.cpp:396
QDebug operator<<(QDebug dbg, const QPoint &p)
Definition qpoint.cpp:432
size_t qHash(QPoint key, size_t seed) noexcept
\qhashold{QHash}
Definition qpoint.cpp:457
QDataStream & operator>>(QDataStream &s, QPointF &p)
Definition qpoint.cpp:809
QDebug operator<<(QDebug dbg, const QPointF &p)
Definition qpoint.cpp:442