From d679187f1594b546bf34321bc29e9801e4ea35c5 Mon Sep 17 00:00:00 2001 From: Valentina Zhuravska Date: Sun, 22 Nov 2015 15:58:31 +0200 Subject: [PATCH] Fixed SumTrapezoids method --HG-- branch : feature --- src/libs/vlayout/vabstractdetail.cpp | 28 +++++++++++-------- src/libs/vlayout/vabstractdetail.h | 2 +- src/libs/vlayout/vlayoutdetail.cpp | 17 ++--------- .../vtools/dialogs/tools/dialogdetail.cpp | 25 ++++++----------- 4 files changed, 28 insertions(+), 44 deletions(-) diff --git a/src/libs/vlayout/vabstractdetail.cpp b/src/libs/vlayout/vabstractdetail.cpp index 0483d6a6b..dc13d85ee 100644 --- a/src/libs/vlayout/vabstractdetail.cpp +++ b/src/libs/vlayout/vabstractdetail.cpp @@ -508,29 +508,33 @@ QPointF VAbstractDetail::SingleParallelPoint(const QLineF &line, const qreal &an } //--------------------------------------------------------------------------------------------------------------------- -qreal VAbstractDetail::SumTrapezoids(int n, QVector x, QVector y) +qreal VAbstractDetail::SumTrapezoids(const QVector &points) { // Calculation a polygon area through the sum of the areas of trapezoids qreal s, res = 0; + const int n = points.size(); - for (int i = 0; i < n; ++i) + if(n > 2) { - if (i == 0) + for (int i = 0; i < n; ++i) { - 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) + if (i == 0) { - s = x.at(i)*(y.at(i-1) - y.at(0)); // if i == n-1, then y[i+1] replace on y[0] + s = points.at(i).x()*(points.at(n-1).y() - points.at(i+1).y()); //if i == 0, then y[i-1] replace on y[n-1] res += s; } else { - s = x.at(i)*(y.at(i-1) - y.at(i+1)); - res += s; + if (i == n-1) + { + s = points.at(i).x()*(points.at(i-1).y() - points.at(0).y()); // if i == n-1, then y[i+1] replace on y[0] + res += s; + } + else + { + s = points.at(i).x()*(points.at(i-1).y() - points.at(i+1).y()); + res += s; + } } } } diff --git a/src/libs/vlayout/vabstractdetail.h b/src/libs/vlayout/vabstractdetail.h index 67c147dc5..f8aad520a 100644 --- a/src/libs/vlayout/vabstractdetail.h +++ b/src/libs/vlayout/vabstractdetail.h @@ -65,7 +65,7 @@ public: void setWidth(const qreal &value); static QVector Equidistant(const QVector &points, const EquidistantType &eqv, qreal width); - static qreal SumTrapezoids(int n, QVector x, QVector y); + static qreal SumTrapezoids(const QVector &points); protected: diff --git a/src/libs/vlayout/vlayoutdetail.cpp b/src/libs/vlayout/vlayoutdetail.cpp index 2b55a3c42..e04ca02c9 100644 --- a/src/libs/vlayout/vlayoutdetail.cpp +++ b/src/libs/vlayout/vlayoutdetail.cpp @@ -288,21 +288,10 @@ qint64 VLayoutDetail::Square() const return 0; } - const int n = d->layoutAllowence.count(); - qint64 sq = 0; + const QVector points = d->layoutAllowence; + const qreal res = SumTrapezoids(points); - QVector x; - QVector y; - - for (int i=0; i < n; ++i) - { - x.append(d->layoutAllowence.at(i).x()); - y.append(d->layoutAllowence.at(i).y()); - } - - qreal res = this->SumTrapezoids(n, x, y); - - sq = qFloor(qAbs(res/2.0)); + const qint64 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 6e485fd44..939da6c5e 100644 --- a/src/libs/vtools/dialogs/tools/dialogdetail.cpp +++ b/src/libs/vtools/dialogs/tools/dialogdetail.cpp @@ -463,11 +463,11 @@ bool DialogDetail::DetailIsValid() const QByteArray byteArray; QBuffer buffer(&byteArray); pixmap.save(&buffer, "PNG"); - QString url = QString(""; + QString url = QString("" + QString(" "); if (ui.listWidget->count() < 3) { - url += QString(" ") + tr("You need more points!"); + url += tr("You need more points!"); ui.helpLabel->setText(url); return false; } @@ -475,13 +475,13 @@ bool DialogDetail::DetailIsValid() const { if(not DetailIsClockwise()) { - url += QString(" ") +tr("You have to choose points in a clockwise direction!"); + url += 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!"); + url += tr("First point can not equal the last point!"); ui.helpLabel->setText(url); return false; } @@ -494,7 +494,7 @@ bool DialogDetail::DetailIsValid() const if (QString::compare(previousRow, nextRow) == 0) { - url += QString(" ") +tr("You have double points!"); + url += tr("You have double points!"); ui.helpLabel->setText(url); return false; } @@ -528,28 +528,19 @@ bool DialogDetail::FirstPointEqualLast() const //--------------------------------------------------------------------------------------------------------------------- bool DialogDetail::DetailIsClockwise() const { - VDetail detail; if(ui.listWidget->count() < 3) { return true; } + VDetail detail; 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); + const 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); + const qreal res = VDetail::SumTrapezoids(points); if (res < 0) { return true;