Fix QLineF::isNull behavior when handling zero points

QLineF::isNull was using qFuzzyCompare on the x and y components of its
points, which was not working correctly if some of the components was
equal to zero. The correct approach is to use qFuzzyIsNull togeter with
qFuzzyCompare. This approach is already implemented in qFuzzyCompare()
overload for QPointF, so just use it.

Add unit-tests for QLine(F)::isNull.

[ChangeLog][QtCore][QLineF] Fixed a bug when QLineF::isNull() returned
incorrect result if the start or end point contained a zero component.

Change-Id: I3cfe6406b1299de32fe82b1fcbfb0416f0eaac15
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Ivan Solovev 2024-04-05 14:48:09 +02:00
parent 2a847dd93b
commit 7072030d93
2 changed files with 29 additions and 3 deletions

View File

@ -305,7 +305,7 @@ constexpr inline qreal QLineF::y2() const
constexpr inline bool QLineF::isNull() const
{
return qFuzzyCompare(pt1.x(), pt2.x()) && qFuzzyCompare(pt1.y(), pt2.y());
return qFuzzyCompare(pt1, pt2);
}
constexpr inline QPointF QLineF::p1() const

View File

@ -16,6 +16,9 @@ private slots:
void testComparison_data();
void testComparison();
void testIsNull_data();
void testIsNull();
void testIntersection();
void testIntersection_data();
@ -46,6 +49,7 @@ private slots:
};
const qreal epsilon = sizeof(qreal) == sizeof(double) ? 1e-8 : 1e-4;
constexpr static qreal qreal_min = std::numeric_limits<qreal>::min();
void tst_QLine::testComparisonCompiles()
{
@ -81,8 +85,6 @@ void tst_QLine::testComparison_data()
<< result << floatResult << mixedResult;
};
constexpr static qreal qreal_min = std::numeric_limits<qreal>::min();
row(-1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, true, true, true);
row(-1.1, -0.9, 1.1, 0.9, -1.0, -1.0, 1.0, 1.0, true, false, false);
row(-1.0, -1.0, 1.0, 1.0, -0.9, -1.1, 0.9, 1.1, true, false, true);
@ -114,6 +116,30 @@ void tst_QLine::testComparison()
QT_TEST_EQUALITY_OPS(l1f, l2, mixedResult);
}
void tst_QLine::testIsNull_data()
{
QTest::addColumn<QLineF>("lineF");
QTest::addColumn<bool>("result");
QTest::addColumn<bool>("floatResult");
QTest::newRow("non-null") << QLineF(1.0, 1.0, 2.0, 2.0) << false << false;
QTest::newRow("null") << QLineF(1.0, 1.0, 1.0, 1.0) << true << true;
QTest::newRow("null_int_non-null_float") << QLineF(1.0, 1.0, 1.1, 1.1) << true << false;
QTest::newRow("with_qreal_min") << QLineF(-qreal_min, qreal_min, 0.0, 0.0) << true << true;
}
void tst_QLine::testIsNull()
{
QFETCH(QLineF, lineF);
QFETCH(bool, result);
QFETCH(bool, floatResult);
const QLine line = lineF.toLine();
QCOMPARE_EQ(line.isNull(), result);
QCOMPARE_EQ(lineF.isNull(), floatResult);
}
void tst_QLine::testSet()
{
{