From 800181e8674a968b5d2c7f0cdaaac5758ef5e586 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 10 Sep 2016 18:18:11 +0300 Subject: [PATCH] Flipping VArc. --HG-- branch : feature --- src/libs/vgeometry/varc.cpp | 14 ++++++ src/libs/vgeometry/varc.h | 1 + src/libs/vgeometry/vpointf.cpp | 10 +++- src/libs/vgeometry/vpointf.h | 1 + src/test/ValentinaTest/tst_varc.cpp | 71 +++++++++++++++++++++++++++++ src/test/ValentinaTest/tst_varc.h | 2 + 6 files changed, 97 insertions(+), 2 deletions(-) diff --git a/src/libs/vgeometry/varc.cpp b/src/libs/vgeometry/varc.cpp index dd424fe66..8616f7079 100644 --- a/src/libs/vgeometry/varc.cpp +++ b/src/libs/vgeometry/varc.cpp @@ -131,6 +131,20 @@ VArc VArc::Rotate(const QPointF &originPoint, qreal degrees, const QString &pref return arc; } +//--------------------------------------------------------------------------------------------------------------------- +VArc VArc::Flip(const QLineF &axis, const QString &prefix) const +{ + const VPointF center = GetCenter().Flip(axis); + + const QPointF p1 = VPointF::FlipPF(axis, GetP1()); + const QPointF p2 = VPointF::FlipPF(axis, GetP2()); + + VArc arc(center, GetRadius(), QLineF(center, p1).angle(), QLineF(center, p2).angle()); + arc.setName(name() + prefix); + arc.SetFlipped(true); + return arc; +} + //--------------------------------------------------------------------------------------------------------------------- VArc::~VArc() {} diff --git a/src/libs/vgeometry/varc.h b/src/libs/vgeometry/varc.h index 67caef6e4..3a3c4d0c7 100644 --- a/src/libs/vgeometry/varc.h +++ b/src/libs/vgeometry/varc.h @@ -63,6 +63,7 @@ public: VArc(const VArc &arc); VArc& operator= (const VArc &arc); VArc Rotate(const QPointF &originPoint, qreal degrees, const QString &prefix = QString()) const; + VArc Flip(const QLineF &axis, const QString &prefix = QString()) const; virtual ~VArc() Q_DECL_OVERRIDE; QString GetFormulaRadius () const; diff --git a/src/libs/vgeometry/vpointf.cpp b/src/libs/vgeometry/vpointf.cpp index d6c3ab42c..fcb3f529d 100644 --- a/src/libs/vgeometry/vpointf.cpp +++ b/src/libs/vgeometry/vpointf.cpp @@ -117,8 +117,7 @@ VPointF VPointF::Rotate(const QPointF &originPoint, qreal degrees, const QString //--------------------------------------------------------------------------------------------------------------------- VPointF VPointF::Flip(const QLineF &axis, const QString &prefix) const { - const QTransform matrix = FlippingMatrix(axis); - const QPointF p = matrix.map(toQPointF()); + const QPointF p = FlipPF(axis, toQPointF()); return VPointF(p, name() + prefix, mx(), my()); } @@ -215,3 +214,10 @@ QPointF VPointF::RotatePF(const QPointF &originPoint, const QPointF &point, qrea axis.setAngle(axis.angle() + degrees); return axis.p2(); } + +//--------------------------------------------------------------------------------------------------------------------- +QPointF VPointF::FlipPF(const QLineF &axis, const QPointF &point) +{ + const QTransform matrix = FlippingMatrix(axis); + return matrix.map(point); +} diff --git a/src/libs/vgeometry/vpointf.h b/src/libs/vgeometry/vpointf.h index 8bd7e1eca..3eb4a25ee 100644 --- a/src/libs/vgeometry/vpointf.h +++ b/src/libs/vgeometry/vpointf.h @@ -77,6 +77,7 @@ public: void setY(const qreal &value); static QPointF RotatePF(const QPointF &originPoint, const QPointF &point, qreal degrees); + static QPointF FlipPF(const QLineF &axis, const QPointF &point); private: QSharedDataPointer d; }; diff --git a/src/test/ValentinaTest/tst_varc.cpp b/src/test/ValentinaTest/tst_varc.cpp index ba2fbfb4c..207050c34 100644 --- a/src/test/ValentinaTest/tst_varc.cpp +++ b/src/test/ValentinaTest/tst_varc.cpp @@ -246,3 +246,74 @@ void TST_VArc::TestRotation() const QString errorMsg = QString("The name doesn't contain the prefix '%1'.").arg(prefix); QVERIFY2(rotatedArc.name().endsWith(prefix), qUtf8Printable(errorMsg)); } + +//--------------------------------------------------------------------------------------------------------------------- +void TST_VArc::TestFlip_data() +{ + QTest::addColumn("center"); + QTest::addColumn("radius"); + QTest::addColumn("startAngle"); + QTest::addColumn("endAngle"); + QTest::addColumn("axis"); + QTest::addColumn("prefix"); + + const qreal radius = 5; + QPointF center(10, 5); + + QPointF p1(10, 0); + QPointF p2(5, 5); + + QLineF axis(QPointF(4, 6), QPointF(12, 6)); + + QTest::newRow("Vertical axis") << center << radius << QLineF(center, p1).angle() << QLineF(center, p2).angle() + << axis << "a2"; + + p1 = QPointF(15, 5); + p2 = QPointF(10, 0); + + axis = QLineF(QPointF(9, -1), QPointF(9, 6)); + + QTest::newRow("Horizontal axis") << center << radius << QLineF(center, p1).angle() << QLineF(center, p2).angle() + << axis << "a2"; + + QLineF l(center.x(), center.y(), center.x() + radius, center.y()); + + l.setAngle(45); + p2 = l.p2(); + + l.setAngle(225); + p1 = l.p2(); + + l.setAngle(45+90); + l.setLength(5); + + const QPointF p1Axis = l.p2(); + axis = QLineF(p1Axis.x(), p1Axis.y(), p1Axis.x() + radius, p1Axis.y()); + axis.setAngle(45); + + QTest::newRow("Diagonal axis") << center << radius << QLineF(center, p1).angle() << QLineF(center, p2).angle() + << axis << "a2"; +} + +//--------------------------------------------------------------------------------------------------------------------- +void TST_VArc::TestFlip() +{ + QFETCH(QPointF, center); + QFETCH(qreal, radius); + QFETCH(qreal, startAngle); + QFETCH(qreal, endAngle); + QFETCH(QLineF, axis); + QFETCH(QString, prefix); + + VArc originArc(center, radius, startAngle, endAngle); + const VArc res = originArc.Flip(axis, prefix); + + const QString errorMsg = QString("The name doesn't contain the prefix '%1'.").arg(prefix); + QVERIFY2(res.name().endsWith(prefix), qUtf8Printable(errorMsg)); + + QVERIFY2(res.IsFlipped(), qUtf8Printable("The arc is not flipped")); + + QCOMPARE(originArc.GetLength()*-1, res.GetLength()); + QCOMPARE(originArc.GetRadius(), res.GetRadius()); + QCOMPARE(originArc.AngleArc(), res.AngleArc()); +} diff --git a/src/test/ValentinaTest/tst_varc.h b/src/test/ValentinaTest/tst_varc.h index 770c2a7a7..74f62f2ed 100644 --- a/src/test/ValentinaTest/tst_varc.h +++ b/src/test/ValentinaTest/tst_varc.h @@ -44,6 +44,8 @@ private slots: void TestGetPoints(); void TestRotation_data(); void TestRotation(); + void TestFlip_data(); + void TestFlip(); }; #endif // TST_VARC_H