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>
23#define QVECTORND_IS_HASHABLE
41 constexpr explicit QVector2D(QPoint point)
noexcept;
42 constexpr explicit QVector2D(QPointF point)
noexcept;
50 constexpr bool isNull()
const noexcept;
52 constexpr float x()
const noexcept;
53 constexpr float y()
const noexcept;
55 constexpr void setX(
float x)
noexcept;
56 constexpr void setY(
float y)
noexcept;
59 constexpr float operator[](
int i)
const;
61 [[nodiscard]]
float length()
const noexcept;
80QT_WARNING_DISABLE_FLOAT_COMPARE
141#ifndef QT_NO_VECTOR3D
144#ifndef QT_NO_VECTOR4D
163 friend constexpr decltype(
auto)
get(
V &&
vec)
noexcept
177#ifndef QT_NO_VECTOR3D
187 constexpr explicit QVector3D(QPointF point)
noexcept;
188#ifndef QT_NO_VECTOR2D
192#ifndef QT_NO_VECTOR4D
196 constexpr bool isNull()
const noexcept;
198 constexpr float x()
const noexcept;
199 constexpr float y()
const noexcept;
200 constexpr float z()
const noexcept;
202 constexpr void setX(
float x)
noexcept;
203 constexpr void setY(
float y)
noexcept;
204 constexpr void setZ(
float z)
noexcept;
207 constexpr float operator[](
int i)
const;
209 [[nodiscard]]
float length()
const noexcept;
210 [[nodiscard]]
constexpr float lengthSquared()
const noexcept;
232QT_WARNING_DISABLE_FLOAT_COMPARE
255 return QVector3D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1], v1.v[2] + v2.v[2]);
260 return QVector3D(v1.v[0] - v2.v[0], v1.v[1] - v2.v[1], v1.v[2] - v2.v[2]);
265 return QVector3D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor);
270 return QVector3D(vector.v[0] * factor, vector.v[1] * factor, vector.v[2] * factor);
275 return QVector3D(v1.v[0] * v2.v[0], v1.v[1] * v2.v[1], v1.v[2] * v2.v[2]);
280 return QVector3D(-vector.v[0], -vector.v[1], -vector.v[2]);
285 Q_ASSERT(divisor < 0 || divisor > 0);
286 return QVector3D(vector.v[0] / divisor, vector.v[1] / divisor, vector.v[2] / divisor);
291 Q_ASSERT(divisor.v[0] > 0 || divisor.v[0] < 0);
292 Q_ASSERT(divisor.v[1] > 0 || divisor.v[1] < 0);
293 Q_ASSERT(divisor.v[2] > 0 || divisor.v[2] < 0);
294 return QVector3D(vector.v[0] / divisor.v[0], vector.v[1] / divisor.v[1],
295 vector.v[2] / divisor.v[2]);
300#ifndef QT_NO_VECTOR2D
303#ifndef QT_NO_VECTOR4D
317#ifndef QT_NO_MATRIX4X4
326 friend constexpr decltype(
auto)
get(
V &&
vec)
noexcept
340#ifndef QT_NO_VECTOR4D
348 constexpr explicit QVector4D(QPoint point)
noexcept;
349 constexpr explicit QVector4D(QPointF point)
noexcept;
350#ifndef QT_NO_VECTOR2D
354#ifndef QT_NO_VECTOR3D
359 constexpr bool isNull()
const noexcept;
361 constexpr float x()
const noexcept;
362 constexpr float y()
const noexcept;
363 constexpr float z()
const noexcept;
364 constexpr float w()
const noexcept;
366 constexpr void setX(
float x)
noexcept;
367 constexpr void setY(
float y)
noexcept;
368 constexpr void setZ(
float z)
noexcept;
369 constexpr void setW(
float w)
noexcept;
372 constexpr float operator[](
int i)
const;
374 [[nodiscard]]
float length()
const noexcept;
375 [[nodiscard]]
constexpr float lengthSquared()
const noexcept;
390QT_WARNING_DISABLE_FLOAT_COMPARE
454#ifndef QT_NO_VECTOR2D
458#ifndef QT_NO_VECTOR3D
473 friend class QMatrix4x4;
474#ifndef QT_NO_MATRIX4X4
483 friend constexpr decltype(
auto)
get(
V &&
vec)
noexcept
497#ifndef QT_NO_VECTOR2D
501constexpr inline QVector2D::QVector2D(
float xpos,
float ypos)
noexcept : v{xpos, ypos} {}
503constexpr inline QVector2D::
QVector2D(QPoint point)
noexcept : v{
float(point.x()),
float(point.y())} {}
505constexpr inline QVector2D::
QVector2D(QPointF point)
noexcept : v{
float(point.x()),
float(point.y())} {}
507#ifndef QT_NO_VECTOR3D
510#ifndef QT_NO_VECTOR4D
516 return qIsNull(v[0]) && qIsNull(v[1]);
519constexpr inline float QVector2D::
x()
const noexcept {
return v[0]; }
520constexpr inline float QVector2D::
y()
const noexcept {
return v[1]; }
527 Q_ASSERT(uint(i) < 2u);
533 Q_ASSERT(uint(i) < 2u);
539 return qHypot(v[0], v[1]);
544 return v[0] * v[0] + v[1] * v[1];
550 return qFuzzyIsNull(len - 1.0f) ? *
this : qFuzzyIsNull(len) ?
QVector2D()
551 : QVector2D(v[0] / len, v[1] / len);
557 if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
566 return (*
this - point).length();
572 return (*
this - point).length();
574 return (*
this - p).length();
607 Q_ASSERT(divisor < 0 || divisor > 0);
615 Q_ASSERT(vector.v[0] > 0 || vector.v[0] < 0);
616 Q_ASSERT(vector.v[1] > 0 || vector.v[1] < 0);
624 return v1.v[0] * v2.v[0] + v1.v[1] * v2.v[1];
627#ifndef QT_NO_VECTOR3D
630 return QVector3D(v[0], v[1], 0.0f);
633#ifndef QT_NO_VECTOR4D
636 return QVector4D(v[0], v[1], 0.0f, 0.0f);
643 return QPoint(qRound(v[0]), qRound(v[1]));
648 return QPointF(qreal(v[0]), qreal(v[1]));
651#ifndef QT_NO_DEBUG_STREAM
655#ifndef QT_NO_DATASTREAM
666#ifndef QT_NO_VECTOR3D
670constexpr inline QVector3D::
QVector3D(QPoint point)
noexcept : v{
float(point.x()),
float(point.y()), 0.0f} {}
672constexpr inline QVector3D::
QVector3D(QPointF point)
noexcept : v{
float(point.x()),
float(point.y()), 0.0f} {}
674#ifndef QT_NO_VECTOR2D
679#ifndef QT_NO_VECTOR4D
685 return qIsNull(v[0]) && qIsNull(v[1]) && qIsNull(v[2]);
688constexpr inline float QVector3D::
x()
const noexcept {
return v[0]; }
689constexpr inline float QVector3D::
y()
const noexcept {
return v[1]; }
690constexpr inline float QVector3D::
z()
const noexcept {
return v[2]; }
698 Q_ASSERT(uint(i) < 3u);
704 Q_ASSERT(uint(i) < 3u);
710 return qHypot(v[0], v[1], v[2]);
716 return qFuzzyIsNull(len - 1.0f) ? *
this : qFuzzyIsNull(len) ?
QVector3D()
717 : QVector3D(v[0] / len, v[1] / len, v[2] / len);
723 if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
733 return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
770 Q_ASSERT(divisor < 0 || divisor > 0);
779 Q_ASSERT(vector.v[0] > 0 || vector.v[0] < 0);
780 Q_ASSERT(vector.v[1] > 0 || vector.v[1] < 0);
781 Q_ASSERT(vector.v[2] > 0 || vector.v[2] < 0);
790 return v1.v[0] * v2.v[0] + v1.v[1] * v2.v[1] + v1.v[2] * v2.v[2];
795 return QVector3D(v1.v[1] * v2.v[2] - v1.v[2] * v2.v[1],
796 v1.v[2] * v2.v[0] - v1.v[0] * v2.v[2],
797 v1.v[0] * v2.v[1] - v1.v[1] * v2.v[0]);
834#ifndef QT_NO_VECTOR2D
837 return QVector2D(v[0], v[1]);
840#ifndef QT_NO_VECTOR4D
843 return QVector4D(v[0], v[1], v[2], 0.0f);
849 return QPoint(qRound(v[0]), qRound(v[1]));
854 return QPointF(qreal(v[0]), qreal(v[1]));
857#ifndef QT_NO_DEBUG_STREAM
861#ifndef QT_NO_DATASTREAM
872#ifndef QT_NO_VECTOR4D
876constexpr inline QVector4D::QVector4D(
float xpos,
float ypos,
float zpos,
float wpos)
noexcept : v{xpos, ypos, zpos, wpos} {}
878constexpr inline QVector4D::
QVector4D(QPoint point)
noexcept : v{
float(point.x()),
float(point.y()), 0.0f, 0.0f} {}
880constexpr inline QVector4D::
QVector4D(QPointF point)
noexcept : v{
float(point.x()),
float(point.y()), 0.0f, 0.0f} {}
882#ifndef QT_NO_VECTOR2D
886#ifndef QT_NO_VECTOR3D
893 return qIsNull(v[0]) && qIsNull(v[1]) && qIsNull(v[2]) && qIsNull(v[3]);
896constexpr inline float QVector4D::
x()
const noexcept {
return v[0]; }
897constexpr inline float QVector4D::
y()
const noexcept {
return v[1]; }
898constexpr inline float QVector4D::
z()
const noexcept {
return v[2]; }
899constexpr inline float QVector4D::
w()
const noexcept {
return v[3]; }
908 Q_ASSERT(uint(i) < 4u);
914 Q_ASSERT(uint(i) < 4u);
920 return qHypot(v[0], v[1], v[2], v[3]);
925 return v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3];
931 return qFuzzyIsNull(len - 1.0f) ? *
this : qFuzzyIsNull(len) ?
QVector4D()
932 : QVector4D(v[0] / len, v[1] / len, v[2] / len, v[3] / len);
938 if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
985 Q_ASSERT(divisor < 0 || divisor > 0);
995 Q_ASSERT(vector.v[0] > 0 || vector.v[0] < 0);
996 Q_ASSERT(vector.v[1] > 0 || vector.v[1] < 0);
997 Q_ASSERT(vector.v[2] > 0 || vector.v[2] < 0);
998 Q_ASSERT(vector.v[3] > 0 || vector.v[3] < 0);
1000 v[1] /= vector.v[1];
1001 v[2] /= vector.v[2];
1002 v[3] /= vector.v[3];
1008 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];
1011#ifndef QT_NO_VECTOR2D
1015 return QVector2D(v[0], v[1]);
1022 return QVector2D(v[0] / v[3], v[1] / v[3]);
1027#ifndef QT_NO_VECTOR3D
1031 return QVector3D(v[0], v[1], v[2]);
1038 return QVector3D(v[0] / v[3], v[1] / v[3], v[2] / v[3]);
1045 return QPoint(qRound(v[0]), qRound(v[1]));
1050 return QPointF(qreal(v[0]), qreal(v[1]));
1053#ifndef QT_NO_DEBUG_STREAM
1057#ifndef QT_NO_DATASTREAM
1070#ifndef QT_NO_VECTOR2D
1079#ifndef QT_NO_VECTOR3D
1090#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)