diff options
author | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-04-15 23:21:44 (GMT) |
---|---|---|
committer | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-04-15 23:25:22 (GMT) |
commit | c57b1a8629b7fa79f48a35eea2f09d923b665dc0 (patch) | |
tree | f17a70d2534cea9b095afa479135b1e5ea79805e /src/gui/math3d/qvector2d.cpp | |
parent | 1cb11b428ef1bee070af72676b7eb1fa325bb980 (diff) | |
download | Qt-c57b1a8629b7fa79f48a35eea2f09d923b665dc0.zip Qt-c57b1a8629b7fa79f48a35eea2f09d923b665dc0.tar.gz Qt-c57b1a8629b7fa79f48a35eea2f09d923b665dc0.tar.bz2 |
Optimize vector normalize for vectors of length 1
If the square of the length is very close to 1, then avoid the qSqrt().
Reviewed-by: trustme
Diffstat (limited to 'src/gui/math3d/qvector2d.cpp')
-rw-r--r-- | src/gui/math3d/qvector2d.cpp | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/src/gui/math3d/qvector2d.cpp b/src/gui/math3d/qvector2d.cpp index 54e1712..c3aaa42 100644 --- a/src/gui/math3d/qvector2d.cpp +++ b/src/gui/math3d/qvector2d.cpp @@ -179,33 +179,39 @@ qreal QVector2D::lengthSquared() const } /*! - 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; } |