# QPoint Class

The QPoint class defines a point in the plane using integer precision. More...

 Header: #include qmake: QT += core

Note: All functions in this class are reentrant.

## Public Functions

 QPoint() QPoint(int xpos, int ypos) bool isNull() const int manhattanLength() const int & rx() int & ry() void setX(int x) void setY(int y) CGPoint toCGPoint() const int x() const int y() const QPoint & operator*=(float factor) QPoint & operator*=(double factor) QPoint & operator*=(int factor) QPoint & operator+=(const QPoint &point) QPoint & operator-=(const QPoint &point) QPoint & operator/=(qreal divisor)

## Static Public Members

 int dotProduct(const QPoint &p1, const QPoint &p2)
 bool operator!=(const QPoint &p1, const QPoint &p2) const QPoint operator*(const QPoint &point, float factor) const QPoint operator*(const QPoint &point, double factor) const QPoint operator*(const QPoint &point, int factor) const QPoint operator*(float factor, const QPoint &point) const QPoint operator*(double factor, const QPoint &point) const QPoint operator*(int factor, const QPoint &point) const QPoint operator+(const QPoint &p1, const QPoint &p2) const QPoint operator+(const QPoint &point) const QPoint operator-(const QPoint &p1, const QPoint &p2) const QPoint operator-(const QPoint &point) const QPoint operator/(const QPoint &point, qreal divisor) QDataStream & operator<<(QDataStream &stream, const QPoint &point) bool operator==(const QPoint &p1, const QPoint &p2) QDataStream & operator>>(QDataStream &stream, QPoint &point)

## Detailed Description

The QPoint class defines a point in the plane using integer precision.

A point is specified by a x coordinate and an y coordinate which can be accessed using the x() and y() functions. The isNull() function returns `true` if both x and y are set to 0. The coordinates can be set (or altered) using the setX() and setY() functions, or alternatively the rx() and ry() functions which return references to the coordinates (allowing direct manipulation).

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

```QPoint p;

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

A QPoint object can also be used as a vector: Addition and subtraction are defined as for vectors (each component is added separately). A QPoint object can also be divided or multiplied by an `int` or a `qreal`.

In addition, the QPoint class provides the manhattanLength() function which gives an inexpensive approximation of the length of the QPoint object interpreted as a vector. Finally, QPoint objects can be streamed as well as compared.

## Member Function Documentation

### QPoint::QPoint()

Constructs a null point, i.e. with coordinates (0, 0)

### QPoint::QPoint(intxpos, intypos)

Constructs a point with the given coordinates (xpos, ypos).

### `[static] `int QPoint::dotProduct(const QPoint &p1, const QPoint &p2)

```QPoint p( 3, 7);
QPoint q(-1, 4);
int lengthSquared = QPoint::dotProduct(p, q);   // lengthSquared becomes 25```

Returns the dot product of p1 and p2.

This function was introduced in Qt 5.1.

### bool QPoint::isNull() const

Returns `true` if both the x and y coordinates are set to 0, otherwise returns `false`.

### 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. For example:

```QPoint oldPosition;

MyWidget::mouseMoveEvent(QMouseEvent *event)
{
QPoint point = event->pos() - oldPosition;
if (point.manhattanLength() > 3)
// the mouse has moved more than 3 pixels since the oldPosition
}```

This is a useful, and quick to calculate, approximation to the true length:

`double trueLength = std::sqrt(std::pow(x(), 2) + std::pow(y(), 2));`

The tradition of "Manhattan length" arises because such distances apply to travelers who can only travel on a rectangular grid, like the streets of Manhattan.

### int &QPoint::rx()

Returns a reference to the x coordinate of this point.

Using a reference makes it possible to directly manipulate x. For example:

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

### int &QPoint::ry()

Returns a reference to the y coordinate of this point.

Using a reference makes it possible to directly manipulate y. For example:

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

### void QPoint::setX(intx)

Sets the x coordinate of this point to the given x coordinate.

### void QPoint::setY(inty)

Sets the y coordinate of this point to the given y coordinate.

### CGPoint QPoint::toCGPoint() const

Creates a CGPoint from a QPoint.

This function was introduced in Qt 5.8.

### int QPoint::x() const

Returns the x coordinate of this point.

### int QPoint::y() const

Returns the y coordinate of this point.

### QPoint &QPoint::operator*=(floatfactor)

Multiplies this point's coordinates by the given factor, and returns a reference to this point.

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF for floating point accuracy.

### QPoint &QPoint::operator*=(doublefactor)

Multiplies this point's coordinates by the given factor, and returns a reference to this point. For example:

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

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF for floating point accuracy.

### QPoint &QPoint::operator*=(intfactor)

Multiplies this point's coordinates by the given factor, and returns a reference to this point.

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

Adds the given point to this point and returns a reference to this point. For example:

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

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

Subtracts the given point from this point and returns a reference to this point. For example:

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

### QPoint &QPoint::operator/=(qrealdivisor)

Divides both x and y by the given divisor, and returns a reference to this point. For example:

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

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF for floating point accuracy.

## Related Non-Members

### booloperator!=(const QPoint &p1, const QPoint &p2)

Returns `true` if p1 and p2 are not equal; otherwise returns `false`.

### const QPointoperator*(const QPoint &point, floatfactor)

Returns a copy of the given point multiplied by the given factor.

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF for floating point accuracy.

### const QPointoperator*(const QPoint &point, doublefactor)

Returns a copy of the given point multiplied by the given factor.

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF for floating point accuracy.

### const QPointoperator*(const QPoint &point, intfactor)

Returns a copy of the given point multiplied by the given factor.

### const QPointoperator*(floatfactor, const QPoint &point)

Returns a copy of the given point multiplied by the given factor.

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF for floating point accuracy.

### const QPointoperator*(doublefactor, const QPoint &point)

Returns a copy of the given point multiplied by the given factor.

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF for floating point accuracy.

### const QPointoperator*(intfactor, const QPoint &point)

Returns a copy of the given point multiplied by the given factor.

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

Returns a QPoint object that is the sum of the given points, p1 and p2; each component is added separately.

### const QPointoperator+(const QPoint &point)

Returns point unmodified.

This function was introduced in Qt 5.0.

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

Returns a QPoint object that is formed by subtracting p2 from p1; each component is subtracted separately.

### const QPointoperator-(const QPoint &point)

Returns a QPoint object that is formed by changing the sign of both components of the given point.

Equivalent to `QPoint(0,0) - point`.

### const QPointoperator/(const QPoint &point, qrealdivisor)

Returns the QPoint formed by dividing both components of the given point by the given divisor.

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF for floating point accuracy.

### QDataStream &operator<<(QDataStream &stream, const QPoint &point)

Writes the given point to the given stream and returns a reference to the stream.

Returns `true` if p1 and p2 are equal; otherwise returns false.
{"ft_lang_label":"__label__en","ft_lang_prob":0.73976916,"math_prob":0.9964651,"size":9865,"snap":"2019-26-2019-30","text_gpt3_token_len":2308,"char_repetition_ratio":0.23902242,"word_repetition_ratio":0.3437077,"special_character_ratio":0.23882413,"punctuation_ratio":0.14770241,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99839437,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-07-17T07:41:49Z\",\"WARC-Record-ID\":\"<urn:uuid:0ab7d826-b453-432a-843a-3b4f218bfcfd>\",\"Content-Length\":\"50514\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4f52fbd9-97bd-4c91-88df-2863a2191baa>\",\"WARC-Concurrent-To\":\"<urn:uuid:29fedf2d-0f5b-469b-8bc9-7aac33ae136d>\",\"WARC-IP-Address\":\"18.203.89.116\",\"WARC-Target-URI\":\"https://doc-snapshots.qt.io/qt5-5.10/qpoint.html\",\"WARC-Payload-Digest\":\"sha1:4VH73ZUC4R5IQHKMBTSC4HQ2RODEJHEL\",\"WARC-Block-Digest\":\"sha1:7KQIJAPWKP7ZVFLTSIDM4HY3NB3FK2VV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-30/CC-MAIN-2019-30_segments_1563195525094.53_warc_CC-MAIN-20190717061451-20190717083451-00288.warc.gz\"}"}