diff options
Diffstat (limited to 'src/gui/math3d/qvector2d.cpp')
-rw-r--r-- | src/gui/math3d/qvector2d.cpp | 64 |
1 files changed, 32 insertions, 32 deletions
diff --git a/src/gui/math3d/qvector2d.cpp b/src/gui/math3d/qvector2d.cpp index 672e2e4dd7..5d542c6f72 100644 --- a/src/gui/math3d/qvector2d.cpp +++ b/src/gui/math3d/qvector2d.cpp @@ -62,11 +62,6 @@ QT_BEGIN_NAMESPACE The QVector2D class can also be used to represent vertices in 2D space. We therefore do not need to provide a separate vertex class. - \b{Note:} By design values in the QVector2D instance are stored as \c float. - This means that on platforms where the \c qreal arguments to QVector2D - functions are represented by \c double values, it is possible to - lose precision. - \sa QVector3D, QVector4D, QQuaternion */ @@ -77,7 +72,7 @@ QT_BEGIN_NAMESPACE */ /*! - \fn QVector2D::QVector2D(qreal xpos, qreal ypos) + \fn QVector2D::QVector2D(float xpos, float ypos) Constructs a vector with coordinates (\a xpos, \a ypos). */ @@ -134,7 +129,7 @@ QVector2D::QVector2D(const QVector4D& vector) */ /*! - \fn qreal QVector2D::x() const + \fn float QVector2D::x() const Returns the x coordinate of this point. @@ -142,7 +137,7 @@ QVector2D::QVector2D(const QVector4D& vector) */ /*! - \fn qreal QVector2D::y() const + \fn float QVector2D::y() const Returns the y coordinate of this point. @@ -150,7 +145,7 @@ QVector2D::QVector2D(const QVector4D& vector) */ /*! - \fn void QVector2D::setX(qreal x) + \fn void QVector2D::setX(float x) Sets the x coordinate of this point to the given \a x coordinate. @@ -158,7 +153,7 @@ QVector2D::QVector2D(const QVector4D& vector) */ /*! - \fn void QVector2D::setY(qreal y) + \fn void QVector2D::setY(float y) Sets the y coordinate of this point to the given \a y coordinate. @@ -170,9 +165,12 @@ QVector2D::QVector2D(const QVector4D& vector) \sa lengthSquared(), normalized() */ -qreal QVector2D::length() const +float QVector2D::length() const { - return qSqrt(xp * xp + yp * yp); + // Need some extra precision if the length is very small. + double len = double(xp) * double(xp) + + double(yp) * double(yp); + return float(sqrt(len)); } /*! @@ -181,7 +179,7 @@ qreal QVector2D::length() const \sa length(), dotProduct() */ -qreal QVector2D::lengthSquared() const +float QVector2D::lengthSquared() const { return xp * xp + yp * yp; } @@ -200,12 +198,14 @@ QVector2D QVector2D::normalized() const // 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)) + if (qFuzzyIsNull(len - 1.0f)) { return *this; - else if (!qFuzzyIsNull(len)) - return *this / qSqrt(len); - else + } else if (!qFuzzyIsNull(len)) { + double sqrtLen = sqrt(len); + return QVector2D(float(double(xp) / sqrtLen), float(double(yp) / sqrtLen)); + } else { return QVector2D(); + } } /*! @@ -222,10 +222,10 @@ void QVector2D::normalize() if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len)) return; - len = qSqrt(len); + len = sqrt(len); - xp /= len; - yp /= len; + xp = float(double(xp) / len); + yp = float(double(yp) / len); } /*! @@ -247,7 +247,7 @@ void QVector2D::normalize() */ /*! - \fn QVector2D &QVector2D::operator*=(qreal factor) + \fn QVector2D &QVector2D::operator*=(float factor) Multiplies this vector's coordinates by the given \a factor, and returns a reference to this vector. @@ -263,7 +263,7 @@ void QVector2D::normalize() */ /*! - \fn QVector2D &QVector2D::operator/=(qreal divisor) + \fn QVector2D &QVector2D::operator/=(float divisor) Divides this vector's coordinates by the given \a divisor, and returns a reference to this vector. @@ -274,7 +274,7 @@ void QVector2D::normalize() /*! Returns the dot product of \a v1 and \a v2. */ -qreal QVector2D::dotProduct(const QVector2D& v1, const QVector2D& v2) +float QVector2D::dotProduct(const QVector2D& v1, const QVector2D& v2) { return v1.xp * v2.xp + v1.yp * v2.yp; } @@ -316,7 +316,7 @@ qreal QVector2D::dotProduct(const QVector2D& v1, const QVector2D& v2) */ /*! - \fn const QVector2D operator*(qreal factor, const QVector2D &vector) + \fn const QVector2D operator*(float factor, const QVector2D &vector) \relates QVector2D Returns a copy of the given \a vector, multiplied by the given \a factor. @@ -325,7 +325,7 @@ qreal QVector2D::dotProduct(const QVector2D& v1, const QVector2D& v2) */ /*! - \fn const QVector2D operator*(const QVector2D &vector, qreal factor) + \fn const QVector2D operator*(const QVector2D &vector, float factor) \relates QVector2D Returns a copy of the given \a vector, multiplied by the given \a factor. @@ -353,7 +353,7 @@ qreal QVector2D::dotProduct(const QVector2D& v1, const QVector2D& v2) */ /*! - \fn const QVector2D operator/(const QVector2D &vector, qreal divisor) + \fn const QVector2D operator/(const QVector2D &vector, float divisor) \relates QVector2D Returns the QVector2D object formed by dividing all three components of @@ -379,7 +379,7 @@ qreal QVector2D::dotProduct(const QVector2D& v1, const QVector2D& v2) */ QVector3D QVector2D::toVector3D() const { - return QVector3D(xp, yp, 0.0f, 1); + return QVector3D(xp, yp, 0.0f); } #endif @@ -393,7 +393,7 @@ QVector3D QVector2D::toVector3D() const */ QVector4D QVector2D::toVector4D() const { - return QVector4D(xp, yp, 0.0f, 0.0f, 1); + return QVector4D(xp, yp, 0.0f, 0.0f); } #endif @@ -446,7 +446,7 @@ QDebug operator<<(QDebug dbg, const QVector2D &vector) QDataStream &operator<<(QDataStream &stream, const QVector2D &vector) { - stream << double(vector.x()) << double(vector.y()); + stream << vector.x() << vector.y(); return stream; } @@ -462,11 +462,11 @@ QDataStream &operator<<(QDataStream &stream, const QVector2D &vector) QDataStream &operator>>(QDataStream &stream, QVector2D &vector) { - double x, y; + float x, y; stream >> x; stream >> y; - vector.setX(qreal(x)); - vector.setY(qreal(y)); + vector.setX(x); + vector.setY(y); return stream; } |