Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

TQPoint Class Reference

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

#include <ntqpoint.h>

List of all member functions.

Public Members

Related Functions


Detailed Description

The TQPoint class defines a point in the plane.

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

The coordinate type is TQCOORD (a 32-bit integer). The minimum value of TQCOORD is TQCOORD_MIN (-2147483648) and the maximum value is TQCOORD_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 += TQPoint( 1, 0 );
        p.rx()++;
    

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

Example:

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

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

See also TQPointArray, TQSize, TQRect, Graphics Classes, and Image Processing Classes.


Member Function Documentation

TQPoint::TQPoint ()

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

TQPoint::TQPoint ( int xpos, int ypos )

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

bool TQPoint::isNull () const

Returns TRUE if both the x value and the y value are 0; otherwise returns FALSE.

int TQPoint::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 because such distances apply to travelers who can only travel on a rectangular grid, like the streets of Manhattan.

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

TQPoint & TQPoint::operator*= ( int c )

Multiplies this point's x and y by c, and returns a reference to this point.

Example:

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

TQPoint & TQPoint::operator*= ( double c )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Multiplies this point's x and y by c, and returns a reference to this point.

Example:

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

Note that the result is truncated because points are held as integers.

TQPoint & TQPoint::operator+= ( const TQPoint & p )

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

Example:

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

TQPoint & TQPoint::operator-= ( const TQPoint & p )

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

Example:

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

TQPoint & TQPoint::operator/= ( int c )

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

Example:

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

TQPoint & TQPoint::operator/= ( double c )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

Example:

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

Note that the result is truncated because points are held as integers.

TQCOORD & TQPoint::rx ()

Returns a reference to the x coordinate of the point.

Using a reference makes it possible to directly manipulate x.

Example:

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

See also ry().

TQCOORD & TQPoint::ry ()

Returns a reference to the y coordinate of the point.

Using a reference makes it possible to directly manipulate y.

Example:

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

See also rx().

void TQPoint::setX ( int x )

Sets the x coordinate of the point to x.

See also x() and setY().

Example: t14/cannon.cpp.

void TQPoint::setY ( int y )

Sets the y coordinate of the point to y.

See also y() and setX().

Example: t14/cannon.cpp.

int TQPoint::x () const

Returns the x coordinate of the point.

See also setX() and y().

Examples: canvas/canvas.cpp, chart/canvasview.cpp, dirview/dirview.cpp, fileiconview/qfileiconview.cpp, helpsystem/tooltip.cpp, life/life.cpp, and t14/cannon.cpp.

int TQPoint::y () const

Returns the y coordinate of the point.

See also setY() and x().

Examples: canvas/canvas.cpp, chart/canvasview.cpp, fileiconview/qfileiconview.cpp, helpsystem/tooltip.cpp, life/life.cpp, t14/cannon.cpp, and themes/wood.cpp.


Related Functions

bool operator!= ( const TQPoint & p1, const TQPoint & p2 )

Returns TRUE if p1 and p2 are not equal; otherwise returns FALSE.

const TQPoint operator* ( const TQPoint & p, int c )

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

const TQPoint operator* ( int c, const TQPoint & p )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

const TQPoint operator* ( const TQPoint & p, double c )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

Note that the result is truncated because points are held as integers.

const TQPoint operator* ( double c, const TQPoint & p )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

Note that the result is truncated because points are held as integers.

const TQPoint operator+ ( const TQPoint & p1, const TQPoint & p2 )

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

const TQPoint operator- ( const TQPoint & p1, const TQPoint & p2 )

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

const TQPoint operator- ( const TQPoint & p )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

const TQPoint operator/ ( const TQPoint & p, int c )

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

const TQPoint operator/ ( const TQPoint & p, double c )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

Note that the result is truncated because points are held as integers.

TQDataStream & operator<< ( TQDataStream & s, const TQPoint & p )

Writes point p to the stream s and returns a reference to the stream.

See also Format of the TQDataStream operators.

bool operator== ( const TQPoint & p1, const TQPoint & p2 )

Returns TRUE if p1 and p2 are equal; otherwise returns FALSE.

TQDataStream & operator>> ( TQDataStream & s, TQPoint & p )

Reads a TQPoint from the stream s into point p and returns a reference to the stream.

See also Format of the TQDataStream operators.


This file is part of the TQt toolkit. Copyright © 1995-2007 Trolltech. All Rights Reserved.


Copyright © 2007 TrolltechTrademarks
TQt 3.3.8