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 <thiago.macieira@intel.com>
This commit is contained in:
Ivan Solovev 2024-04-05 14:52:27 +02:00
parent 7072030d93
commit 9bedf8a53c
3 changed files with 52 additions and 0 deletions

View File

@ -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)
{

View File

@ -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);

View File

@ -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<QLineF>("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()