diff options
Diffstat (limited to 'src/gui/math3d/qvector2d.cpp')
-rw-r--r-- | src/gui/math3d/qvector2d.cpp | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/src/gui/math3d/qvector2d.cpp b/src/gui/math3d/qvector2d.cpp index 0426fc5..c3aaa42 100644 --- a/src/gui/math3d/qvector2d.cpp +++ b/src/gui/math3d/qvector2d.cpp @@ -42,7 +42,8 @@ #include "qvector2d.h" #include "qvector3d.h" #include "qvector4d.h" -#include "qmath3dutil_p.h" +#include <QtCore/qdebug.h> +#include <QtCore/qmath.h> QT_BEGIN_NAMESPACE @@ -74,12 +75,6 @@ QT_BEGIN_NAMESPACE */ /*! - \fn QVector2D::QVector2D(int xpos, int ypos) - - Constructs a vector with coordinates (\a xpos, \a ypos). -*/ - -/*! \fn QVector2D::QVector2D(const QPoint& point) Constructs a vector with x and y coordinates from a 2D \a point. @@ -169,7 +164,7 @@ QVector2D::QVector2D(const QVector4D& vector) */ qreal QVector2D::length() const { - return qvtsqrt64(qvtmul64(xp, xp) + qvtmul64(yp, yp)); + return qSqrt(xp * xp + yp * yp); } /*! @@ -180,37 +175,43 @@ qreal QVector2D::length() const */ qreal QVector2D::lengthSquared() const { - return qvtdot64(qvtmul64(xp, xp) + qvtmul64(yp, yp)); + return xp * xp + yp * yp; } /*! - Returns the normalized unit vector form of this vector. If this vector - is not null, the returned vector is guaranteed to be 1.0 in length. - If this vector is null, then a null vector is returned. + 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. \sa length(), normalize() */ QVector2D QVector2D::normalized() const { - qreal len = length(); - if (!qIsNull(len)) - return *this / len; + qreal len = lengthSquared(); + if (qFuzzyIsNull(len - 1.0f)) + return *this; + else if (!qFuzzyIsNull(len)) + return *this / qSqrt(len); else return QVector2D(); } /*! Normalizes the currect vector in place. Nothing happens if this - vector is a null vector. + vector is a null vector or the length of the vector is very close to 1. \sa length(), normalized() */ void QVector2D::normalize() { - qreal len = length(); - if (qIsNull(len)) + qreal len = lengthSquared(); + if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len)) return; + len = qSqrt(len); + xp /= len; yp /= len; } @@ -263,7 +264,7 @@ void QVector2D::normalize() */ qreal QVector2D::dotProduct(const QVector2D& v1, const QVector2D& v2) { - return qvtdot64(qvtmul64(v1.xp, v2.xp) + qvtmul64(v1.yp, v2.yp)); + return v1.xp * v2.xp + v1.yp * v2.yp; } /*! |