source: trunk/src/corelib/tools/qpoint.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 16.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qpoint.h"
43#include "qdatastream.h"
44#include "qdebug.h"
45
46QT_BEGIN_NAMESPACE
47
48/*!
49 \class QPoint
50 \ingroup multimedia
51
52 \brief The QPoint class defines a point in the plane using integer
53 precision.
54
55 A point is specified by a x coordinate and an y coordinate which
56 can be accessed using the x() and y() functions. The isNull()
57 function returns true if both x and y are set to 0. The
58 coordinates can be set (or altered) using the setX() and setY()
59 functions, or alternatively the rx() and ry() functions which
60 return references to the coordinates (allowing direct
61 manipulation).
62
63 Given a point \e p, the following statements are all equivalent:
64
65 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 0
66
67 A QPoint object can also be used as a vector: Addition and
68 subtraction are defined as for vectors (each component is added
69 separately). A QPoint object can also be divided or multiplied by
70 an \c int or a \c qreal.
71
72 In addition, the QPoint class provides the manhattanLength()
73 function which gives an inexpensive approximation of the length of
74 the QPoint object interpreted as a vector. Finally, QPoint objects
75 can be streamed as well as compared.
76
77 \sa QPointF, QPolygon
78*/
79
80
81/*****************************************************************************
82 QPoint member functions
83 *****************************************************************************/
84
85/*!
86 \fn QPoint::QPoint()
87
88 Constructs a null point, i.e. with coordinates (0, 0)
89
90 \sa isNull()
91*/
92
93/*!
94 \fn QPoint::QPoint(int x, int y)
95
96 Constructs a point with the given coordinates (\a x, \a y).
97
98 \sa setX(), setY()
99*/
100
101/*!
102 \fn bool QPoint::isNull() const
103
104 Returns true if both the x and y coordinates are set to 0,
105 otherwise returns false.
106*/
107
108/*!
109 \fn int QPoint::x() const
110
111 Returns the x coordinate of this point.
112
113 \sa setX(), rx()
114*/
115
116/*!
117 \fn int QPoint::y() const
118
119 Returns the y coordinate of this point.
120
121 \sa setY(), ry()
122*/
123
124/*!
125 \fn void QPoint::setX(int x)
126
127 Sets the x coordinate of this point to the given \a x coordinate.
128
129 \sa x() setY()
130*/
131
132/*!
133 \fn void QPoint::setY(int y)
134
135 Sets the y coordinate of this point to the given \a y coordinate.
136
137 \sa y() setX()
138*/
139
140
141/*!
142 \fn int &QPoint::rx()
143
144 Returns a reference to the x coordinate of this point.
145
146 Using a reference makes it possible to directly manipulate x. For example:
147
148 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 1
149
150 \sa x() setX()
151*/
152
153/*!
154 \fn int &QPoint::ry()
155
156 Returns a reference to the y coordinate of this point.
157
158 Using a reference makes it possible to directly manipulate y. For
159 example:
160
161 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 2
162
163 \sa y(), setY()
164*/
165
166
167/*!
168 \fn QPoint &QPoint::operator+=(const QPoint &point)
169
170 Adds the given \a point to this point and returns a reference to
171 this point. For example:
172
173 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 3
174
175 \sa operator-=()
176*/
177
178/*!
179 \fn QPoint &QPoint::operator-=(const QPoint &point)
180
181 Subtracts the given \a point from this point and returns a
182 reference to this point. For example:
183
184 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 4
185
186 \sa operator+=()
187*/
188
189/*!
190 \fn QPoint &QPoint::operator*=(qreal factor)
191
192 Multiplies this point's coordinates by the given \a factor, and
193 returns a reference to this point. For example:
194
195 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 5
196
197 Note that the result is rounded to the nearest integer as points are held as
198 integers. Use QPointF for floating point accuracy.
199
200 \sa operator/=()
201*/
202
203
204/*!
205 \fn bool operator==(const QPoint &p1, const QPoint &p2)
206 \relates QPoint
207
208 Returns true if \a p1 and \a p2 are equal; otherwise returns
209 false.
210*/
211
212/*!
213 \fn bool operator!=(const QPoint &p1, const QPoint &p2)
214 \relates QPoint
215
216 Returns true if \a p1 and \a p2 are not equal; otherwise returns false.
217*/
218
219/*!
220 \fn const QPoint operator+(const QPoint &p1, const QPoint &p2)
221 \relates QPoint
222
223 Returns a QPoint object that is the sum of the given points, \a p1
224 and \a p2; each component is added separately.
225
226 \sa QPoint::operator+=()
227*/
228
229/*!
230 \fn const QPoint operator-(const QPoint &p1, const QPoint &p2)
231 \relates QPoint
232
233 Returns a QPoint object that is formed by subtracting \a p2 from
234 \a p1; each component is subtracted separately.
235
236 \sa QPoint::operator-=()
237*/
238
239/*!
240 \fn const QPoint operator*(const QPoint &point, qreal factor)
241 \relates QPoint
242
243 Returns a copy of the given \a point multiplied by the given \a factor.
244
245 Note that the result is rounded to the nearest integer as points
246 are held as integers. Use QPointF for floating point accuracy.
247
248 \sa QPoint::operator*=()
249*/
250
251/*!
252 \fn const QPoint operator*(qreal factor, const QPoint &point)
253 \overload
254 \relates QPoint
255
256 Returns a copy of the given \a point multiplied by the given \a factor.
257*/
258
259/*!
260 \fn const QPoint operator-(const QPoint &point)
261 \overload
262 \relates QPoint
263
264 Returns a QPoint object that is formed by changing the sign of
265 both components of the given \a point.
266
267 Equivalent to \c{QPoint(0,0) - point}.
268*/
269
270/*!
271 \fn QPoint &QPoint::operator/=(qreal divisor)
272 \overload
273
274 Divides both x and y by the given \a divisor, and returns a reference to this
275 point. For example:
276
277 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 6
278
279 Note that the result is rounded to the nearest integer as points are held as
280 integers. Use QPointF for floating point accuracy.
281
282 \sa operator*=()
283*/
284
285/*!
286 \fn const QPoint operator/(const QPoint &point, qreal divisor)
287 \relates QPoint
288
289 Returns the QPoint formed by dividing both components of the given \a point
290 by the given \a divisor.
291
292 Note that the result is rounded to the nearest integer as points are held as
293 integers. Use QPointF for floating point accuracy.
294
295 \sa QPoint::operator/=()
296*/
297
298/*****************************************************************************
299 QPoint stream functions
300 *****************************************************************************/
301#ifndef QT_NO_DATASTREAM
302/*!
303 \fn QDataStream &operator<<(QDataStream &stream, const QPoint &point)
304 \relates QPoint
305
306 Writes the given \a point to the given \a stream and returns a
307 reference to the stream.
308
309 \sa {Format of the QDataStream Operators}
310*/
311
312QDataStream &operator<<(QDataStream &s, const QPoint &p)
313{
314 if (s.version() == 1)
315 s << (qint16)p.x() << (qint16)p.y();
316 else
317 s << (qint32)p.x() << (qint32)p.y();
318 return s;
319}
320
321/*!
322 \fn QDataStream &operator>>(QDataStream &stream, QPoint &point)
323 \relates QPoint
324
325 Reads a point from the given \a stream into the given \a point
326 and returns a reference to the stream.
327
328 \sa {Format of the QDataStream Operators}
329*/
330
331QDataStream &operator>>(QDataStream &s, QPoint &p)
332{
333 if (s.version() == 1) {
334 qint16 x, y;
335 s >> x; p.rx() = x;
336 s >> y; p.ry() = y;
337 }
338 else {
339 qint32 x, y;
340 s >> x; p.rx() = x;
341 s >> y; p.ry() = y;
342 }
343 return s;
344}
345
346#endif // QT_NO_DATASTREAM
347/*!
348 Returns the sum of the absolute values of x() and y(),
349 traditionally known as the "Manhattan length" of the vector from
350 the origin to the point. For example:
351
352 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 7
353
354 This is a useful, and quick to calculate, approximation to the
355 true length:
356
357 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 8
358
359 The tradition of "Manhattan length" arises because such distances
360 apply to travelers who can only travel on a rectangular grid, like
361 the streets of Manhattan.
362*/
363int QPoint::manhattanLength() const
364{
365 return qAbs(x())+qAbs(y());
366}
367
368#ifndef QT_NO_DEBUG_STREAM
369QDebug operator<<(QDebug dbg, const QPoint &p) {
370 dbg.nospace() << "QPoint(" << p.x() << ',' << p.y() << ')';
371 return dbg.space();
372}
373
374QDebug operator<<(QDebug d, const QPointF &p)
375{
376 d.nospace() << "QPointF(" << p.x() << ", " << p.y() << ")";
377 return d;
378}
379#endif
380
381/*!
382 \class QPointF
383 \ingroup multimedia
384
385 \brief The QPointF class defines a point in the plane using
386 floating point precision.
387
388 A point is specified by a x coordinate and an y coordinate which
389 can be accessed using the x() and y() functions. The coordinates
390 of the point are specified using floating point numbers for
391 accuracy. The isNull() function returns true if both x and y are
392 set to 0.0. The coordinates can be set (or altered) using the setX()
393 and setY() functions, or alternatively the rx() and ry() functions which
394 return references to the coordinates (allowing direct
395 manipulation).
396
397 Given a point \e p, the following statements are all equivalent:
398
399 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 9
400
401 A QPointF object can also be used as a vector: Addition and
402 subtraction are defined as for vectors (each component is added
403 separately). A QPointF object can also be divided or multiplied by
404 an \c int or a \c qreal.
405
406 In addition, the QPointF class provides a constructor converting a
407 QPoint object into a QPointF object, and a corresponding toPoint()
408 function which returns a QPoint copy of \e this point. Finally,
409 QPointF objects can be streamed as well as compared.
410
411 \sa QPoint, QPolygonF
412*/
413
414/*!
415 \fn QPointF::QPointF()
416
417 Constructs a null point, i.e. with coordinates (0.0, 0.0)
418
419 \sa isNull()
420*/
421
422/*!
423 \fn QPointF::QPointF(const QPoint &point)
424
425 Constructs a copy of the given \a point.
426
427 \sa toPoint()
428*/
429
430/*!
431 \fn QPointF::QPointF(qreal x, qreal y)
432
433 Constructs a point with the given coordinates (\a x, \a y).
434
435 \sa setX(), setY()
436*/
437
438/*!
439 \fn bool QPointF::isNull() const
440
441 Returns true if both the x and y coordinates are set to 0.0,
442 otherwise returns false.
443*/
444
445/*!
446 \fn qreal QPointF::x() const
447
448 Returns the x-coordinate of this point.
449
450 \sa setX(), rx()
451*/
452
453/*!
454 \fn qreal QPointF::y() const
455
456 Returns the y-coordinate of this point.
457
458 \sa setY(), ry()
459*/
460
461/*!
462 \fn void QPointF::setX(qreal x)
463
464 Sets the x coordinate of this point to the given \a x coordinate.
465
466 \sa x() setY()
467*/
468
469/*!
470 \fn void QPointF::setY(qreal y)
471
472 Sets the y coordinate of this point to the given \a y coordinate.
473
474 \sa y(), setX()
475*/
476
477/*!
478 \fn qreal& QPointF::rx()
479
480 Returns a reference to the x coordinate of this point.
481
482 Using a reference makes it possible to directly manipulate x. For example:
483
484 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 10
485
486 \sa x(), setX()
487*/
488
489/*!
490 \fn qreal& QPointF::ry()
491
492 Returns a reference to the y coordinate of this point.
493
494 Using a reference makes it possible to directly manipulate y. For example:
495
496 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 11
497
498 \sa y() setY()
499*/
500
501/*!
502 \fn QPointF& QPointF::operator+=(const QPointF &point)
503
504 Adds the given \a point to this point and returns a reference to
505 this point. For example:
506
507 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 12
508
509 \sa operator-=()
510*/
511
512/*!
513 \fn QPointF& QPointF::operator-=(const QPointF &point)
514
515 Subtracts the given \a point from this point and returns a reference
516 to this point. For example:
517
518 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 13
519
520 \sa operator+=()
521*/
522
523/*!
524 \fn QPointF& QPointF::operator*=(qreal factor)
525
526 Multiplies this point's coordinates by the given \a factor, and
527 returns a reference to this point. For example:
528
529 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 14
530
531 \sa operator/=()
532*/
533
534/*!
535 \fn QPointF& QPointF::operator/=(qreal divisor)
536
537 Divides both x and y by the given \a divisor, and returns a reference
538 to this point. For example:
539
540 \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 15
541
542 \sa operator*=()
543*/
544
545/*!
546 \fn const QPointF operator+(const QPointF &p1, const QPointF &p2)
547 \relates QPointF
548
549 Returns a QPointF object that is the sum of the given points, \a p1
550 and \a p2; each component is added separately.
551
552 \sa QPointF::operator+=()
553*/
554
555/*!
556 \fn const QPointF operator-(const QPointF &p1, const QPointF &p2)
557 \relates QPointF
558
559 Returns a QPointF object that is formed by subtracting \a p2 from \a p1;
560 each component is subtracted separately.
561
562 \sa QPointF::operator-=()
563*/
564
565/*!
566 \fn const QPointF operator*(const QPointF &point, qreal factor)
567 \relates QPointF
568
569 Returns a copy of the given \a point, multiplied by the given \a factor.
570
571 \sa QPointF::operator*=()
572*/
573
574/*!
575 \fn const QPointF operator*(qreal factor, const QPointF &point)
576 \relates QPointF
577
578 \overload
579
580 Returns a copy of the given \a point, multiplied by the given \a factor.
581*/
582
583/*!
584 \fn const QPointF operator-(const QPointF &point)
585 \relates QPointF
586 \overload
587
588 Returns a QPointF object that is formed by changing the sign of
589 both components of the given \a point.
590
591 Equivalent to \c {QPointF(0,0) - point}.
592*/
593
594/*!
595 \fn const QPointF operator/(const QPointF &point, qreal divisor)
596 \relates QPointF
597
598 Returns the QPointF object formed by dividing both components of
599 the given \a point by the given \a divisor.
600
601 \sa QPointF::operator/=()
602*/
603
604/*!
605 \fn QPoint QPointF::toPoint() const
606
607 Rounds the coordinates of this point to the nearest integer, and
608 returns a QPoint object with the rounded coordinates.
609
610 \sa QPointF()
611*/
612
613/*!
614 \fn bool operator==(const QPointF &p1, const QPointF &p2)
615 \relates QPointF
616
617 Returns true if \a p1 is equal to \a p2; otherwise returns false.
618*/
619
620/*!
621 \fn bool operator!=(const QPointF &p1, const QPointF &p2);
622 \relates QPointF
623
624 Returns true if \a p1 is not equal to \a p2; otherwise returns false.
625*/
626
627#ifndef QT_NO_DATASTREAM
628/*!
629 \fn QDataStream &operator<<(QDataStream &stream, const QPointF &point)
630 \relates QPointF
631
632 Writes the given \a point to the given \a stream and returns a
633 reference to the stream.
634
635 \sa {Format of the QDataStream Operators}
636*/
637
638QDataStream &operator<<(QDataStream &s, const QPointF &p)
639{
640 s << double(p.x()) << double(p.y());
641 return s;
642}
643
644/*!
645 \fn QDataStream &operator>>(QDataStream &stream, QPointF &point)
646 \relates QPointF
647
648 Reads a point from the given \a stream into the given \a point
649 and returns a reference to the stream.
650
651 \sa {Format of the QDataStream Operators}
652*/
653
654QDataStream &operator>>(QDataStream &s, QPointF &p)
655{
656 double x, y;
657 s >> x;
658 s >> y;
659 p.setX(qreal(x));
660 p.setY(qreal(y));
661 return s;
662}
663#endif // QT_NO_DATASTREAM
664
665QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.