| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
|
| 5 |
#include <QTest> |
| 6 |
|
| 7 |
#include <qpolygon.h> |
| 8 |
#include <qpainterpath.h> |
| 9 |
#include <math.h> |
| 10 |
|
| 11 |
#include <qpainter.h> |
| 12 |
|
| 13 |
class tst_QPolygon : public QObject |
| 14 |
{ |
| 15 |
Q_OBJECT |
| 16 |
|
| 17 |
private slots: |
| 18 |
void constructors(); |
| 19 |
void toPolygonF(); |
| 20 |
void boundingRect_data(); |
| 21 |
void boundingRect(); |
| 22 |
void boundingRectF_data(); |
| 23 |
void boundingRectF(); |
| 24 |
void makeEllipse(); |
| 25 |
void swap(); |
| 26 |
void intersections_data(); |
| 27 |
void intersections(); |
| 28 |
}; |
| 29 |
|
| 30 |
void constructors_helper(QPolygon) {} |
| 31 |
void constructors_helperF(QPolygonF) {} |
| 32 |
|
| 33 |
void tst_QPolygon::constructors() |
| 34 |
{ |
| 35 |
constructors_helper(QPolygon()); |
| 36 |
constructors_helper({}); |
| 37 |
constructors_helper({ QPoint(1, 2), QPoint(3, 4)}); |
| 38 |
constructors_helper({ {1, 2}, {3, 4} }); |
| 39 |
constructors_helper(QPolygon(12)); |
| 40 |
QList<QPoint> pointList; |
| 41 |
constructors_helper(pointList); |
| 42 |
constructors_helper(std::move(pointList)); |
| 43 |
constructors_helper(QRect(1, 2, 3, 4)); |
| 44 |
const int points[2] = { 10, 20 }; |
| 45 |
constructors_helper(QPolygon(1, points)); |
| 46 |
|
| 47 |
constructors_helperF(QPolygonF()); |
| 48 |
constructors_helperF({}); |
| 49 |
constructors_helperF({ QPointF(1, 2), QPointF(3, 4)}); |
| 50 |
constructors_helperF({ {1, 2}, {3, 4} }); |
| 51 |
constructors_helperF(QPolygonF(12)); |
| 52 |
constructors_helperF(QPolygon()); |
| 53 |
QList<QPointF> pointFList; |
| 54 |
constructors_helperF(pointFList); |
| 55 |
constructors_helperF(std::move(pointFList)); |
| 56 |
constructors_helperF(QRectF(1, 2, 3, 4)); |
| 57 |
} |
| 58 |
|
| 59 |
void tst_QPolygon::toPolygonF() |
| 60 |
{ |
| 61 |
const QPolygon p = {{1, 1}, {-1, 1}, {-1, -1}, {1, -1}}; |
| 62 |
auto pf = p.toPolygonF(); |
| 63 |
static_assert(std::is_same_v<decltype(pf), QPolygonF>); |
| 64 |
QCOMPARE(pf.size(), p.size()); |
| 65 |
auto p2 = pf.toPolygon(); |
| 66 |
static_assert(std::is_same_v<decltype(p2), QPolygon>); |
| 67 |
QCOMPARE(p, p2); |
| 68 |
} |
| 69 |
|
| 70 |
void tst_QPolygon::boundingRect_data() |
| 71 |
{ |
| 72 |
QTest::addColumn<QPolygon>("poly"); |
| 73 |
QTest::addColumn<QRect>("brect"); |
| 74 |
|
| 75 |
#define ROW(args, rect) \ |
| 76 |
do { \ |
| 77 |
QPolygon poly; \ |
| 78 |
poly.setPoints args; \ |
| 79 |
QTest::newRow(#args) << poly << QRect rect; \ |
| 80 |
} while (0) |
| 81 |
|
| 82 |
QTest::newRow("empty") << QPolygon() << QRect(0, 0, 0, 0); |
| 83 |
ROW((1, 0,1), ( 0, 1, 1, 1)); |
| 84 |
ROW((2, 0,1, 1,0), ( 0, 0, 2, 2)); |
| 85 |
ROW((3, -1,1, -1,-1, 1,0), (-1,-1, 3, 3)); |
| 86 |
#undef ROW |
| 87 |
} |
| 88 |
|
| 89 |
void tst_QPolygon::boundingRect() |
| 90 |
{ |
| 91 |
QFETCH(QPolygon, poly); |
| 92 |
QFETCH(QRect, brect); |
| 93 |
|
| 94 |
QCOMPARE(poly.boundingRect(), brect); |
| 95 |
} |
| 96 |
|
| 97 |
namespace { |
| 98 |
struct MyPolygonF : QPolygonF |
| 99 |
{ |
| 100 |
// QPolygonF doesn't have setPoints... |
| 101 |
void setPoints(int nPoints, int firstx, int firsty, ...) { |
| 102 |
va_list ap; |
| 103 |
reserve(nPoints); |
| 104 |
*this << QPointF(firstx, firsty); |
| 105 |
va_start(ap, firsty); |
| 106 |
while (--nPoints) { |
| 107 |
const int x = va_arg(ap, int); |
| 108 |
const int y = va_arg(ap, int); |
| 109 |
*this << QPointF(x, y); |
| 110 |
} |
| 111 |
va_end(ap); |
| 112 |
} |
| 113 |
}; |
| 114 |
} |
| 115 |
|
| 116 |
void tst_QPolygon::boundingRectF_data() |
| 117 |
{ |
| 118 |
QTest::addColumn<QPolygonF>("poly"); |
| 119 |
QTest::addColumn<QRectF>("brect"); |
| 120 |
|
| 121 |
#define ROW(args, rect) \ |
| 122 |
do { \ |
| 123 |
MyPolygonF poly; \ |
| 124 |
poly.setPoints args; \ |
| 125 |
QTest::newRow(#args) << QPolygonF(poly) << QRectF rect; \ |
| 126 |
} while (0) |
| 127 |
|
| 128 |
QTest::newRow("empty") << QPolygonF() << QRectF(0, 0, 0, 0); |
| 129 |
ROW((1, 0,1), ( 0, 1, 0, 0)); |
| 130 |
ROW((2, 0,1, 1,0), ( 0, 0, 1, 1)); |
| 131 |
ROW((3, -1,1, -1,-1, 1,0), (-1,-1, 2, 2)); |
| 132 |
#undef ROW |
| 133 |
} |
| 134 |
|
| 135 |
void tst_QPolygon::boundingRectF() |
| 136 |
{ |
| 137 |
QFETCH(QPolygonF, poly); |
| 138 |
QFETCH(QRectF, brect); |
| 139 |
|
| 140 |
QCOMPARE(poly.boundingRect(), brect); |
| 141 |
} |
| 142 |
|
| 143 |
void tst_QPolygon::makeEllipse() |
| 144 |
{ |
| 145 |
// create an ellipse with R1 = R2 = R, i.e. a circle |
| 146 |
QPolygon pa; |
| 147 |
const int R = 50; // radius |
| 148 |
QPainterPath path; |
| 149 |
path.addEllipse(0, 0, 2*R, 2*R); |
| 150 |
pa = path.toSubpathPolygons().at(0).toPolygon(); |
| 151 |
|
| 152 |
int i; |
| 153 |
// make sure that all points are R+-1 away from the center |
| 154 |
bool err = false; |
| 155 |
for (i = 1; i < pa.size(); i++) { |
| 156 |
QPoint p = pa.at(i); |
| 157 |
double r = sqrt(pow(double(p.x() - R), 2.0) + pow(double(p.y() - R), 2.0)); |
| 158 |
// ### too strict ? at least from visual inspection it looks |
| 159 |
// quite odd around the main axes. 2.0 passes easily. |
| 160 |
err |= (qAbs(r - double(R)) > 2.0); |
| 161 |
} |
| 162 |
QVERIFY( !err ); |
| 163 |
} |
| 164 |
|
| 165 |
void tst_QPolygon::swap() |
| 166 |
{ |
| 167 |
QPolygon p1(QList<QPoint>() << QPoint(0, 0) << QPoint(10, 10) << QPoint(-10, 10)); |
| 168 |
QPolygon p2(QList<QPoint>() << QPoint(0, 0) << QPoint(0, 10) << QPoint(10, 10) |
| 169 |
<< QPoint(10, 0)); |
| 170 |
p1.swap(p2); |
| 171 |
QCOMPARE(p1.size(),4); |
| 172 |
QCOMPARE(p2.size(),3); |
| 173 |
} |
| 174 |
|
| 175 |
void tst_QPolygon::intersections_data() |
| 176 |
{ |
| 177 |
QTest::addColumn<QPolygon>("poly1"); |
| 178 |
QTest::addColumn<QPolygon>("poly2"); |
| 179 |
QTest::addColumn<bool>("result"); |
| 180 |
|
| 181 |
QTest::newRow("empty intersects nothing") |
| 182 |
<< QPolygon() |
| 183 |
<< QPolygon(QList<QPoint>() << QPoint(0, 0) << QPoint(10, 10) << QPoint(-10, 10)) |
| 184 |
<< false; |
| 185 |
QTest::newRow("identical triangles") |
| 186 |
<< QPolygon(QList<QPoint>() << QPoint(0, 0) << QPoint(10, 10) << QPoint(-10, 10)) |
| 187 |
<< QPolygon(QList<QPoint>() << QPoint(0, 0) << QPoint(10, 10) << QPoint(-10, 10)) |
| 188 |
<< true; |
| 189 |
QTest::newRow("not intersecting") |
| 190 |
<< QPolygon(QList<QPoint>() << QPoint(0, 0) << QPoint(10, 10) << QPoint(-10, 10)) |
| 191 |
<< QPolygon(QList<QPoint>() << QPoint(0, 20) << QPoint(10, 12) << QPoint(-10, 12)) |
| 192 |
<< false; |
| 193 |
QTest::newRow("clean intersection of squares") |
| 194 |
<< QPolygon(QList<QPoint>() |
| 195 |
<< QPoint(0, 0) << QPoint(0, 10) << QPoint(10, 10) << QPoint(10, 0)) |
| 196 |
<< QPolygon(QList<QPoint>() |
| 197 |
<< QPoint(5, 5) << QPoint(5, 15) << QPoint(15, 15) << QPoint(15, 5)) |
| 198 |
<< true; |
| 199 |
QTest::newRow("clean contains of squares") |
| 200 |
<< QPolygon(QList<QPoint>() |
| 201 |
<< QPoint(0, 0) << QPoint(0, 10) << QPoint(10, 10) << QPoint(10, 0)) |
| 202 |
<< QPolygon(QList<QPoint>() |
| 203 |
<< QPoint(5, 5) << QPoint(5, 8) << QPoint(8, 8) << QPoint(8, 5)) |
| 204 |
<< true; |
| 205 |
} |
| 206 |
|
| 207 |
void tst_QPolygon::intersections() |
| 208 |
{ |
| 209 |
QFETCH(QPolygon, poly1); |
| 210 |
QFETCH(QPolygon, poly2); |
| 211 |
QFETCH(bool, result); |
| 212 |
|
| 213 |
QCOMPARE(poly2.intersects(poly1), poly1.intersects(poly2)); |
| 214 |
QCOMPARE(poly2.intersected(poly1).isEmpty(), poly1.intersected(poly2).isEmpty()); |
| 215 |
QCOMPARE(!poly1.intersected(poly2).isEmpty(), poly1.intersects(poly2)); |
| 216 |
QCOMPARE(poly1.intersects(poly2), result); |
| 217 |
} |
| 218 |
|
| 219 |
QTEST_APPLESS_MAIN(tst_QPolygon) |
| 220 |
#include "tst_qpolygon.moc" |