diff options
author | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-09-21 11:30:06 +1000 |
---|---|---|
committer | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-09-21 11:30:06 +1000 |
commit | 2dfdd7d0f937f2d3fe7cb5327ac199f701de7890 (patch) | |
tree | 52a16fa388dacd4788777b1dc008f56e8c9920f1 /src/gui/math3d/qvector2d.cpp | |
parent | 11fed1f64f43593a2890e0a3f27b4e2e7ebde783 (diff) |
Increase precision when normalizing vectors
QVector3D::normalize() and other similar functions were losing
precision when qreal was float, causing vectors with small lengths
to normalize to (0, 0, 0). Do the length calculations with
double precision to avoid this problem.
Reviewed-by: Sarah Smith
Diffstat (limited to 'src/gui/math3d/qvector2d.cpp')
-rw-r--r-- | src/gui/math3d/qvector2d.cpp | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/src/gui/math3d/qvector2d.cpp b/src/gui/math3d/qvector2d.cpp index f12bec008d..2555a6f3bb 100644 --- a/src/gui/math3d/qvector2d.cpp +++ b/src/gui/math3d/qvector2d.cpp @@ -190,7 +190,9 @@ qreal QVector2D::lengthSquared() const */ QVector2D QVector2D::normalized() const { - qreal len = lengthSquared(); + // Need some extra precision if the length is very small. + double len = double(xp) * double(xp) + + double(yp) * double(yp); if (qFuzzyIsNull(len - 1.0f)) return *this; else if (!qFuzzyIsNull(len)) @@ -207,7 +209,9 @@ QVector2D QVector2D::normalized() const */ void QVector2D::normalize() { - qreal len = lengthSquared(); + // Need some extra precision if the length is very small. + double len = double(xp) * double(xp) + + double(yp) * double(yp); if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len)) return; |