src/corelib/tools/qpoint.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtCore module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qpoint.h"
       
    43 #include "qdatastream.h"
       
    44 #include "qdebug.h"
       
    45 
       
    46 QT_BEGIN_NAMESPACE
       
    47 
       
    48 /*!
       
    49     \class QPoint
       
    50     \ingroup painting
       
    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 
       
   312 QDataStream &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 
       
   331 QDataStream &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 */
       
   363 int QPoint::manhattanLength() const
       
   364 {
       
   365     return qAbs(x())+qAbs(y());
       
   366 }
       
   367 
       
   368 #ifndef QT_NO_DEBUG_STREAM
       
   369 QDebug operator<<(QDebug dbg, const QPoint &p) {
       
   370     dbg.nospace() << "QPoint(" << p.x() << ',' << p.y() << ')';
       
   371     return dbg.space();
       
   372 }
       
   373 
       
   374 QDebug 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 painting
       
   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 /*!
       
   447     \since 4.6
       
   448 
       
   449     Returns the sum of the absolute values of x() and y(),
       
   450     traditionally known as the "Manhattan length" of the vector from
       
   451     the origin to the point.
       
   452 
       
   453     \sa QPoint::manhattanLength()
       
   454 */
       
   455 qreal QPointF::manhattanLength() const
       
   456 {
       
   457     return qAbs(x())+qAbs(y());
       
   458 }
       
   459 
       
   460 /*!
       
   461     \fn qreal QPointF::x() const
       
   462 
       
   463     Returns the x-coordinate of this point.
       
   464 
       
   465     \sa setX(), rx()
       
   466 */
       
   467 
       
   468 /*!
       
   469     \fn qreal QPointF::y() const
       
   470 
       
   471     Returns the y-coordinate of this point.
       
   472 
       
   473     \sa setY(), ry()
       
   474 */
       
   475 
       
   476 /*!
       
   477     \fn void QPointF::setX(qreal x)
       
   478 
       
   479     Sets the x coordinate of this point to the given \a x coordinate.
       
   480 
       
   481     \sa x() setY()
       
   482 */
       
   483 
       
   484 /*!
       
   485     \fn void QPointF::setY(qreal y)
       
   486 
       
   487     Sets the y coordinate of this point to the given \a y coordinate.
       
   488 
       
   489     \sa  y(), setX()
       
   490 */
       
   491 
       
   492 /*!
       
   493     \fn qreal& QPointF::rx()
       
   494 
       
   495     Returns a reference to the x coordinate of this point.
       
   496 
       
   497     Using a reference makes it possible to directly manipulate x. For example:
       
   498 
       
   499     \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 10
       
   500 
       
   501     \sa x(), setX()
       
   502 */
       
   503 
       
   504 /*!
       
   505     \fn qreal& QPointF::ry()
       
   506 
       
   507     Returns a reference to the y coordinate of this point.
       
   508 
       
   509     Using a reference makes it possible to directly manipulate y. For example:
       
   510 
       
   511     \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 11
       
   512 
       
   513     \sa y() setY()
       
   514 */
       
   515 
       
   516 /*!
       
   517     \fn QPointF& QPointF::operator+=(const QPointF &point)
       
   518 
       
   519     Adds the given \a point to this point and returns a reference to
       
   520     this point. For example:
       
   521 
       
   522     \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 12
       
   523 
       
   524     \sa operator-=()
       
   525 */
       
   526 
       
   527 /*!
       
   528     \fn QPointF& QPointF::operator-=(const QPointF &point)
       
   529 
       
   530     Subtracts the given \a point from this point and returns a reference
       
   531     to this point. For example:
       
   532 
       
   533     \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 13
       
   534 
       
   535     \sa operator+=()
       
   536 */
       
   537 
       
   538 /*!
       
   539     \fn QPointF& QPointF::operator*=(qreal factor)
       
   540 
       
   541     Multiplies this point's coordinates by the given \a factor, and
       
   542     returns a reference to this point. For example:
       
   543 
       
   544     \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 14
       
   545 
       
   546     \sa operator/=()
       
   547 */
       
   548 
       
   549 /*!
       
   550     \fn QPointF& QPointF::operator/=(qreal divisor)
       
   551 
       
   552     Divides both x and y by the given \a divisor, and returns a reference
       
   553     to this point. For example:
       
   554 
       
   555     \snippet doc/src/snippets/code/src_corelib_tools_qpoint.cpp 15
       
   556 
       
   557     \sa operator*=()
       
   558 */
       
   559 
       
   560 /*!
       
   561     \fn const QPointF operator+(const QPointF &p1, const QPointF &p2)
       
   562     \relates QPointF
       
   563 
       
   564     Returns a QPointF object that is the sum of the given points, \a p1
       
   565     and \a p2; each component is added separately.
       
   566 
       
   567     \sa QPointF::operator+=()
       
   568 */
       
   569 
       
   570 /*!
       
   571     \fn const QPointF operator-(const QPointF &p1, const QPointF &p2)
       
   572     \relates QPointF
       
   573 
       
   574     Returns a QPointF object that is formed by subtracting \a p2 from \a p1;
       
   575     each component is subtracted separately.
       
   576 
       
   577     \sa QPointF::operator-=()
       
   578 */
       
   579 
       
   580 /*!
       
   581     \fn const QPointF operator*(const QPointF &point, qreal factor)
       
   582     \relates QPointF
       
   583 
       
   584     Returns a copy of the given \a point,  multiplied by the given \a factor.
       
   585 
       
   586     \sa QPointF::operator*=()
       
   587 */
       
   588 
       
   589 /*!
       
   590     \fn const QPointF operator*(qreal factor, const QPointF &point)
       
   591     \relates QPointF
       
   592 
       
   593     \overload
       
   594 
       
   595     Returns a copy of the given \a point, multiplied by the given \a factor.
       
   596 */
       
   597 
       
   598 /*!
       
   599     \fn const QPointF operator-(const QPointF &point)
       
   600     \relates QPointF
       
   601     \overload
       
   602 
       
   603     Returns a QPointF object that is formed by changing the sign of
       
   604     both components of the given \a point.
       
   605 
       
   606     Equivalent to \c {QPointF(0,0) - point}.
       
   607 */
       
   608 
       
   609 /*!
       
   610     \fn const QPointF operator/(const QPointF &point, qreal divisor)
       
   611     \relates QPointF
       
   612 
       
   613     Returns the QPointF object formed by dividing both components of
       
   614     the given \a point by the given \a divisor.
       
   615 
       
   616     \sa QPointF::operator/=()
       
   617 */
       
   618 
       
   619 /*!
       
   620     \fn QPoint QPointF::toPoint() const
       
   621 
       
   622     Rounds the coordinates of this point to the nearest integer, and
       
   623     returns a QPoint object with the rounded coordinates.
       
   624 
       
   625     \sa QPointF()
       
   626 */
       
   627 
       
   628 /*!
       
   629     \fn bool operator==(const QPointF &p1, const QPointF &p2)
       
   630     \relates QPointF
       
   631 
       
   632     Returns true if \a p1 is equal to \a p2; otherwise returns false.
       
   633 */
       
   634 
       
   635 /*!
       
   636     \fn bool operator!=(const QPointF &p1, const QPointF &p2);
       
   637     \relates QPointF
       
   638 
       
   639     Returns true if \a p1 is not equal to \a p2; otherwise returns false.
       
   640 */
       
   641 
       
   642 #ifndef QT_NO_DATASTREAM
       
   643 /*!
       
   644     \fn QDataStream &operator<<(QDataStream &stream, const QPointF &point)
       
   645     \relates QPointF
       
   646 
       
   647     Writes the given \a point to the given \a stream and returns a
       
   648     reference to the stream.
       
   649 
       
   650     \sa {Format of the QDataStream Operators}
       
   651 */
       
   652 
       
   653 QDataStream &operator<<(QDataStream &s, const QPointF &p)
       
   654 {
       
   655     s << double(p.x()) << double(p.y());
       
   656     return s;
       
   657 }
       
   658 
       
   659 /*!
       
   660     \fn QDataStream &operator>>(QDataStream &stream, QPointF &point)
       
   661     \relates QPointF
       
   662 
       
   663     Reads a point from the given \a stream into the given \a point
       
   664     and returns a reference to the stream.
       
   665 
       
   666     \sa {Format of the QDataStream Operators}
       
   667 */
       
   668 
       
   669 QDataStream &operator>>(QDataStream &s, QPointF &p)
       
   670 {
       
   671     double x, y;
       
   672     s >> x;
       
   673     s >> y;
       
   674     p.setX(qreal(x));
       
   675     p.setY(qreal(y));
       
   676     return s;
       
   677 }
       
   678 #endif // QT_NO_DATASTREAM
       
   679 
       
   680 QT_END_NAMESPACE