Qt Toolkit - QPoint Class

| Classes - Annotated - Tree - Functions - Home - Structure

# QPoint Class Reference

The QPoint class defines a point in the plane. More...

`#include <qpoint.h>`

List of all member functions.

## Public Members

- QPoint ()
- QPoint ( int xpos, int ypos )
- bool isNull () const
- int x () const
- int y () const
- void setX ( int x )
- void setY ( int y )
- int manhattanLength () const
- QCOORD& rx ()
- QCOORD& ry ()
- QPoint& operator+= ( const QPoint & p )
- QPoint& operator-= ( const QPoint & p )
- QPoint& operator*= ( int c )
- QPoint& operator*= ( double c )
- QPoint& operator/= ( int c )
- QPoint& operator/= ( double c )

## Related Functions

(Note that these are not member functions.)

- QPoint operator+ (const QPoint & p1, const QPoint & p2)
- QPoint operator* (double c, const QPoint & p)
- QPoint operator- (const QPoint & p)
- QDataStream & operator<< (QDataStream & s, const QPoint & p)
- QPoint operator/ (const QPoint & p, int c)
- QPoint operator* (const QPoint & p, double c)
- QPoint operator* (int c, const QPoint & p)
- QDataStream & operator>> (QDataStream & s, QPoint & p)
- bool operator!= (const QPoint & p1, const QPoint & p2)
- bool operator== (const QPoint & p1, const QPoint & p2)
- QPoint operator* (const QPoint & p, int c)
- QPoint operator- (const QPoint & p1, const QPoint & p2)
- QPoint operator/ (const QPoint & p, double c)

## Detailed Description

The QPoint class defines a point in the plane.

A point is specified by an x coordinate and a y coordinate.

The coordinate type is `QCOORD` (a 32-bit integer). The minimum value of `QCOORD` is `QCOORD_MIN` (-2147483648) and the maximum value is `QCOORD_MAX` (2147483647).

The coordinates are accessed by the functions x() and y(), they can be set by setX() and setY(), or by the reference functions rx() and ry().

Given a point p, the following statements are all equivalent:

         p.setX( p.x() + 1 );
         p += QPoint( 1, 0 );
         p.rx()++;

A QPoint can also be used as a vector. Addition and subtraction of QPoint are defined as for vectors (each component is added separately). You can divide or multiply a QPoint by an `int` or a `double.` The function manhattanLength() gives an inexpensive approximation to the length of the QPoint interpreted as a vector.

Example:

         //QPoint oldPos is defined somewhere else
         MyWidget::mouseMoveEvent( QMouseEvent *e )
         {
            QPoint vector = e->pos() - oldPos;
            if ( vector.manhattanLength() > 3 )
               ... //mouse has moved more than 3 pixels since oldPos
         }

QPoints can be compared for equality or inequality, and they can be written to and read from a QStream.

See also QSize and QRect.

Examples: drawlines/connect.cpp xform/xform.cpp qmag/qmag.cpp forever/forever.cpp

## Member Function Documentation

### QPoint::QPoint ()

Constructs a point with coordinates (0,0) (isNull() returns TRUE).

### QPoint::QPoint ( int xpos, int ypos )

Constructs a point with the x value xpos and y value ypos.

### bool QPoint::isNull () const

Returns TRUE if both the x value and the y value are 0.

### int QPoint::manhattanLength () const

Returns the sum of the absolute values of x() and y(), traditionally known as the "Manhattan length" of the vector from the origin to the point. The tradition arises since such distances apply to travelers who can only travel on a rectangular grid, like the streets of Manhattan.

This is a useful approximation to the true length, sqrt(pow(x(),2)+pow(y(),2)).

### QPoint & QPoint::operator*= ( double c )

Multiplies both x and y with c, and return a reference to this point.

Example:

        QPoint p( -1, 4 );
        p *= 2.5;                   // p becomes (-3,10)

Note that the result is truncated.

### QPoint & QPoint::operator*= ( int c )

Multiplies both x and y with c, and return a reference to this point.

Example:

        QPoint p( -1, 4 );
        p *= 2;                     // p becomes (-2,8)

### QPoint & QPoint::operator+= ( const QPoint & p )

Adds p to the point and returns a reference to this point.

Example:

        QPoint p(  3, 7 );
        QPoint q( -1, 4 );
        p += q;                     // p becomes (2,11)

### QPoint & QPoint::operator-= ( const QPoint & p )

Subtracts p from the point and returns a reference to this point.

Example:

        QPoint p(  3, 7 );
        QPoint q( -1, 4 );
        p -= q;                     // p becomes (4,3)

### QPoint & QPoint::operator/= ( double c )

Divides both x and y by c, and return a reference to this point.

Example:

        QPoint p( -3, 10 );
        p /= 2.5;                   // p becomes (-1,4)

Note that the result is truncated.

### QPoint & QPoint::operator/= ( int c )

Divides both x and y by c, and return a reference to this point.

Example:

        QPoint p( -2, 8 );
        p /= 2;                     // p becomes (-1,4)

### QCOORD & QPoint::rx ()

Returns a reference to the x coordinate of the point.

Using a reference makes it possible to directly manipulate x.

Example:

        QPoint p( 1, 2 );
        p.rx()--;                   // p becomes (0,2)

See also ry().

### QCOORD & QPoint::ry ()

Returns a reference to the y coordinate of the point.

Using a reference makes it possible to directly manipulate y.

Example:

        QPoint p( 1, 2 );
        p.ry()++;                   // p becomes (1,3)

See also rx().

### void QPoint::setX ( int x )

Sets the x coordinate of the point to x.

See also x() and setY().

### void QPoint::setY ( int y )

Sets the y coordinate of the point to y.

See also y() and setX().

### int QPoint::x () const

Returns the x coordinate of the point.

See also setX() and y().

### int QPoint::y () const

Returns the y coordinate of the point.

See also setY() and x().

## Related Functions

### QPoint operator+ (const QPoint & p1, const QPoint & p2)

Returns the sum of p1 and p2; each component is added separately.

### QPoint operator* (double c, const QPoint & p)

Returns the QPoint formed by multiplying both components of p by c.

Note that the result is truncated.

### QPoint operator- (const QPoint & p)

Returns the QPoint formed by changing the sign of both components of p, equivalent to `QPoint(0,0) - p`

### QDataStream & operator<< (QDataStream & s, const QPoint & p)

Writes a QPoint to the stream and returns a reference to the stream.

See also Format of the QDataStream operators

### QPoint operator/ (const QPoint & p, int c)

Returns the QPoint formed by dividing both components of p by c.

### QPoint operator* (const QPoint & p, double c)

Returns the QPoint formed by multiplying both components of p by c.

Note that the result is truncated.

### QPoint operator* (int c, const QPoint & p)

Returns the QPoint formed by multiplying both components of p by c.

### QDataStream & operator>> (QDataStream & s, QPoint & p)

Reads a QPoint from the stream and returns a reference to the stream.

See also Format of the QDataStream operators

### bool operator!= (const QPoint & p1, const QPoint & p2)

Returns TRUE if p1 and p2 are different, or FALSE if they are equal.

### bool operator== (const QPoint & p1, const QPoint & p2)

Returns TRUE if p1 and p2 are equal, or FALSE if they are different.

### QPoint operator* (const QPoint & p, int c)

Returns the QPoint formed by multiplying both components of p by c.

### QPoint operator- (const QPoint & p1, const QPoint & p2)

Returns p2 subtracted from p1; each component is subtracted separately.

### QPoint operator/ (const QPoint & p, double c)

Returns the QPoint formed by dividing both components of p by c.

Note that the result is truncated.

Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.