From c25bce109ef4a2a1fa1bfd7ecd6aba871546dbff Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Fri, 12 May 2017 16:50:24 +0200 Subject: Add QPolygon::intersects() methods Corresponds to the similar function QPainterPath::intersects() and is faster than calculating the intersection and then checking if it is empty. Change-Id: I694bb2206ed330a456a41d4118a952a68177b7a2 Reviewed-by: Eirik Aavitsland --- tests/auto/gui/painting/qpolygon/tst_qpolygon.cpp | 42 +++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'tests/auto') diff --git a/tests/auto/gui/painting/qpolygon/tst_qpolygon.cpp b/tests/auto/gui/painting/qpolygon/tst_qpolygon.cpp index 13b6e28f5f..bf3e5dfb52 100644 --- a/tests/auto/gui/painting/qpolygon/tst_qpolygon.cpp +++ b/tests/auto/gui/painting/qpolygon/tst_qpolygon.cpp @@ -49,6 +49,8 @@ private slots: void boundingRectF(); void makeEllipse(); void swap(); + void intersections_data(); + void intersections(); }; tst_QPolygon::tst_QPolygon() @@ -159,5 +161,45 @@ void tst_QPolygon::swap() QCOMPARE(p2.count(),3); } +void tst_QPolygon::intersections_data() +{ + QTest::addColumn("poly1"); + QTest::addColumn("poly2"); + QTest::addColumn("result"); + + QTest::newRow("empty intersects nothing") + << QPolygon() + << QPolygon(QVector() << QPoint(0,0) << QPoint(10,10) << QPoint(-10,10)) + << false; + QTest::newRow("identical triangles") + << QPolygon(QVector() << QPoint(0,0) << QPoint(10,10) << QPoint(-10,10)) + << QPolygon(QVector() << QPoint(0,0) << QPoint(10,10) << QPoint(-10,10)) + << true; + QTest::newRow("not intersecting") + << QPolygon(QVector() << QPoint(0,0) << QPoint(10,10) << QPoint(-10,10)) + << QPolygon(QVector() << QPoint(0,20) << QPoint(10,12) << QPoint(-10,12)) + << false; + QTest::newRow("clean intersection of squares") + << QPolygon(QVector() << QPoint(0,0) << QPoint(0,10) << QPoint(10,10) << QPoint(10,0)) + << QPolygon(QVector() << QPoint(5,5) << QPoint(5,15) << QPoint(15,15) << QPoint(15,5)) + << true; + QTest::newRow("clean contains of squares") + << QPolygon(QVector() << QPoint(0,0) << QPoint(0,10) << QPoint(10,10) << QPoint(10,0)) + << QPolygon(QVector() << QPoint(5,5) << QPoint(5,8) << QPoint(8,8) << QPoint(8,5)) + << true; +} + +void tst_QPolygon::intersections() +{ + QFETCH(QPolygon, poly1); + QFETCH(QPolygon, poly2); + QFETCH(bool, result); + + QCOMPARE(poly2.intersects(poly1), poly1.intersects(poly2)); + QCOMPARE(poly2.intersected(poly1).isEmpty(), poly1.intersected(poly2).isEmpty()); + QCOMPARE(!poly1.intersected(poly2).isEmpty(), poly1.intersects(poly2)); + QCOMPARE(poly1.intersects(poly2), result); +} + QTEST_APPLESS_MAIN(tst_QPolygon) #include "tst_qpolygon.moc" -- cgit v1.2.3