1 | /****************************************************************************
|
---|
2 | ** $Id: qpoint.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Implementation of QPoint class
|
---|
5 | **
|
---|
6 | ** Created : 931028
|
---|
7 | **
|
---|
8 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
9 | **
|
---|
10 | ** This file is part of the kernel module of the Qt GUI Toolkit.
|
---|
11 | **
|
---|
12 | ** This file may be distributed under the terms of the Q Public License
|
---|
13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
15 | **
|
---|
16 | ** This file may be distributed and/or modified under the terms of the
|
---|
17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
19 | ** packaging of this file.
|
---|
20 | **
|
---|
21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
22 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
23 | ** Agreement provided with the Software.
|
---|
24 | **
|
---|
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
27 | **
|
---|
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
29 | ** information about Qt Commercial License Agreements.
|
---|
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
32 | **
|
---|
33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
34 | ** not clear to you.
|
---|
35 | **
|
---|
36 | **********************************************************************/
|
---|
37 |
|
---|
38 | #include "qpoint.h"
|
---|
39 | #include "qdatastream.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \class QPoint qpoint.h
|
---|
44 | \brief The QPoint class defines a point in the plane.
|
---|
45 |
|
---|
46 | \ingroup images
|
---|
47 | \ingroup graphics
|
---|
48 | \mainclass
|
---|
49 |
|
---|
50 | A point is specified by an x coordinate and a y coordinate.
|
---|
51 |
|
---|
52 | The coordinate type is \c QCOORD (a 32-bit integer). The minimum
|
---|
53 | value of \c QCOORD is \c QCOORD_MIN (-2147483648) and the maximum
|
---|
54 | value is \c QCOORD_MAX (2147483647).
|
---|
55 |
|
---|
56 | The coordinates are accessed by the functions x() and y(); they
|
---|
57 | can be set by setX() and setY() or by the reference functions rx()
|
---|
58 | and ry().
|
---|
59 |
|
---|
60 | Given a point \e p, the following statements are all equivalent:
|
---|
61 | \code
|
---|
62 | p.setX( p.x() + 1 );
|
---|
63 | p += QPoint( 1, 0 );
|
---|
64 | p.rx()++;
|
---|
65 | \endcode
|
---|
66 |
|
---|
67 | A QPoint can also be used as a vector. Addition and subtraction
|
---|
68 | of QPoints are defined as for vectors (each component is added
|
---|
69 | separately). You can divide or multiply a QPoint by an \c int or a
|
---|
70 | \c double. The function manhattanLength() gives an inexpensive
|
---|
71 | approximation of the length of the QPoint interpreted as a vector.
|
---|
72 |
|
---|
73 | Example:
|
---|
74 | \code
|
---|
75 | //QPoint oldPos is defined somewhere else
|
---|
76 | MyWidget::mouseMoveEvent( QMouseEvent *e )
|
---|
77 | {
|
---|
78 | QPoint vector = e->pos() - oldPos;
|
---|
79 | if ( vector.manhattanLength() > 3 )
|
---|
80 | ... //mouse has moved more than 3 pixels since oldPos
|
---|
81 | }
|
---|
82 | \endcode
|
---|
83 |
|
---|
84 | QPoints can be compared for equality or inequality, and they can
|
---|
85 | be written to and read from a QStream.
|
---|
86 |
|
---|
87 | \sa QPointArray QSize, QRect
|
---|
88 | */
|
---|
89 |
|
---|
90 |
|
---|
91 | /*****************************************************************************
|
---|
92 | QPoint member functions
|
---|
93 | *****************************************************************************/
|
---|
94 |
|
---|
95 | /*!
|
---|
96 | \fn QPoint::QPoint()
|
---|
97 |
|
---|
98 | Constructs a point with coordinates (0, 0) (isNull() returns TRUE).
|
---|
99 | */
|
---|
100 |
|
---|
101 | /*!
|
---|
102 | \fn QPoint::QPoint( int xpos, int ypos )
|
---|
103 |
|
---|
104 | Constructs a point with x value \a xpos and y value \a ypos.
|
---|
105 | */
|
---|
106 |
|
---|
107 | /*!
|
---|
108 | \fn bool QPoint::isNull() const
|
---|
109 |
|
---|
110 | Returns TRUE if both the x value and the y value are 0; otherwise
|
---|
111 | returns FALSE.
|
---|
112 | */
|
---|
113 |
|
---|
114 | /*!
|
---|
115 | \fn int QPoint::x() const
|
---|
116 |
|
---|
117 | Returns the x coordinate of the point.
|
---|
118 |
|
---|
119 | \sa setX() y()
|
---|
120 | */
|
---|
121 |
|
---|
122 | /*!
|
---|
123 | \fn int QPoint::y() const
|
---|
124 |
|
---|
125 | Returns the y coordinate of the point.
|
---|
126 |
|
---|
127 | \sa setY() x()
|
---|
128 | */
|
---|
129 |
|
---|
130 | /*!
|
---|
131 | \fn void QPoint::setX( int x )
|
---|
132 |
|
---|
133 | Sets the x coordinate of the point to \a x.
|
---|
134 |
|
---|
135 | \sa x() setY()
|
---|
136 | */
|
---|
137 |
|
---|
138 | /*!
|
---|
139 | \fn void QPoint::setY( int y )
|
---|
140 |
|
---|
141 | Sets the y coordinate of the point to \a y.
|
---|
142 |
|
---|
143 | \sa y() setX()
|
---|
144 | */
|
---|
145 |
|
---|
146 |
|
---|
147 | /*!
|
---|
148 | \fn QCOORD &QPoint::rx()
|
---|
149 |
|
---|
150 | Returns a reference to the x coordinate of the point.
|
---|
151 |
|
---|
152 | Using a reference makes it possible to directly manipulate x.
|
---|
153 |
|
---|
154 | Example:
|
---|
155 | \code
|
---|
156 | QPoint p( 1, 2 );
|
---|
157 | p.rx()--; // p becomes (0, 2)
|
---|
158 | \endcode
|
---|
159 |
|
---|
160 | \sa ry()
|
---|
161 | */
|
---|
162 |
|
---|
163 | /*!
|
---|
164 | \fn QCOORD &QPoint::ry()
|
---|
165 |
|
---|
166 | Returns a reference to the y coordinate of the point.
|
---|
167 |
|
---|
168 | Using a reference makes it possible to directly manipulate y.
|
---|
169 |
|
---|
170 | Example:
|
---|
171 | \code
|
---|
172 | QPoint p( 1, 2 );
|
---|
173 | p.ry()++; // p becomes (1, 3)
|
---|
174 | \endcode
|
---|
175 |
|
---|
176 | \sa rx()
|
---|
177 | */
|
---|
178 |
|
---|
179 |
|
---|
180 | /*!
|
---|
181 | \fn QPoint &QPoint::operator+=( const QPoint &p )
|
---|
182 |
|
---|
183 | Adds point \a p to this point and returns a reference to this
|
---|
184 | point.
|
---|
185 |
|
---|
186 | Example:
|
---|
187 | \code
|
---|
188 | QPoint p( 3, 7 );
|
---|
189 | QPoint q( -1, 4 );
|
---|
190 | p += q; // p becomes (2,11)
|
---|
191 | \endcode
|
---|
192 | */
|
---|
193 |
|
---|
194 | /*!
|
---|
195 | \fn QPoint &QPoint::operator-=( const QPoint &p )
|
---|
196 |
|
---|
197 | Subtracts point \a p from this point and returns a reference to
|
---|
198 | this point.
|
---|
199 |
|
---|
200 | Example:
|
---|
201 | \code
|
---|
202 | QPoint p( 3, 7 );
|
---|
203 | QPoint q( -1, 4 );
|
---|
204 | p -= q; // p becomes (4,3)
|
---|
205 | \endcode
|
---|
206 | */
|
---|
207 |
|
---|
208 | /*!
|
---|
209 | \fn QPoint &QPoint::operator*=( int c )
|
---|
210 |
|
---|
211 | Multiplies this point's x and y by \a c, and returns a reference
|
---|
212 | to this point.
|
---|
213 |
|
---|
214 | Example:
|
---|
215 | \code
|
---|
216 | QPoint p( -1, 4 );
|
---|
217 | p *= 2; // p becomes (-2,8)
|
---|
218 | \endcode
|
---|
219 | */
|
---|
220 |
|
---|
221 | /*!
|
---|
222 | \overload QPoint &QPoint::operator*=( double c )
|
---|
223 |
|
---|
224 | Multiplies this point's x and y by \a c, and returns a reference
|
---|
225 | to this point.
|
---|
226 |
|
---|
227 | Example:
|
---|
228 | \code
|
---|
229 | QPoint p( -1, 4 );
|
---|
230 | p *= 2.5; // p becomes (-3,10)
|
---|
231 | \endcode
|
---|
232 |
|
---|
233 | Note that the result is truncated because points are held as
|
---|
234 | integers.
|
---|
235 | */
|
---|
236 |
|
---|
237 |
|
---|
238 | /*!
|
---|
239 | \fn bool operator==( const QPoint &p1, const QPoint &p2 )
|
---|
240 |
|
---|
241 | \relates QPoint
|
---|
242 |
|
---|
243 | Returns TRUE if \a p1 and \a p2 are equal; otherwise returns FALSE.
|
---|
244 | */
|
---|
245 |
|
---|
246 | /*!
|
---|
247 | \fn bool operator!=( const QPoint &p1, const QPoint &p2 )
|
---|
248 |
|
---|
249 | \relates QPoint
|
---|
250 |
|
---|
251 | Returns TRUE if \a p1 and \a p2 are not equal; otherwise returns FALSE.
|
---|
252 | */
|
---|
253 |
|
---|
254 | /*!
|
---|
255 | \fn const QPoint operator+( const QPoint &p1, const QPoint &p2 )
|
---|
256 |
|
---|
257 | \relates QPoint
|
---|
258 |
|
---|
259 | Returns the sum of \a p1 and \a p2; each component is added separately.
|
---|
260 | */
|
---|
261 |
|
---|
262 | /*!
|
---|
263 | \fn const QPoint operator-( const QPoint &p1, const QPoint &p2 )
|
---|
264 |
|
---|
265 | \relates QPoint
|
---|
266 |
|
---|
267 | Returns \a p2 subtracted from \a p1; each component is subtracted
|
---|
268 | separately.
|
---|
269 | */
|
---|
270 |
|
---|
271 | /*!
|
---|
272 | \fn const QPoint operator*( const QPoint &p, int c )
|
---|
273 |
|
---|
274 | \relates QPoint
|
---|
275 |
|
---|
276 | Returns the QPoint formed by multiplying both components of \a p
|
---|
277 | by \a c.
|
---|
278 | */
|
---|
279 |
|
---|
280 | /*!
|
---|
281 | \overload const QPoint operator*( int c, const QPoint &p )
|
---|
282 |
|
---|
283 | \relates QPoint
|
---|
284 |
|
---|
285 | Returns the QPoint formed by multiplying both components of \a p
|
---|
286 | by \a c.
|
---|
287 | */
|
---|
288 |
|
---|
289 | /*!
|
---|
290 | \overload const QPoint operator*( const QPoint &p, double c )
|
---|
291 |
|
---|
292 | \relates QPoint
|
---|
293 |
|
---|
294 | Returns the QPoint formed by multiplying both components of \a p
|
---|
295 | by \a c.
|
---|
296 |
|
---|
297 | Note that the result is truncated because points are held as
|
---|
298 | integers.
|
---|
299 | */
|
---|
300 |
|
---|
301 | /*!
|
---|
302 | \overload const QPoint operator*( double c, const QPoint &p )
|
---|
303 |
|
---|
304 | \relates QPoint
|
---|
305 |
|
---|
306 | Returns the QPoint formed by multiplying both components of \a p
|
---|
307 | by \a c.
|
---|
308 |
|
---|
309 | Note that the result is truncated because points are held as
|
---|
310 | integers.
|
---|
311 | */
|
---|
312 |
|
---|
313 | /*!
|
---|
314 | \overload const QPoint operator-( const QPoint &p )
|
---|
315 |
|
---|
316 | \relates QPoint
|
---|
317 |
|
---|
318 | Returns the QPoint formed by changing the sign of both components
|
---|
319 | of \a p, equivalent to \c{QPoint(0,0) - p}.
|
---|
320 | */
|
---|
321 |
|
---|
322 | /*!
|
---|
323 | \fn QPoint &QPoint::operator/=( int c )
|
---|
324 |
|
---|
325 | Divides both x and y by \a c, and returns a reference to this
|
---|
326 | point.
|
---|
327 |
|
---|
328 | Example:
|
---|
329 | \code
|
---|
330 | QPoint p( -2, 8 );
|
---|
331 | p /= 2; // p becomes (-1,4)
|
---|
332 | \endcode
|
---|
333 | */
|
---|
334 |
|
---|
335 | /*!
|
---|
336 | \overload QPoint &QPoint::operator/=( double c )
|
---|
337 |
|
---|
338 | Divides both x and y by \a c, and returns a reference to this
|
---|
339 | point.
|
---|
340 |
|
---|
341 | Example:
|
---|
342 | \code
|
---|
343 | QPoint p( -3, 10 );
|
---|
344 | p /= 2.5; // p becomes (-1,4)
|
---|
345 | \endcode
|
---|
346 |
|
---|
347 | Note that the result is truncated because points are held as
|
---|
348 | integers.
|
---|
349 | */
|
---|
350 |
|
---|
351 | /*!
|
---|
352 | \fn const QPoint operator/( const QPoint &p, int c )
|
---|
353 |
|
---|
354 | \relates QPoint
|
---|
355 |
|
---|
356 | Returns the QPoint formed by dividing both components of \a p by
|
---|
357 | \a c.
|
---|
358 | */
|
---|
359 |
|
---|
360 | /*!
|
---|
361 | \overload const QPoint operator/( const QPoint &p, double c )
|
---|
362 |
|
---|
363 | \relates QPoint
|
---|
364 |
|
---|
365 | Returns the QPoint formed by dividing both components of \a p
|
---|
366 | by \a c.
|
---|
367 |
|
---|
368 | Note that the result is truncated because points are held as
|
---|
369 | integers.
|
---|
370 | */
|
---|
371 |
|
---|
372 |
|
---|
373 | void QPoint::warningDivByZero()
|
---|
374 | {
|
---|
375 | #if defined(QT_CHECK_MATH)
|
---|
376 | qWarning( "QPoint: Division by zero error" );
|
---|
377 | #endif
|
---|
378 | }
|
---|
379 |
|
---|
380 |
|
---|
381 | /*****************************************************************************
|
---|
382 | QPoint stream functions
|
---|
383 | *****************************************************************************/
|
---|
384 | #ifndef QT_NO_DATASTREAM
|
---|
385 | /*!
|
---|
386 | \relates QPoint
|
---|
387 |
|
---|
388 | Writes point \a p to the stream \a s and returns a reference to
|
---|
389 | the stream.
|
---|
390 |
|
---|
391 | \sa \link datastreamformat.html Format of the QDataStream operators \endlink
|
---|
392 | */
|
---|
393 |
|
---|
394 | QDataStream &operator<<( QDataStream &s, const QPoint &p )
|
---|
395 | {
|
---|
396 | if ( s.version() == 1 )
|
---|
397 | s << (Q_INT16)p.x() << (Q_INT16)p.y();
|
---|
398 | else
|
---|
399 | s << (Q_INT32)p.x() << (Q_INT32)p.y();
|
---|
400 | return s;
|
---|
401 | }
|
---|
402 |
|
---|
403 | /*!
|
---|
404 | \relates QPoint
|
---|
405 |
|
---|
406 | Reads a QPoint from the stream \a s into point \a p and returns a
|
---|
407 | reference to the stream.
|
---|
408 |
|
---|
409 | \sa \link datastreamformat.html Format of the QDataStream operators \endlink
|
---|
410 | */
|
---|
411 |
|
---|
412 | QDataStream &operator>>( QDataStream &s, QPoint &p )
|
---|
413 | {
|
---|
414 | if ( s.version() == 1 ) {
|
---|
415 | Q_INT16 x, y;
|
---|
416 | s >> x; p.rx() = x;
|
---|
417 | s >> y; p.ry() = y;
|
---|
418 | }
|
---|
419 | else {
|
---|
420 | Q_INT32 x, y;
|
---|
421 | s >> x; p.rx() = x;
|
---|
422 | s >> y; p.ry() = y;
|
---|
423 | }
|
---|
424 | return s;
|
---|
425 | }
|
---|
426 | #endif // QT_NO_DATASTREAM
|
---|
427 | /*!
|
---|
428 | Returns the sum of the absolute values of x() and y(),
|
---|
429 | traditionally known as the "Manhattan length" of the vector from
|
---|
430 | the origin to the point. The tradition arises because such
|
---|
431 | distances apply to travelers who can only travel on a rectangular
|
---|
432 | grid, like the streets of Manhattan.
|
---|
433 |
|
---|
434 | This is a useful, and quick to calculate, approximation to the
|
---|
435 | true length: sqrt(pow(x(),2)+pow(y(),2)).
|
---|
436 | */
|
---|
437 | int QPoint::manhattanLength() const
|
---|
438 | {
|
---|
439 | return QABS(x())+QABS(y());
|
---|
440 | }
|
---|