From e8deb4b29c08dbeb48be558b9321fbf0b620fc35 Mon Sep 17 00:00:00 2001 From: Valentina Zhuravska Date: Sun, 22 Nov 2015 01:52:18 +0200 Subject: [PATCH] The user must select points in a clockwise direction --HG-- branch : feature --- src/libs/vlayout/vabstractdetail.cpp | 30 +++++++++++++++ src/libs/vlayout/vabstractdetail.h | 2 + src/libs/vlayout/vlayoutdetail.cpp | 25 +----------- .../vtools/dialogs/tools/dialogdetail.cpp | 38 +++++++++++++++++++ src/libs/vtools/dialogs/tools/dialogdetail.h | 1 + 5 files changed, 73 insertions(+), 23 deletions(-) diff --git a/src/libs/vlayout/vabstractdetail.cpp b/src/libs/vlayout/vabstractdetail.cpp index a3aad5ab1..a8bf32f66 100644 --- a/src/libs/vlayout/vabstractdetail.cpp +++ b/src/libs/vlayout/vabstractdetail.cpp @@ -506,3 +506,33 @@ QPointF VAbstractDetail::SingleParallelPoint(const QLineF &line, const qreal &an pLine.setLength( width ); return pLine.p2(); } + +//--------------------------------------------------------------------------------------------------------------------- +qreal VAbstractDetail::SumTrapezoids(int n, QVector x, QVector y) const +{ + // Calculation a polygon area through the sum of the areas of trapezoids + qreal s, res = 0; + + for (int i = 0; i < n; ++i) + { + if (i == 0) + { + s = x.at(i)*(y.at(n-1) - y.at(i+1)); //if i == 0, then y[i-1] replace on y[n-1] + res += s; + } + else + { + if (i == n-1) + { + s = x.at(i)*(y.at(i-1) - y.at(0)); // if i == n-1, then y[i+1] replace on y[0] + res += s; + } + else + { + s = x.at(i)*(y.at(i-1) - y.at(i+1)); + res += s; + } + } + } + return res; +} diff --git a/src/libs/vlayout/vabstractdetail.h b/src/libs/vlayout/vabstractdetail.h index 94168f7c2..10ce5594e 100644 --- a/src/libs/vlayout/vabstractdetail.h +++ b/src/libs/vlayout/vabstractdetail.h @@ -65,6 +65,8 @@ public: void setWidth(const qreal &value); static QVector Equidistant(const QVector &points, const EquidistantType &eqv, qreal width); + qreal SumTrapezoids(int n, QVector x, QVector y) const; + protected: static QVector RemoveDublicates(const QVector &points); diff --git a/src/libs/vlayout/vlayoutdetail.cpp b/src/libs/vlayout/vlayoutdetail.cpp index 21fef345c..2b55a3c42 100644 --- a/src/libs/vlayout/vlayoutdetail.cpp +++ b/src/libs/vlayout/vlayoutdetail.cpp @@ -289,7 +289,6 @@ qint64 VLayoutDetail::Square() const } const int n = d->layoutAllowence.count(); - qreal s, res = 0; qint64 sq = 0; QVector x; @@ -301,28 +300,8 @@ qint64 VLayoutDetail::Square() const y.append(d->layoutAllowence.at(i).y()); } - // Calculation a polygon area through the sum of the areas of trapezoids - for (int i = 0; i < n; ++i) - { - if (i == 0) - { - s = x.at(i)*(y.at(n-1) - y.at(i+1)); //if i == 0, then y[i-1] replace on y[n-1] - res += s; - } - else - { - if (i == n-1) - { - s = x.at(i)*(y.at(i-1) - y.at(0)); // if i == n-1, then y[i+1] replace on y[0] - res += s; - } - else - { - s = x.at(i)*(y.at(i-1) - y.at(i+1)); - res += s; - } - } - } + qreal res = this->SumTrapezoids(n, x, y); + sq = qFloor(qAbs(res/2.0)); return sq; } diff --git a/src/libs/vtools/dialogs/tools/dialogdetail.cpp b/src/libs/vtools/dialogs/tools/dialogdetail.cpp index f0617c79b..6e485fd44 100644 --- a/src/libs/vtools/dialogs/tools/dialogdetail.cpp +++ b/src/libs/vtools/dialogs/tools/dialogdetail.cpp @@ -473,6 +473,12 @@ bool DialogDetail::DetailIsValid() const } else { + if(not DetailIsClockwise()) + { + url += QString(" ") +tr("You have to choose points in a clockwise direction!"); + ui.helpLabel->setText(url); + return false; + } if (FirstPointEqualLast()) { url += QString(" ") +tr("First point can not equal the last point!"); @@ -518,3 +524,35 @@ bool DialogDetail::FirstPointEqualLast() const } return false; } + +//--------------------------------------------------------------------------------------------------------------------- +bool DialogDetail::DetailIsClockwise() const +{ + VDetail detail; + if(ui.listWidget->count() < 3) + { + return true; + } + for (qint32 i = 0; i < ui.listWidget->count(); ++i) + { + QListWidgetItem *item = ui.listWidget->item(i); + detail.append( qvariant_cast(item->data(Qt::UserRole))); + } + QVector points = detail.ContourPoints(data); + + QVector x; + QVector y; + const int n = points.size(); + for (int i=0; i < n; ++i) + { + x.append(points.at(i).x()); + y.append(points.at(i).y()); + } + + qreal res = detail.SumTrapezoids(n, x, y); + if (res < 0) + { + return true; + } + return false; +} diff --git a/src/libs/vtools/dialogs/tools/dialogdetail.h b/src/libs/vtools/dialogs/tools/dialogdetail.h index 9a8ec4314..6dcd5c43e 100644 --- a/src/libs/vtools/dialogs/tools/dialogdetail.h +++ b/src/libs/vtools/dialogs/tools/dialogdetail.h @@ -78,6 +78,7 @@ private: bool flagWidth; bool DetailIsValid() const; bool FirstPointEqualLast() const; + bool DetailIsClockwise() const; void NewItem(quint32 id, const Tool &typeTool, const NodeDetail &typeNode, qreal mx = 0, qreal my = 0, bool reverse = false);