diff --git a/src/corelib/tools/qpoint.cpp b/src/corelib/tools/qpoint.cpp index ce7514cddd..775a354469 100644 --- a/src/corelib/tools/qpoint.cpp +++ b/src/corelib/tools/qpoint.cpp @@ -762,6 +762,26 @@ size_t qHash(QPoint key, size_t seed) noexcept \sa qFuzzyCompare */ +/*! + \fn bool QPointF::qFuzzyCompare(const QPointF &p1, const QPointF &p2) + \since 6.8 + + Returns \c true if \a p1 is approximately equal to \a p2; otherwise + returns \c false. + + \sa qFuzzyIsNull +*/ + +/*! + \fn bool QPointF::qFuzzyIsNull(const QPointF &point) + \since 6.8 + + Returns \c true if \a point is approximately equal to a point + \c {(0.0, 0.0)}. + + \sa qFuzzyCompare +*/ + #ifndef QT_NO_DATASTREAM /*! \fn QDataStream &operator<<(QDataStream &stream, const QPointF &point) diff --git a/src/corelib/tools/qpoint.h b/src/corelib/tools/qpoint.h index 2960033c6d..50b4c864be 100644 --- a/src/corelib/tools/qpoint.h +++ b/src/corelib/tools/qpoint.h @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -246,12 +247,18 @@ public: private: QT_WARNING_PUSH QT_WARNING_DISABLE_FLOAT_COMPARE - friend constexpr bool comparesEqual(const QPointF &p1, const QPointF &p2) noexcept + friend constexpr bool qFuzzyCompare(const QPointF &p1, const QPointF &p2) noexcept { return ((!p1.xp || !p2.xp) ? qFuzzyIsNull(p1.xp - p2.xp) : qFuzzyCompare(p1.xp, p2.xp)) && ((!p1.yp || !p2.yp) ? qFuzzyIsNull(p1.yp - p2.yp) : qFuzzyCompare(p1.yp, p2.yp)); } QT_WARNING_POP + friend constexpr bool qFuzzyIsNull(const QPointF &point) noexcept + { + return qFuzzyIsNull(point.xp) && qFuzzyIsNull(point.yp); + } + friend constexpr bool comparesEqual(const QPointF &p1, const QPointF &p2) noexcept + { return qFuzzyCompare(p1, p2); } Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QPointF) friend constexpr bool comparesEqual(const QPointF &p1, const QPoint &p2) noexcept { return comparesEqual(p1, p2.toPointF()); } diff --git a/tests/auto/corelib/tools/qpointf/tst_qpointf.cpp b/tests/auto/corelib/tools/qpointf/tst_qpointf.cpp index e75dd9d5ab..ebbac4ec7c 100644 --- a/tests/auto/corelib/tools/qpointf/tst_qpointf.cpp +++ b/tests/auto/corelib/tools/qpointf/tst_qpointf.cpp @@ -76,6 +76,9 @@ private slots: void operator_eq_data(); void operator_eq(); + void fuzzyCompare_data(); + void fuzzyCompare(); + void toPoint_data(); void toPoint(); @@ -103,15 +106,19 @@ void tst_QPointF::isNull() { QPointF point(0, 0); QVERIFY(point.isNull()); + QVERIFY(qFuzzyIsNull(point)); ++point.rx(); QVERIFY(!point.isNull()); + QVERIFY(!qFuzzyIsNull(point)); point.rx() -= 2; QVERIFY(!point.isNull()); + QVERIFY(!qFuzzyIsNull(point)); QPointF nullNegativeZero(qreal(-0.0), qreal(-0.0)); QCOMPARE(nullNegativeZero.x(), (qreal)-0.0f); QCOMPARE(nullNegativeZero.y(), (qreal)-0.0f); QVERIFY(nullNegativeZero.isNull()); + QVERIFY(qFuzzyIsNull(nullNegativeZero)); } void tst_QPointF::manhattanLength_data() @@ -385,6 +392,20 @@ void tst_QPointF::operator_eq() QT_TEST_EQUALITY_OPS(point1, intPoint2, expectIntEqual); } +void tst_QPointF::fuzzyCompare_data() +{ + operator_eq_data(); +} + +void tst_QPointF::fuzzyCompare() +{ + QFETCH(QPointF, point1); + QFETCH(QPointF, point2); + QFETCH(bool, expectEqual); + + QCOMPARE_EQ(qFuzzyCompare(point1, point2), expectEqual); +} + void tst_QPointF::toPoint_data() { QTest::addColumn("pointf");