From 9bedf8a53c8127effb7ab6f331986b967d9c1254 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Fri, 5 Apr 2024 14:52:27 +0200 Subject: [PATCH] QLineF: add qFuzzyCompare and qFuzzyIsNull overloads [ChangeLog][QtCore][QLineF] Added qFuzzyCompare and qFuzzyIsNull overloads for QLineF Task-number: QTBUG-120308 Change-Id: I3d032f47851d886adce95ac72109fde169892688 Reviewed-by: Thiago Macieira --- src/corelib/tools/qline.cpp | 19 ++++++++++++++ src/corelib/tools/qline.h | 6 +++++ tests/auto/corelib/tools/qline/tst_qline.cpp | 27 ++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/src/corelib/tools/qline.cpp b/src/corelib/tools/qline.cpp index a1794f98f69..e313b06aa92 100644 --- a/src/corelib/tools/qline.cpp +++ b/src/corelib/tools/qline.cpp @@ -787,6 +787,25 @@ qreal QLineF::angleTo(const QLineF &l) const return delta_normalized; } +/*! + \fn bool QLineF::qFuzzyCompare(const QLineF &lhs, const QLineF &rhs) + \since 6.8 + + Returns \c true if line \a lhs is approximately equal to line \a rhs; + otherwise returns \c false. + + The lines are considered approximately equal if their start and end + points are approximately equal. +*/ + +/*! + \fn bool QLineF::qFuzzyIsNull(const QLineF &line) + \since 6.8 + + Returns \c true if the start point of line \a line is approximately + equal to its end point; otherwise returns \c false. +*/ + #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QLineF &p) { diff --git a/src/corelib/tools/qline.h b/src/corelib/tools/qline.h index 1d43b7e705d..03dac30e162 100644 --- a/src/corelib/tools/qline.h +++ b/src/corelib/tools/qline.h @@ -261,6 +261,12 @@ private: { return comparesEqual(lhs, rhs.toLineF()); } Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QLineF, QLine) + friend constexpr bool qFuzzyCompare(const QLineF &lhs, const QLineF &rhs) noexcept + { return qFuzzyCompare(lhs.pt1, rhs.pt1) && qFuzzyCompare(lhs.pt2, rhs.pt2); } + + friend constexpr bool qFuzzyIsNull(const QLineF &line) noexcept + { return qFuzzyCompare(line.pt1, line.pt2); } + QPointF pt1, pt2; }; Q_DECLARE_TYPEINFO(QLineF, Q_PRIMITIVE_TYPE); diff --git a/tests/auto/corelib/tools/qline/tst_qline.cpp b/tests/auto/corelib/tools/qline/tst_qline.cpp index 3954cf53267..10069e821ba 100644 --- a/tests/auto/corelib/tools/qline/tst_qline.cpp +++ b/tests/auto/corelib/tools/qline/tst_qline.cpp @@ -16,6 +16,9 @@ private slots: void testComparison_data(); void testComparison(); + void testFuzzyCompare_data(); + void testFuzzyCompare(); + void testIsNull_data(); void testIsNull(); @@ -116,6 +119,29 @@ void tst_QLine::testComparison() QT_TEST_EQUALITY_OPS(l1f, l2, mixedResult); } +void tst_QLine::testFuzzyCompare_data() +{ + testComparison_data(); +} + +void tst_QLine::testFuzzyCompare() +{ + QFETCH(double, xa1); + QFETCH(double, ya1); + QFETCH(double, xa2); + QFETCH(double, ya2); + QFETCH(double, xb1); + QFETCH(double, yb1); + QFETCH(double, xb2); + QFETCH(double, yb2); + QFETCH(bool, floatResult); + + const QLineF l1f(xa1, ya1, xa2, ya2); + const QLineF l2f(xb1, yb1, xb2, yb2); + + QCOMPARE_EQ(qFuzzyCompare(l1f, l2f), floatResult); +} + void tst_QLine::testIsNull_data() { QTest::addColumn("lineF"); @@ -138,6 +164,7 @@ void tst_QLine::testIsNull() QCOMPARE_EQ(line.isNull(), result); QCOMPARE_EQ(lineF.isNull(), floatResult); + QCOMPARE_EQ(qFuzzyIsNull(lineF), floatResult); } void tst_QLine::testSet()