diff options
author | Laszlo Papp <lpapp@kde.org> | 2013-03-15 10:22:04 +0000 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-03-18 08:36:03 +0100 |
commit | 4905c0754bf4944a2889aae2c546a02ffeab0e5a (patch) | |
tree | 9ed88c353553cdf03feee92264fe3f5604dc7fcf /src | |
parent | b74ba4e198256f9c4a4c8af02a1339bf0ecc41aa (diff) |
Add convenience distance methods to QVector2D
Change-Id: I8ecdda35912a95e69c2f8dd98ce9c41c77b222d2
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Reviewed-by: Samuel Rødal <samuel.rodal@digia.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/math3d/qvector2d.cpp | 33 | ||||
-rw-r--r-- | src/gui/math3d/qvector2d.h | 3 |
2 files changed, 36 insertions, 0 deletions
diff --git a/src/gui/math3d/qvector2d.cpp b/src/gui/math3d/qvector2d.cpp index 6aa360fc6b..5710ee273a 100644 --- a/src/gui/math3d/qvector2d.cpp +++ b/src/gui/math3d/qvector2d.cpp @@ -229,6 +229,39 @@ void QVector2D::normalize() } /*! + \since 5.1 + + Returns the distance from this vertex to a point defined by + the vertex \a point. + + \sa distanceToLine() +*/ +float QVector2D::distanceToPoint(const QVector2D& point) const +{ + return (*this - point).length(); +} + +/*! + \since 5.1 + + Returns the distance that this vertex is from a line defined + by \a point and the unit vector \a direction. + + If \a direction is a null vector, then it does not define a line. + In that case, the distance from \a point to this vertex is returned. + + \sa distanceToPoint() +*/ +float QVector2D::distanceToLine + (const QVector2D& point, const QVector2D& direction) const +{ + if (direction.isNull()) + return (*this - point).length(); + QVector2D p = point + dotProduct(*this - point, direction) * direction; + return (*this - p).length(); +} + +/*! \fn QVector2D &QVector2D::operator+=(const QVector2D &vector) Adds the given \a vector to this vector and returns a reference to diff --git a/src/gui/math3d/qvector2d.h b/src/gui/math3d/qvector2d.h index 647e5e36f2..b3c2272287 100644 --- a/src/gui/math3d/qvector2d.h +++ b/src/gui/math3d/qvector2d.h @@ -82,6 +82,9 @@ public: QVector2D normalized() const; void normalize(); + float distanceToPoint(const QVector2D &point) const; + float distanceToLine(const QVector2D& point, const QVector2D& direction) const; + QVector2D &operator+=(const QVector2D &vector); QVector2D &operator-=(const QVector2D &vector); QVector2D &operator*=(float factor); |