The user must select points in a clockwise direction

--HG--
branch : feature
This commit is contained in:
Valentina Zhuravska 2015-11-22 01:52:18 +02:00
parent e58265b728
commit e8deb4b29c
5 changed files with 73 additions and 23 deletions

View file

@ -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<qreal> x, QVector<qreal> 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;
}

View file

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

View file

@ -289,7 +289,6 @@ qint64 VLayoutDetail::Square() const
}
const int n = d->layoutAllowence.count();
qreal s, res = 0;
qint64 sq = 0;
QVector<qreal> 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;
}

View file

@ -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<VNodeDetail>(item->data(Qt::UserRole)));
}
QVector<QPointF> points = detail.ContourPoints(data);
QVector<qreal> x;
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)
{
return true;
}
return false;
}

View file

@ -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);