From 79891d8284736dc318cff8d9c3f6d7a6e87affd9 Mon Sep 17 00:00:00 2001 From: Gunnar Sletta Date: Wed, 15 Apr 2009 12:01:36 +0200 Subject: Introduce QPointF::manhattanLength Task-number: 251068 --- src/corelib/tools/qpoint.cpp | 13 +++++++++++++ src/corelib/tools/qpoint.h | 2 ++ tests/auto/qpoint/tst_qpoint.cpp | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/src/corelib/tools/qpoint.cpp b/src/corelib/tools/qpoint.cpp index feea473..5cc71d6 100644 --- a/src/corelib/tools/qpoint.cpp +++ b/src/corelib/tools/qpoint.cpp @@ -442,6 +442,19 @@ QDebug operator<<(QDebug d, const QPointF &p) otherwise returns false. */ + +/*! + Returns the sum of the absolute values of x() and y(), + traditionally known as the "Manhattan length" of the vector from + the origin to the point. + + \sa QPoint::manhattanLength() +*/ +qreal QPointF::manhattanLength() const +{ + return qAbs(x())+qAbs(y()); +} + /*! \fn qreal QPointF::x() const diff --git a/src/corelib/tools/qpoint.h b/src/corelib/tools/qpoint.h index 1dab7e2..e716bf7 100644 --- a/src/corelib/tools/qpoint.h +++ b/src/corelib/tools/qpoint.h @@ -192,6 +192,8 @@ public: QPointF(const QPoint &p); QPointF(qreal xpos, qreal ypos); + qreal manhattanLength() const; + bool isNull() const; qreal x() const; diff --git a/tests/auto/qpoint/tst_qpoint.cpp b/tests/auto/qpoint/tst_qpoint.cpp index 67fefa8..16d55fc 100644 --- a/tests/auto/qpoint/tst_qpoint.cpp +++ b/tests/auto/qpoint/tst_qpoint.cpp @@ -60,6 +60,8 @@ public: private slots: void getSetCheck(); void division(); + + void manhattanLength(); }; tst_QPoint::tst_QPoint() @@ -70,6 +72,24 @@ tst_QPoint::~tst_QPoint() { } + + +void tst_QPoint::manhattanLength() +{ + { + QPoint p(10, 20); + QCOMPARE(p.manhattanLength(), 30); + } + { + QPointF p(10., 20.); + QCOMPARE(p.manhattanLength(), 30.); + } + { + QPointF p(10.1, 20.2); + QCOMPARE(p.manhattanLength(), 30.3); + } +} + // Testing get/set functions void tst_QPoint::getSetCheck() { -- cgit v0.12