Fixed SumTrapezoids method

--HG--
branch : feature
This commit is contained in:
Valentina Zhuravska 2015-11-22 15:58:31 +02:00
parent 40ef4732ca
commit d679187f15
4 changed files with 28 additions and 44 deletions

View file

@ -508,29 +508,33 @@ QPointF VAbstractDetail::SingleParallelPoint(const QLineF &line, const qreal &an
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
qreal VAbstractDetail::SumTrapezoids(int n, QVector<qreal> x, QVector<qreal> y) qreal VAbstractDetail::SumTrapezoids(const QVector<QPointF> &points)
{ {
// Calculation a polygon area through the sum of the areas of trapezoids // Calculation a polygon area through the sum of the areas of trapezoids
qreal s, res = 0; 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] if (i == 0)
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] 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; res += s;
} }
else else
{ {
s = x.at(i)*(y.at(i-1) - y.at(i+1)); if (i == n-1)
res += s; {
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;
}
} }
} }
} }

View file

@ -65,7 +65,7 @@ public:
void setWidth(const qreal &value); void setWidth(const qreal &value);
static QVector<QPointF> Equidistant(const QVector<QPointF> &points, const EquidistantType &eqv, qreal width); static QVector<QPointF> Equidistant(const QVector<QPointF> &points, const EquidistantType &eqv, qreal width);
static qreal SumTrapezoids(int n, QVector<qreal> x, QVector<qreal> y); static qreal SumTrapezoids(const QVector<QPointF> &points);
protected: protected:

View file

@ -288,21 +288,10 @@ qint64 VLayoutDetail::Square() const
return 0; return 0;
} }
const int n = d->layoutAllowence.count(); const QVector<QPointF> points = d->layoutAllowence;
qint64 sq = 0; const qreal res = SumTrapezoids(points);
QVector<qreal> x; const qint64 sq = qFloor(qAbs(res/2.0));
QVector<qreal> 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));
return sq; return sq;
} }

View file

@ -463,11 +463,11 @@ bool DialogDetail::DetailIsValid() const
QByteArray byteArray; QByteArray byteArray;
QBuffer buffer(&byteArray); QBuffer buffer(&byteArray);
pixmap.save(&buffer, "PNG"); pixmap.save(&buffer, "PNG");
QString url = QString("<img src=\"data:image/png;base64,") + byteArray.toBase64() + "\"/>"; QString url = QString("<img src=\"data:image/png;base64,") + byteArray.toBase64() + "\"/>" + QString(" ");
if (ui.listWidget->count() < 3) if (ui.listWidget->count() < 3)
{ {
url += QString(" ") + tr("You need more points!"); url += tr("You need more points!");
ui.helpLabel->setText(url); ui.helpLabel->setText(url);
return false; return false;
} }
@ -475,13 +475,13 @@ bool DialogDetail::DetailIsValid() const
{ {
if(not DetailIsClockwise()) 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); ui.helpLabel->setText(url);
return false; return false;
} }
if (FirstPointEqualLast()) 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); ui.helpLabel->setText(url);
return false; return false;
} }
@ -494,7 +494,7 @@ bool DialogDetail::DetailIsValid() const
if (QString::compare(previousRow, nextRow) == 0) if (QString::compare(previousRow, nextRow) == 0)
{ {
url += QString(" ") +tr("You have double points!"); url += tr("You have double points!");
ui.helpLabel->setText(url); ui.helpLabel->setText(url);
return false; return false;
} }
@ -528,28 +528,19 @@ bool DialogDetail::FirstPointEqualLast() const
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
bool DialogDetail::DetailIsClockwise() const bool DialogDetail::DetailIsClockwise() const
{ {
VDetail detail;
if(ui.listWidget->count() < 3) if(ui.listWidget->count() < 3)
{ {
return true; return true;
} }
VDetail detail;
for (qint32 i = 0; i < ui.listWidget->count(); ++i) for (qint32 i = 0; i < ui.listWidget->count(); ++i)
{ {
QListWidgetItem *item = ui.listWidget->item(i); QListWidgetItem *item = ui.listWidget->item(i);
detail.append( qvariant_cast<VNodeDetail>(item->data(Qt::UserRole))); detail.append( qvariant_cast<VNodeDetail>(item->data(Qt::UserRole)));
} }
QVector<QPointF> points = detail.ContourPoints(data); const QVector<QPointF> points = detail.ContourPoints(data);
QVector<qreal> x; const qreal res = VDetail::SumTrapezoids(points);
QVector<qreal> 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) if (res < 0)
{ {
return true; return true;