9#include <QtGui/qtguiglobal.h>
11#include <QtCore/qhashfunctions.h>
12#include <QtCore/qpoint.h>
13#include <QtCore/qrect.h>
14#include <QtCore/qmath.h>
16#include <QtCore/q20type_traits.h>
17#include <QtCore/q23utility.h>
39 constexpr explicit QVector2D(QPoint point)
noexcept;
40 constexpr explicit QVector2D(QPointF point)
noexcept;
48 constexpr bool isNull()
const noexcept;
50 constexpr float x()
const noexcept;
51 constexpr float y()
const noexcept;
53 constexpr void setX(
float x)
noexcept;
54 constexpr void setY(
float y)
noexcept;
57 constexpr float operator[](
int i)
const;
59 [[nodiscard]]
float length()
const noexcept;
78QT_WARNING_DISABLE_FLOAT_COMPARE
139#ifndef QT_NO_VECTOR3D
142#ifndef QT_NO_VECTOR4D
161 friend constexpr decltype(
auto)
get(
V &&
vec)
noexcept
175#ifndef QT_NO_VECTOR3D
185 constexpr explicit QVector3D(QPointF point)
noexcept;
186#ifndef QT_NO_VECTOR2D
190#ifndef QT_NO_VECTOR4D
194 constexpr bool isNull()
const noexcept;
196 constexpr float x()
const noexcept;
197 constexpr float y()
const noexcept;
198 constexpr float z()
const noexcept;
200 constexpr void setX(
float x)
noexcept;
201 constexpr void setY(
float y)
noexcept;
202 constexpr void setZ(
float z)
noexcept;
205 constexpr float operator[](
int i)
const;
207 [[nodiscard]]
float length()
const noexcept;
208 [[nodiscard]]
constexpr float lengthSquared()
const noexcept;
230QT_WARNING_DISABLE_FLOAT_COMPARE
253 return QVector3D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1], v1.v[2] + v2.v[2]);
258 return QVector3D(v1.v[0] - v2.v[0], v1.v[1] - v2.v[1], v1.v[2] - v2.v[2]);
263 return QVector3D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor);
268 return QVector3D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor);
273 return QVector3D(v1.v[0] * v2.v[0], v1.v[1] * v2.v[1], v1.v[2] * v2.v[2]);
278 return QVector3D(-vector.v[0], -vector.v[1], -vector.v[2]);
283 Q_ASSERT(divisor < 0 || divisor > 0);
284 return QVector3D(vector.v[0] / divisor, vector.v[1] / divisor, vector.v[2] / divisor);
289 Q_ASSERT(divisor.v[0] > 0 || divisor.v[0] < 0);
290 Q_ASSERT(divisor.v[1] > 0 || divisor.v[1] < 0);
291 Q_ASSERT(divisor.v[2] > 0 || divisor.v[2] < 0);
292 return QVector3D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1],
293 vector.v[2] / divisor.v[2]);
298#ifndef QT_NO_VECTOR2D
301#ifndef QT_NO_VECTOR4D
315#ifndef QT_NO_MATRIX4X4
324 friend constexpr decltype(
auto)
get(
V &&
vec)
noexcept
338#ifndef QT_NO_VECTOR4D
346 constexpr explicit QVector4D(QPoint point)
noexcept;
347 constexpr explicit QVector4D(QPointF point)
noexcept;
348#ifndef QT_NO_VECTOR2D
352#ifndef QT_NO_VECTOR3D
357 constexpr bool isNull()
const noexcept;
359 constexpr float x()
const noexcept;
360 constexpr float y()
const noexcept;
361 constexpr float z()
const noexcept;
362 constexpr float w()
const noexcept;
364 constexpr void setX(
float x)
noexcept;
365 constexpr void setY(
float y)
noexcept;
366 constexpr void setZ(
float z)
noexcept;
367 constexpr void setW(
float w)
noexcept;
370 constexpr float operator[](
int i)
const;
372 [[nodiscard]]
float length()
const noexcept;
373 [[nodiscard]]
constexpr float lengthSquared()
const noexcept;
388QT_WARNING_DISABLE_FLOAT_COMPARE
452#ifndef QT_NO_VECTOR2D
456#ifndef QT_NO_VECTOR3D
471 friend class QMatrix4x4;
472#ifndef QT_NO_MATRIX4X4
481 friend constexpr decltype(
auto)
get(
V &&
vec)
noexcept
495#ifndef QT_NO_VECTOR2D
499constexpr inline QVector2D::QVector2D(
float xpos,
float ypos)
noexcept : v{xpos, ypos} {}
501constexpr inline QVector2D::
QVector2D(QPoint point)
noexcept : v{
float(point.x()),
float(point.y())} {}
503constexpr inline QVector2D::
QVector2D(QPointF point)
noexcept : v{
float(point.x()),
float(point.y())} {}
505#ifndef QT_NO_VECTOR3D
508#ifndef QT_NO_VECTOR4D
514 return qIsNull(v[0]) && qIsNull(v[1]);
517constexpr inline float QVector2D::
x()
const noexcept {
return v[0]; }
518constexpr inline float QVector2D::
y()
const noexcept {
return v[1]; }
525 Q_ASSERT(uint(i) < 2u);
531 Q_ASSERT(uint(i) < 2u);
537 return qHypot(v[0], v[1]);
542 return v[0] * v[0] + v[1] * v[1];
548 return qFuzzyIsNull(len - 1.0f) ? *
this : qFuzzyIsNull(len) ?
QVector2D()
549 : QVector2D(v[0] / len, v[1] / len);
555 if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
564 return (*
this - point).length();
570 return (*
this - point).length();
572 return (*
this - p).length();
605 Q_ASSERT(divisor < 0 || divisor > 0);
613 Q_ASSERT(vector.v[0] > 0 || vector.v[0] < 0);
614 Q_ASSERT(vector.v[1] > 0 || vector.v[1] < 0);
622 return v1.v[0] * v2.v[0] + v1.v[1] * v2.v[1];
625#ifndef QT_NO_VECTOR3D
628 return QVector3D(v[0], v[1], 0.0f);
631#ifndef QT_NO_VECTOR4D
634 return QVector4D(v[0], v[1], 0.0f, 0.0f);
641 return QPoint(qRound(v[0]), qRound(v[1]));
646 return QPointF(qreal(v[0]), qreal(v[1]));
649#ifndef QT_NO_DEBUG_STREAM
653#ifndef QT_NO_DATASTREAM
664#ifndef QT_NO_VECTOR3D
668constexpr inline QVector3D::
QVector3D(QPoint point)
noexcept : v{
float(point.x()),
float(point.y()), 0.0f} {}
670constexpr inline QVector3D::
QVector3D(QPointF point)
noexcept : v{
float(point.x()),
float(point.y()), 0.0f} {}
672#ifndef QT_NO_VECTOR2D
677#ifndef QT_NO_VECTOR4D
683 return qIsNull(v[0]) && qIsNull(v[1]) && qIsNull(v[2]);
686constexpr inline float QVector3D::
x()
const noexcept {
return v[0]; }
687constexpr inline float QVector3D::
y()
const noexcept {
return v[1]; }
688constexpr inline float QVector3D::
z()
const noexcept {
return v[2]; }
696 Q_ASSERT(uint(i) < 3u);
702 Q_ASSERT(uint(i) < 3u);
708 return qHypot(v[0], v[1], v[2]);
714 return qFuzzyIsNull(len - 1.0f) ? *
this : qFuzzyIsNull(len) ?
QVector3D()
715 : QVector3D(v[0] / len, v[1] / len, v[2] / len);
721 if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
731 return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
768 Q_ASSERT(divisor < 0 || divisor > 0);
777 Q_ASSERT(vector.v[0] > 0 || vector.v[0] < 0);
778 Q_ASSERT(vector.v[1] > 0 || vector.v[1] < 0);
779 Q_ASSERT(vector.v[2] > 0 || vector.v[2] < 0);
788 return v1.v[0] * v2.v[0] + v1.v[1] * v2.v[1] + v1.v[2] * v2.v[2];
793 return QVector3D(v1.v[1] * v2.v[2] - v1.v[2] * v2.v[1],
794 v1.v[2] * v2.v[0] - v1.v[0] * v2.v[2],
795 v1.v[0] * v2.v[1] - v1.v[1] * v2.v[0]);
832#ifndef QT_NO_VECTOR2D
835 return QVector2D(v[0], v[1]);
838#ifndef QT_NO_VECTOR4D
841 return QVector4D(v[0], v[1], v[2], 0.0f);
847 return QPoint(qRound(v[0]), qRound(v[1]));
852 return QPointF(qreal(v[0]), qreal(v[1]));
855#ifndef QT_NO_DEBUG_STREAM
859#ifndef QT_NO_DATASTREAM
870#ifndef QT_NO_VECTOR4D
874constexpr inline QVector4D::QVector4D(
float xpos,
float ypos,
float zpos,
float wpos)
noexcept : v{xpos, ypos, zpos, wpos} {}
876constexpr inline QVector4D::
QVector4D(QPoint point)
noexcept : v{
float(point.x()),
float(point.y()), 0.0f, 0.0f} {}
878constexpr inline QVector4D::
QVector4D(QPointF point)
noexcept : v{
float(point.x()),
float(point.y()), 0.0f, 0.0f} {}
880#ifndef QT_NO_VECTOR2D
884#ifndef QT_NO_VECTOR3D
891 return qIsNull(v[0]) && qIsNull(v[1]) && qIsNull(v[2]) && qIsNull(v[3]);
894constexpr inline float QVector4D::
x()
const noexcept {
return v[0]; }
895constexpr inline float QVector4D::
y()
const noexcept {
return v[1]; }
896constexpr inline float QVector4D::
z()
const noexcept {
return v[2]; }
897constexpr inline float QVector4D::
w()
const noexcept {
return v[3]; }
906 Q_ASSERT(uint(i) < 4u);
912 Q_ASSERT(uint(i) < 4u);
918 return qHypot(v[0], v[1], v[2], v[3]);
923 return v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3];
929 return qFuzzyIsNull(len - 1.0f) ? *
this : qFuzzyIsNull(len) ?
QVector4D()
930 : QVector4D(v[0] / len, v[1] / len, v[2] / len, v[3] / len);
936 if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
983 Q_ASSERT(divisor < 0 || divisor > 0);
993 Q_ASSERT(vector.v[0] > 0 || vector.v[0] < 0);
994 Q_ASSERT(vector.v[1] > 0 || vector.v[1] < 0);
995 Q_ASSERT(vector.v[2] > 0 || vector.v[2] < 0);
996 Q_ASSERT(vector.v[3] > 0 || vector.v[3] < 0);
1000 v[3] /= vector.v[3];
1006 return v1.v[0] * v2.v[0] + v1.v[1] * v2.v[1] + v1.v[2] * v2.v[2] + v1.v[3] * v2.v[3];
1009#ifndef QT_NO_VECTOR2D
1013 return QVector2D(v[0], v[1]);
1020 return QVector2D(v[0] / v[3], v[1] / v[3]);
1025#ifndef QT_NO_VECTOR3D
1029 return QVector3D(v[0], v[1], v[2]);
1036 return QVector3D(v[0] / v[3], v[1] / v[3], v[2] / v[3]);
1043 return QPoint(qRound(v[0]), qRound(v[1]));
1048 return QPointF(qreal(v[0]), qreal(v[1]));
1051#ifndef QT_NO_DEBUG_STREAM
1055#ifndef QT_NO_DATASTREAM
1068#ifndef QT_NO_VECTOR2D
1077#ifndef QT_NO_VECTOR3D
1088#ifndef QT_NO_VECTOR4D
\inmodule QtCore\reentrant
The QVector2D class represents a vector or vertex in 2D space.
float length() const noexcept
Returns the length of the vector from the origin.
constexpr float y() const noexcept
Returns the y coordinate of this point.
constexpr QVector2D(QPoint point) noexcept
Constructs a vector with x and y coordinates from a 2D point.
constexpr float & operator[](int i)
QVector2D normalized() const noexcept
Returns the normalized unit vector form of this vector.
constexpr float lengthSquared() const noexcept
Returns the squared length of the vector from the origin.
constexpr QVector3D toVector3D() const noexcept
Returns the 3D form of this 2D vector, with the z coordinate set to zero.
constexpr QVector2D & operator*=(float factor) noexcept
Multiplies this vector's coordinates by the given finite factor and returns a reference to this vecto...
constexpr QVector2D & operator+=(QVector2D vector) noexcept
Adds the given vector to this vector and returns a reference to this vector.
constexpr float x() const noexcept
Returns the x coordinate of this point.
constexpr QVector2D(QVector4D vector) noexcept
Constructs a vector with x and y coordinates from a 3D vector.
static constexpr float dotProduct(QVector2D v1, QVector2D v2) noexcept
Returns the dot product of v1 and v2.
constexpr QVector2D & operator*=(QVector2D vector) noexcept
Multiplies each component of this vector by the corresponding component of vector and returns a refer...
constexpr QVector2D & operator/=(float divisor)
Divides this vector's coordinates by the given divisor and returns a reference to this vector.
float distanceToPoint(QVector2D point) const noexcept
constexpr QVector2D() noexcept
Constructs a null vector, i.e.
constexpr QVector2D & operator-=(QVector2D vector) noexcept
Subtracts the given vector from this vector and returns a reference to this vector.
constexpr float operator[](int i) const
constexpr void setY(float y) noexcept
Sets the y coordinate of this point to the given finite y coordinate.
constexpr QPointF toPointF() const noexcept
Returns the QPointF form of this 2D vector.
constexpr QVector2D(QVector3D vector) noexcept
Constructs a vector with x and y coordinates from a 3D vector.
constexpr void setX(float x) noexcept
Sets the x coordinate of this point to the given finite x coordinate.
constexpr QPoint toPoint() const noexcept
Returns the QPoint form of this 2D vector.
constexpr QVector4D toVector4D() const noexcept
Returns the 4D form of this 2D vector, with the z and w coordinates set to zero.
void normalize() noexcept
Normalizes the current vector in place.
float distanceToLine(QVector2D point, QVector2D direction) const noexcept
constexpr bool isNull() const noexcept
Returns true if the x and y coordinates are set to 0.0, otherwise returns false.
constexpr QVector2D & operator/=(QVector2D vector)
The QVector3D class represents a vector or vertex in 3D space.
constexpr void setX(float x) noexcept
Sets the x coordinate of this point to the given finite x coordinate.
float distanceToLine(QVector3D point, QVector3D direction) const noexcept
Returns the distance that this vertex is from a line defined by point and the unit vector direction.
constexpr bool isNull() const noexcept
Returns true if the x, y, and z coordinates are set to 0.0, otherwise returns false.
constexpr QVector3D & operator*=(float factor) noexcept
Multiplies this vector's coordinates by the given finite factor and returns a reference to this vecto...
constexpr float distanceToPlane(QVector3D plane, QVector3D normal) const noexcept
Returns the distance from this vertex to a plane defined by the vertex plane and a normal unit vector...
static QVector3D normal(QVector3D v1, QVector3D v2) noexcept
Returns the unit normal vector of a plane spanned by vectors v1 and v2, which must not be parallel to...
float length() const noexcept
Returns the length of the vector from the origin.
constexpr friend QVector3D operator*(QVector3D v1, QVector3D v2) noexcept
//!
constexpr friend QVector3D operator+(QVector3D v1, QVector3D v2) noexcept
//!
QVector3D normalized() const noexcept
Returns the normalized unit vector form of this vector.
constexpr float lengthSquared() const noexcept
Returns the squared length of the vector from the origin.
constexpr friend QVector3D operator*(QVector3D vector, float factor) noexcept
//!
constexpr void setY(float y) noexcept
Sets the y coordinate of this point to the given finite y coordinate.
static QVector3D normal(QVector3D v1, QVector3D v2, QVector3D v3) noexcept
Returns the unit normal vector of a plane spanned by vectors v2 - v1 and v3 - v1, which must not be p...
friend QVector3D operator*(const QVector3D &vector, const QMatrix4x4 &matrix)
constexpr float & operator[](int i)
constexpr QPoint toPoint() const noexcept
Returns the QPoint form of this 3D vector.
constexpr QPointF toPointF() const noexcept
Returns the QPointF form of this 3D vector.
constexpr QVector3D & operator-=(QVector3D vector) noexcept
Subtracts the given vector from this vector and returns a reference to this vector.
constexpr friend QVector3D operator-(QVector3D vector) noexcept
//!
constexpr QVector3D(QVector4D vector) noexcept
Constructs a 3D vector from the specified 4D vector.
constexpr float y() const noexcept
Returns the y coordinate of this point.
constexpr QVector2D toVector2D() const noexcept
Returns the 2D vector form of this 3D vector, dropping the z coordinate.
constexpr float operator[](int i) const
constexpr friend QVector3D operator/(QVector3D vector, float divisor)
//!
constexpr friend QVector3D operator/(QVector3D vector, QVector3D divisor)
//!
constexpr QVector4D toVector4D() const noexcept
Returns the 4D form of this 3D vector, with the w coordinate set to zero.
constexpr QVector3D(QPointF point) noexcept
Constructs a vector with x and y coordinates from a 2D point, and a z coordinate of 0.
constexpr float x() const noexcept
Returns the x coordinate of this point.
constexpr QVector3D(QVector2D vector, float zpos) noexcept
Constructs a 3D vector from the specified 2D vector.
constexpr QVector3D & operator*=(QVector3D vector) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
constexpr void setZ(float z) noexcept
Sets the z coordinate of this point to the given finite z coordinate.
constexpr QVector3D & operator/=(QVector3D vector)
friend QVector3D operator*(const QMatrix4x4 &matrix, const QVector3D &vector)
constexpr QVector3D & operator/=(float divisor)
Divides this vector's coordinates by the given divisor, and returns a reference to this vector.
static constexpr float dotProduct(QVector3D v1, QVector3D v2) noexcept
Returns the dot product of v1 and v2.
float distanceToPlane(QVector3D plane1, QVector3D plane2, QVector3D plane3) const noexcept
Returns the distance from this vertex to a plane defined by the vertices plane1, plane2 and plane3.
constexpr QVector3D() noexcept
Constructs a null vector, i.e.
constexpr QVector3D & operator+=(QVector3D vector) noexcept
Adds the given vector to this vector and returns a reference to this vector.
static constexpr QVector3D crossProduct(QVector3D v1, QVector3D v2) noexcept
Returns the cross-product of vectors v1 and v2, which is normal to the plane spanned by v1 and v2.
constexpr QVector3D(QVector2D vector) noexcept
Constructs a 3D vector from the specified 2D vector.
constexpr friend QVector3D operator*(float factor, QVector3D vector) noexcept
//!
void normalize() noexcept
Normalizes the current vector in place.
constexpr friend QVector3D operator-(QVector3D v1, QVector3D v2) noexcept
//!
constexpr float z() const noexcept
Returns the z coordinate of this point.
The QVector4D class represents a vector or vertex in 4D space.
constexpr QVector4D & operator-=(QVector4D vector) noexcept
Subtracts the given vector from this vector and returns a reference to this vector.
constexpr QVector4D(QVector2D vector) noexcept
Constructs a 4D vector from the specified 2D vector.
constexpr void setZ(float z) noexcept
Sets the z coordinate of this point to the given finite z coordinate.
QVector4D normalized() const noexcept
Returns the normalized unit vector form of this vector.
constexpr float lengthSquared() const noexcept
Returns the squared length of the vector from the origin.
constexpr QVector4D & operator*=(float factor) noexcept
Multiplies this vector's coordinates by the given finite factor, and returns a reference to this vect...
constexpr bool isNull() const noexcept
Returns true if the x, y, z, and w coordinates are set to 0.0, otherwise returns false.
constexpr float x() const noexcept
Returns the x coordinate of this point.
float length() const noexcept
Returns the length of the vector from the origin.
constexpr void setY(float y) noexcept
Sets the y coordinate of this point to the given finite y coordinate.
constexpr float w() const noexcept
Returns the w coordinate of this point.
friend QVector4D operator*(const QVector4D &vector, const QMatrix4x4 &matrix)
constexpr QVector4D(QPoint point) noexcept
Constructs a vector with x and y coordinates from a 2D point, and z and w coordinates of 0.
constexpr QVector2D toVector2D() const noexcept
Returns the 2D vector form of this 4D vector, dropping the z and w coordinates.
constexpr QVector2D toVector2DAffine() const noexcept
Returns the 2D vector form of this 4D vector, dividing the x and y coordinates by the w coordinate an...
constexpr void setX(float x) noexcept
Sets the x coordinate of this point to the given finite x coordinate.
constexpr QVector4D & operator/=(QVector4D vector)
friend QVector4D operator*(const QMatrix4x4 &matrix, const QVector4D &vector)
constexpr QVector4D(QVector3D vector) noexcept
Constructs a 4D vector from the specified 3D vector.
constexpr QVector4D & operator+=(QVector4D vector) noexcept
Adds the given vector to this vector and returns a reference to this vector.
void normalize() noexcept
Normalizes the current vector in place.
constexpr QVector4D & operator/=(float divisor)
Divides this vector's coordinates by the given divisor, and returns a reference to this vector.
constexpr float operator[](int i) const
constexpr QVector4D(QVector3D vector, float wpos) noexcept
Constructs a 4D vector from the specified 3D vector.
constexpr QVector4D & operator*=(QVector4D vector) noexcept
Multiplies each component of this vector by the corresponding component of vector and returns a refer...
constexpr float y() const noexcept
Returns the y coordinate of this point.
constexpr QPointF toPointF() const noexcept
Returns the QPointF form of this 4D vector.
constexpr QVector4D(QVector2D vector, float zpos, float wpos) noexcept
Constructs a 4D vector from the specified 2D vector.
static constexpr float dotProduct(QVector4D v1, QVector4D v2) noexcept
Returns the dot product of v1 and v2.
constexpr float & operator[](int i)
constexpr QPoint toPoint() const noexcept
Returns the QPoint form of this 4D vector.
constexpr QVector3D toVector3D() const noexcept
Returns the 3D vector form of this 4D vector, dropping the w coordinate.
constexpr QVector4D() noexcept
Constructs a null vector, i.e.
constexpr float z() const noexcept
Returns the z coordinate of this point.
constexpr void setW(float w) noexcept
Sets the w coordinate of this point to the given finite w coordinate.
constexpr QVector3D toVector3DAffine() const noexcept
Returns the 3D vector form of this 4D vector, dividing the x, y, and z coordinates by the w coordinat...
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
QDataStream & operator<<(QDataStream &stream, QVector2D vector)
QDataStream & operator<<(QDataStream &stream, QVector3D vector)
QDataStream & operator>>(QDataStream &stream, QVector2D &vector)
QDataStream & operator<<(QDataStream &stream, QVector4D vector)
QDebug operator<<(QDebug dbg, QVector2D vector)
bool qFuzzyCompare(QVector4D v1, QVector4D v2) noexcept
bool qFuzzyCompare(QVector3D v1, QVector3D v2) noexcept
QDebug operator<<(QDebug dbg, QVector4D vector)
QDebug operator<<(QDebug dbg, QVector3D vector)
QDataStream & operator>>(QDataStream &stream, QVector3D &vector)
QDataStream & operator>>(QDataStream &stream, QVector4D &vector)
bool qFuzzyCompare(QVector2D v1, QVector2D v2) noexcept
Q_DECLARE_TYPEINFO(QVector3D, Q_PRIMITIVE_TYPE)
Q_DECLARE_TYPEINFO(QVector4D, Q_PRIMITIVE_TYPE)
Q_DECLARE_TYPEINFO(QVector2D, Q_PRIMITIVE_TYPE)