Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QVector2D Class Reference

The QVector2D class represents a vector or vertex in 2D space. More...

#include <qvectornd.h>

+ Collaboration diagram for QVector2D:

Public Member Functions

constexpr QVector2D () noexcept
 Constructs a null vector, i.e.
 
 QVector2D (Qt::Initialization) noexcept
 
constexpr QVector2D (float xpos, float ypos) noexcept
 Constructs a vector with coordinates (xpos, ypos).
 
constexpr QVector2D (QPoint point) noexcept
 Constructs a vector with x and y coordinates from a 2D point.
 
constexpr QVector2D (QPointF point) noexcept
 Constructs a vector with x and y coordinates from a 2D point.
 
constexpr QVector2D (QVector3D vector) noexcept
 Constructs a vector with x and y coordinates from a 3D vector.
 
constexpr QVector2D (QVector4D vector) noexcept
 Constructs a vector with x and y coordinates from a 3D vector.
 
constexpr bool isNull () const noexcept
 Returns true if the x and y coordinates are set to 0.0, otherwise returns false.
 
constexpr float x () const noexcept
 Returns the x coordinate of this point.
 
constexpr float y () const noexcept
 Returns the y coordinate of this point.
 
constexpr void setX (float x) noexcept
 Sets the x coordinate of this point to the given finite x coordinate.
 
constexpr void setY (float y) noexcept
 Sets the y coordinate of this point to the given finite y coordinate.
 
constexpr float & operator[] (int i)
 
constexpr float operator[] (int i) const
 
float length () const noexcept
 Returns the length of the vector from the origin.
 
constexpr float lengthSquared () const noexcept
 Returns the squared length of the vector from the origin.
 
QVector2D normalized () const noexcept
 Returns the normalized unit vector form of this vector.
 
void normalize () noexcept
 Normalizes the current vector in place.
 
float distanceToPoint (QVector2D point) const noexcept
 
float distanceToLine (QVector2D point, QVector2D direction) const noexcept
 
constexpr QVector2Doperator+= (QVector2D vector) noexcept
 Adds the given vector to this vector and returns a reference to this vector.
 
constexpr QVector2Doperator-= (QVector2D vector) noexcept
 Subtracts the given vector from this vector and returns a reference to this vector.
 
constexpr QVector2Doperator*= (float factor) noexcept
 Multiplies this vector's coordinates by the given finite factor and returns a reference to this vector.
 
constexpr QVector2Doperator*= (QVector2D vector) noexcept
 Multiplies each component of this vector by the corresponding component of vector and returns a reference to this vector.
 
constexpr QVector2Doperator/= (float divisor)
 Divides this vector's coordinates by the given divisor and returns a reference to this vector.
 
constexpr QVector2Doperator/= (QVector2D vector)
 
constexpr QVector3D toVector3D () const noexcept
 Returns the 3D form of this 2D vector, with the z coordinate set to zero.
 
constexpr QVector4D toVector4D () const noexcept
 Returns the 4D form of this 2D vector, with the z and w coordinates set to zero.
 
constexpr QPoint toPoint () const noexcept
 Returns the QPoint form of this 2D vector.
 
constexpr QPointF toPointF () const noexcept
 Returns the QPointF form of this 2D vector.
 
Q_GUI_EXPORT operator QVariant () const
 Returns the 2D vector as a QVariant.
 

Static Public Member Functions

static constexpr float dotProduct (QVector2D v1, QVector2D v2) noexcept
 Returns the dot product of v1 and v2.
 

Friends

class QVector3D
 
class QVector4D
 
QT_WARNING_PUSH QT_WARNING_DISABLE_FLOAT_COMPARE constexpr friend bool operator== (QVector2D v1, QVector2D v2) noexcept
 Returns true if v1 is equal to v2; otherwise returns false.
 
constexpr friend bool operator!= (QVector2D v1, QVector2D v2) noexcept
 Returns true if v1 is not equal to v2; otherwise returns false.
 
QT_WARNING_POP constexpr friend QVector2D operator+ (QVector2D v1, QVector2D v2) noexcept
 //! friend
 
constexpr friend QVector2D operator- (QVector2D v1, QVector2D v2) noexcept
 //! friend
 
constexpr friend QVector2D operator* (float factor, QVector2D vector) noexcept
 //! friend
 
constexpr friend QVector2D operator* (QVector2D vector, float factor) noexcept
 //! friend
 
constexpr friend QVector2D operator* (QVector2D v1, QVector2D v2) noexcept
 //! friend
 
constexpr friend QVector2D operator- (QVector2D vector) noexcept
 //! friendThis is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a QVector2D object that is formed by changing the sign of each component of the given vector.
 
constexpr friend QVector2D operator/ (QVector2D vector, float divisor)
 //! friend
 
constexpr friend QVector2D operator/ (QVector2D vector, QVector2D divisor)
 //! friend
 
Q_GUI_EXPORT bool qFuzzyCompare (QVector2D v1, QVector2D v2) noexcept
 //! friend
 
template<std::size_t I, typename V , std::enable_if_t<(I< 2), bool > = true, std::enable_if_t< std::is_same_v< q20::remove_cvref_t< V >, QVector2D >, bool > = true>
constexpr decltype(auto) get (V &&vec) noexcept
 

Related Symbols

(Note that these are not member symbols.)

QDataStreamoperator<< (QDataStream &stream, QVector2D vector)
 Writes the given vector to the given stream and returns a reference to the stream.
 
QDataStreamoperator>> (QDataStream &stream, QVector2D &vector)
 Reads a 2D vector from the given stream into the given vector and returns a reference to the stream.
 

Detailed Description

The QVector2D class represents a vector or vertex in 2D space.

Since
4.6

\inmodule QtGui

Vectors are one of the main building blocks of 2D representation and drawing. They consist of two finite floating-point coordinates, traditionally called x and y.

The QVector2D class can also be used to represent vertices in 2D space. We therefore do not need to provide a separate vertex class.

See also
QVector3D, QVector4D, QQuaternion

Definition at line 30 of file qvectornd.h.

Constructor & Destructor Documentation

◆ QVector2D() [1/7]

constexpr QVector2D::QVector2D ( )
inlineconstexprnoexcept

Constructs a null vector, i.e.

with coordinates (0, 0).

Definition at line 481 of file qvectornd.h.

Referenced by normalized().

+ Here is the caller graph for this function:

◆ QVector2D() [2/7]

QVector2D::QVector2D ( Qt::Initialization )
inlineexplicitnoexcept
Since
5.5

Constructs a vector without initializing the contents.

Definition at line 34 of file qvectornd.h.

◆ QVector2D() [3/7]

constexpr QVector2D::QVector2D ( float xpos,
float ypos )
inlineconstexprnoexcept

Constructs a vector with coordinates (xpos, ypos).

Both coordinates must be finite.

Definition at line 483 of file qvectornd.h.

◆ QVector2D() [4/7]

constexpr QVector2D::QVector2D ( QPoint point)
inlineexplicitconstexprnoexcept

Constructs a vector with x and y coordinates from a 2D point.

Definition at line 485 of file qvectornd.h.

◆ QVector2D() [5/7]

constexpr QVector2D::QVector2D ( QPointF point)
inlineexplicitconstexprnoexcept

Constructs a vector with x and y coordinates from a 2D point.

Definition at line 487 of file qvectornd.h.

◆ QVector2D() [6/7]

constexpr QVector2D::QVector2D ( QVector3D vector)
inlineexplicitconstexprnoexcept

Constructs a vector with x and y coordinates from a 3D vector.

The z coordinate of vector is dropped.

See also
toVector3D()

Definition at line 490 of file qvectornd.h.

References vector.

◆ QVector2D() [7/7]

constexpr QVector2D::QVector2D ( QVector4D vector)
inlineexplicitconstexprnoexcept

Constructs a vector with x and y coordinates from a 3D vector.

The z and w coordinates of vector are dropped.

See also
toVector4D()

Definition at line 493 of file qvectornd.h.

References vector.

Member Function Documentation

◆ distanceToLine()

float QVector2D::distanceToLine ( QVector2D point,
QVector2D direction ) const
inlinenoexcept
Since
5.1

Returns the distance that this vertex is from a line defined by point and the unit vector direction.

If direction is a null vector, then it does not define a line. In that case, the distance from point to this vertex is returned.

See also
distanceToPoint()

Definition at line 551 of file qvectornd.h.

References direction, dotProduct, and length().

+ Here is the call graph for this function:

◆ distanceToPoint()

float QVector2D::distanceToPoint ( QVector2D point) const
inlinenoexcept
Since
5.1

Returns the distance from this vertex to a point defined by the vertex point.

See also
distanceToLine()

Definition at line 546 of file qvectornd.h.

◆ dotProduct()

constexpr float QVector2D::dotProduct ( QVector2D v1,
QVector2D v2 )
inlinestaticconstexprnoexcept

Returns the dot product of v1 and v2.

Definition at line 604 of file qvectornd.h.

Referenced by QQuadPath::closestPointOnLine(), QQuickVector2DValueType::dotProduct(), QQuadPath::isPointNearLine(), and QSGCurveProcessor::processFill().

+ Here is the caller graph for this function:

◆ isNull()

constexpr bool QVector2D::isNull ( ) const
inlineconstexprnoexcept

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

Definition at line 496 of file qvectornd.h.

References qIsNull().

+ Here is the call graph for this function:

◆ length()

float QVector2D::length ( ) const
inlinenoexcept

Returns the length of the vector from the origin.

See also
lengthSquared(), normalized()

Definition at line 519 of file qvectornd.h.

References qHypot().

Referenced by distanceToLine(), QOpenGL2PaintEngineExPrivate::drawCachedGlyphs(), QQuickPinchHandler::handlePointerEventImpl(), QQuickVector2DValueType::length(), normalize(), and normalized().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lengthSquared()

constexpr float QVector2D::lengthSquared ( ) const
inlineconstexprnoexcept

Returns the squared length of the vector from the origin.

This is equivalent to the dot product of the vector with itself.

See also
length(), dotProduct()

Definition at line 524 of file qvectornd.h.

◆ normalize()

void QVector2D::normalize ( )
inlinenoexcept

Normalizes the current vector in place.

Nothing happens if this vector is a null vector or the length of the vector is very close to 1.

See also
length(), normalized()

Definition at line 536 of file qvectornd.h.

References length(), and qFuzzyIsNull().

+ Here is the call graph for this function:

◆ normalized()

QVector2D QVector2D::normalized ( ) const
inlinenoexcept

Returns the normalized unit vector form of this vector.

If this vector is null, then a null vector is returned. If the length of the vector is very close to 1, then the vector will be returned as-is. Otherwise the normalized form of the vector of length 1 will be returned.

See also
length(), normalize()

Definition at line 529 of file qvectornd.h.

References QVector2D(), length(), qFuzzyIsNull(), and this.

Referenced by QQuickVector2DValueType::normalized(), and QSGCurveProcessor::processStroke().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator QVariant()

QVector2D::operator QVariant ( ) const

Returns the 2D vector as a QVariant.

Definition at line 421 of file qvectornd.cpp.

References QVariant::fromValue().

+ Here is the call graph for this function:

◆ operator*=() [1/2]

constexpr QVector2D & QVector2D::operator*= ( float factor)
inlineconstexprnoexcept

Multiplies this vector's coordinates by the given finite factor and returns a reference to this vector.

See also
operator/=(), operator*()

Definition at line 573 of file qvectornd.h.

◆ operator*=() [2/2]

constexpr QVector2D & QVector2D::operator*= ( QVector2D vector)
inlineconstexprnoexcept

Multiplies each component of this vector by the corresponding component of vector and returns a reference to this vector.

Note
This is not a cross product of this vector with vector. (Its components add up to the dot product of this vector and vector.)
See also
operator/=(), operator*()

Definition at line 580 of file qvectornd.h.

References vector.

◆ operator+=()

constexpr QVector2D & QVector2D::operator+= ( QVector2D vector)
inlineconstexprnoexcept

Adds the given vector to this vector and returns a reference to this vector.

See also
operator-=()

Definition at line 559 of file qvectornd.h.

References vector.

◆ operator-=()

constexpr QVector2D & QVector2D::operator-= ( QVector2D vector)
inlineconstexprnoexcept

Subtracts the given vector from this vector and returns a reference to this vector.

See also
operator+=()

Definition at line 566 of file qvectornd.h.

References vector.

◆ operator/=() [1/2]

constexpr QVector2D & QVector2D::operator/= ( float divisor)
inlineconstexpr

Divides this vector's coordinates by the given divisor and returns a reference to this vector.

The divisor must not be either zero or NaN.

See also
operator*=()

Definition at line 587 of file qvectornd.h.

References Q_ASSERT.

◆ operator/=() [2/2]

constexpr QVector2D & QVector2D::operator/= ( QVector2D vector)
inlineconstexpr
Since
5.5

Divides each component of this vector by the corresponding component of vector and returns a reference to this vector.

The vector must have no component that is either zero or NaN.

See also
operator*=(), operator/()

Definition at line 595 of file qvectornd.h.

References Q_ASSERT, and vector.

◆ operator[]() [1/2]

constexpr float & QVector2D::operator[] ( int i)
inlineconstexpr
Since
5.2

Returns the component of the vector at index position i as a modifiable reference.

i must be a valid index position in the vector (i.e., 0 <= i < 2).

Definition at line 507 of file qvectornd.h.

References i, and Q_ASSERT.

◆ operator[]() [2/2]

constexpr float QVector2D::operator[] ( int i) const
inlineconstexpr
Since
5.2

Returns the component of the vector at index position i.

i must be a valid index position in the vector (i.e., 0 <= i < 2).

Definition at line 513 of file qvectornd.h.

References i, and Q_ASSERT.

◆ setX()

constexpr void QVector2D::setX ( float x)
inlineconstexprnoexcept

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

See also
x(), setY()

Definition at line 504 of file qvectornd.h.

Referenced by QSSGRenderCamera::calculateProjection(), QQuadPath::controlPointRect(), QQuick3DParticleShapeDataUtils::readValue(), and QtWaylandClient::QWaylandTouchExtension::touch_extension_touch().

+ Here is the caller graph for this function:

◆ setY()

constexpr void QVector2D::setY ( float y)
inlineconstexprnoexcept

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

See also
y(), setX()

Definition at line 505 of file qvectornd.h.

Referenced by QSSGRenderCamera::calculateProjection(), QQuadPath::controlPointRect(), and QtWaylandClient::QWaylandTouchExtension::touch_extension_touch().

+ Here is the caller graph for this function:

◆ toPoint()

constexpr QPoint QVector2D::toPoint ( ) const
inlineconstexprnoexcept

Returns the QPoint form of this 2D vector.

Each coordinate is rounded to the nearest integer.

See also
toPointF(), toVector3D()

Definition at line 623 of file qvectornd.h.

References qRound().

+ Here is the call graph for this function:

◆ toPointF()

constexpr QPointF QVector2D::toPointF ( ) const
inlineconstexprnoexcept

Returns the QPointF form of this 2D vector.

See also
toPoint(), toVector3D()

Definition at line 628 of file qvectornd.h.

Referenced by QQuadPath::controlPointRect(), operator<<(), and QSGCurveProcessor::processFill().

+ Here is the caller graph for this function:

◆ toVector3D()

constexpr QVector3D QVector2D::toVector3D ( ) const
inlineconstexprnoexcept

Returns the 3D form of this 2D vector, with the z coordinate set to zero.

See also
toVector4D(), toPoint()

Definition at line 610 of file qvectornd.h.

References QVector3D.

◆ toVector4D()

constexpr QVector4D QVector2D::toVector4D ( ) const
inlineconstexprnoexcept

Returns the 4D form of this 2D vector, with the z and w coordinates set to zero.

See also
toVector3D(), toPoint()

Definition at line 616 of file qvectornd.h.

References QVector4D.

◆ x()

constexpr float QVector2D::x ( ) const
inlineconstexprnoexcept

Returns the x coordinate of this point.

See also
setX(), y()

Definition at line 501 of file qvectornd.h.

Referenced by QSGCurveFillNode::appendTriangle(), QSGCurveFillNode::appendTriangle(), QSSGQmlUtilities::builtinQmlType(), QSSGRenderImage::calculateTextureTransform(), QQuadPath::contains(), QQuadPath::controlPointRect(), QQuickFlickablePrivate::drag(), QQuickDeliveryAgentPrivate::dragOverThreshold(), QQuickDeliveryAgentPrivate::dragOverThreshold(), QQuickDeliveryAgentPrivate::dragOverThreshold(), QQuickPointerHandlerPrivate::dragOverThreshold(), QQuadPath::Element::extent(), QQuadPath::fillSideOf(), QSGCurveProcessor::findOverlappingCandidates(), QQuick3DSceneRenderer::getRayFromViewportPos(), QQuickPinchHandler::handlePointerEventImpl(), importanceSampleGGX(), QtWayland::TouchExtensionGlobal::postTouchEvent(), QT_FT_Outline_Decompose(), QSSGUtils::rect::relativeToNormalizedCoordinates(), QSGCurveProcessor::removeNestedSubpaths(), QQuickContext2DCommandBuffer::replay(), QQuickDragHandler::setActiveTranslation(), QQuickDragHandler::setPersistentTranslation(), QQuickVectorImageGenerator::Utils::toSvgString(), QSSGRenderCamera::unproject(), QWavefrontMesh::updateGeometry(), QQuickShapeLinearGradientRhiShader::updateUniformData(), QQuickShapeRadialGradientRhiShader::updateUniformData(), and QQuickShapeConicalGradientRhiShader::updateUniformData().

+ Here is the caller graph for this function:

◆ y()

constexpr float QVector2D::y ( ) const
inlineconstexprnoexcept

Friends And Related Symbol Documentation

◆ get

template<std::size_t I, typename V , std::enable_if_t<(I< 2), bool > = true, std::enable_if_t< std::is_same_v< q20::remove_cvref_t< V >, QVector2D >, bool > = true>
constexpr decltype(auto) get ( V && vec)
friend

Definition at line 154 of file qvectornd.h.

◆ operator!=

bool QVector2D::operator!= ( QVector2D v1,
QVector2D v2 )
friend

Returns true if v1 is not equal to v2; otherwise returns false.

This operator uses an exact floating-point comparison.

Definition at line 81 of file qvectornd.h.

◆ operator* [1/3]

const QVector2D QVector2D::operator* ( float factor,
QVector2D vector )
friend

//! friend

Returns a copy of the given vector, multiplied by the given finite factor.

See also
QVector2D::operator*=()

Definition at line 97 of file qvectornd.h.

◆ operator* [2/3]

const QVector2D QVector2D::operator* ( QVector2D v1,
QVector2D v2 )
friend

//! friend

Returns the QVector2D object formed by multiplying each component of v1 by the corresponding component of v2.

Note
This is not a cross product of v1 and v2 in any sense. (Its components add up to the dot product of v1 and v2.)
See also
QVector2D::operator*=()

Definition at line 107 of file qvectornd.h.

◆ operator* [3/3]

const QVector2D QVector2D::operator* ( QVector2D vector,
float factor )
friend

//! friend

Returns a copy of the given vector, multiplied by the given finite factor.

See also
QVector2D::operator*=()

Definition at line 102 of file qvectornd.h.

◆ operator+

const QVector2D QVector2D::operator+ ( QVector2D v1,
QVector2D v2 )
friend

//! friend

Returns a QVector2D object that is the sum of the given vectors, v1 and v2; each component is added separately.

See also
QVector2D::operator+=()

Definition at line 87 of file qvectornd.h.

◆ operator- [1/2]

const QVector2D QVector2D::operator- ( QVector2D v1,
QVector2D v2 )
friend

//! friend

Returns a QVector2D object that is formed by subtracting v2 from v1; each component is subtracted separately.

See also
QVector2D::operator-=()

Definition at line 92 of file qvectornd.h.

◆ operator- [2/2]

const QVector2D QVector2D::operator- ( QVector2D vector)
friend

//! friendThis is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a QVector2D object that is formed by changing the sign of each component of the given vector.

Equivalent to {QVector2D(0,0) - vector}.

Definition at line 112 of file qvectornd.h.

◆ operator/ [1/2]

const QVector2D QVector2D::operator/ ( QVector2D vector,
float divisor )
friend

//! friend

Returns the QVector2D object formed by dividing each component of the given vector by the given divisor.

The divisor must not be either zero or NaN.

See also
QVector2D::operator/=()

Definition at line 117 of file qvectornd.h.

◆ operator/ [2/2]

const QVector2D QVector2D::operator/ ( QVector2D vector,
QVector2D divisor )
friend

//! friend

Since
5.5

Returns the QVector2D object formed by dividing each component of the given vector by the corresponding component of the given divisor.

The divisor must have no component that is either zero or NaN.

See also
QVector2D::operator/=()

Definition at line 123 of file qvectornd.h.

◆ operator<<()

QDataStream & operator<< ( QDataStream & stream,
QVector2D vector )
related

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

See also
{Serializing Qt Data Types}

Definition at line 449 of file qvectornd.cpp.

References vector.

◆ operator==

bool QVector2D::operator== ( QVector2D v1,
QVector2D v2 )
friend

Returns true if v1 is equal to v2; otherwise returns false.

This operator uses an exact floating-point comparison.

Definition at line 76 of file qvectornd.h.

◆ operator>>()

QDataStream & operator>> ( QDataStream & stream,
QVector2D & vector )
related

Reads a 2D vector from the given stream into the given vector and returns a reference to the stream.

See also
{Serializing Qt Data Types}

Definition at line 465 of file qvectornd.cpp.

References Q_ASSERT, qIsFinite(), and vector.

+ Here is the call graph for this function:

◆ qFuzzyCompare

bool QVector2D::qFuzzyCompare ( QVector2D v1,
QVector2D v2 )
friend

//! friend

Returns true if v1 and v2 are equal, allowing for a small fuzziness factor for floating-point comparisons; false otherwise.

Definition at line 376 of file qvectornd.cpp.

◆ QVector3D

friend class QVector3D
friend

Definition at line 147 of file qvectornd.h.

Referenced by toVector3D().

◆ QVector4D

friend class QVector4D
friend

Definition at line 148 of file qvectornd.h.

Referenced by toVector4D().


The documentation for this class was generated from the following files: