QT Doubt graphing path of points

2

I receive positions in the plane and I want to draw the route. It is not a line, but a path ... that is, I receive every point and I generate a polygon that has a "width of work" of 7, so every point I receive, I generate a polygon of 7 wide and I join them . This is the code I generated:

void WorkedPathGraph::addPointToDraw(QPointF point)
{
    if (points.count() == 0)
        points.append(point);
    else
    {
        QPointF lastIns = points.last();
        QLineF aux1;
        aux1.setP1(lastIns);
        aux1.setP2(point);
        aux1.setLength(_anchoLabor / 2);
        aux1 = aux1.normalVector();

        QLineF lpt1;
        lpt1.setP1(aux1.p2());
        lpt1.setP2(aux1.p1());
        lpt1.setLength(_anchoLabor);

        QLineF aux2;
        aux2.setP1(point);
        aux2.setP2(lastIns);
        aux2.setLength(_anchoLabor / 2);
        aux2 = aux2.normalVector();

        QLineF lpt2;
        lpt2.setP1(aux2.p2());
        lpt2.setP2(aux2.p1());
        lpt2.setLength(_anchoLabor);

        QPolygonF newPol = QPolygonF(QVector<QPointF>() << lpt1.p1() << 
                 lpt1.p2() << lpt2.p1() << lpt2.p2());

        areas.append(newPol);
        path.addPolygon(newPol);

        points.append(point);
    }
    this->parentItem()->update();
}

What I do there is take the point I receive and help me with the previous one so that if it moved to one side, I take the perpendicular to the line that generates that point with the previous one to generate the polygon. ALL those polygons, I insert them in a QPainterPath and I draw them ... BUT the graph is cut out ... I need to be neat and the curves are more CURVED. I leave an image of how it is to see if someone can guide me to improve it.

  • areas is a QVector<QPolygonF> , which I use to accumulate the polygons and add the area that is covering the machine.
  • points is a QVector<QPointF> , which I accumulate the points I receive to generate the polygons between the point I receive and the last one that is stored in points .
  • path is a QPainterPath that there you throw all polygons to paint.

In the PAINT method what I do is:

void WorkedPathGraph::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
{
    Q_UNUSED(widget);

    QPen qpen, qpenOld;
    QBrush oldBrush;
    qpen.setColor(QColor(0, 255, 0, 128));

    qpen.setWidth(1);
    qpen.setCosmetic(true);

    qpenOld = painter->pen();
    oldBrush = painter->brush();
    painter->setPen(qpen);

    painter->setClipRect(item->exposedRect);
    painter->setBrush(QColor(0,255,0, 128));

    painter->drawPath(path.simplified());
    painter->setPen(qpenOld);
   painter->setBrush(oldBrush);
}

The idea is to keep something in the same style (It does not have the same shape because I made it in paint):

    
asked by Emiliano Torres 03.05.2018 в 14:13
source

1 answer

2

The polygons are independent of each other. If you want the line to be continuous you have to draw the route point by point:

void WorkedPathGraph::addPointToDraw(QPointF point)
{
    if (points.count() == 0)
    {
        points.append(point);
        path.moveTo(point); // <<---
    }
    else
    {
        QPointF lastIns = points.last();
        QLineF aux1;
        aux1.setP1(lastIns);
        aux1.setP2(point);
        aux1.setLength(_anchoLabor / 2);
        aux1 = aux1.normalVector();

        QLineF lpt1;
        lpt1.setP1(aux1.p2());
        lpt1.setP2(aux1.p1());
        lpt1.setLength(_anchoLabor);

        QLineF aux2;
        aux2.setP1(point);
        aux2.setP2(lastIns);
        aux2.setLength(_anchoLabor / 2);
        aux2 = aux2.normalVector();

        QLineF lpt2;
        lpt2.setP1(aux2.p2());
        lpt2.setP2(aux2.p1());
        lpt2.setLength(_anchoLabor);

        QPolygonF newPol = QPolygonF(QVector<QPointF>() << lpt1.p1() << 
                 lpt1.p2() << lpt2.p1() << lpt2.p2());

        areas.append(newPol);
        // path.addPolygon(newPol);
        path.lineTo(point);    // <<---

        points.append(point);
    }
    this->parentItem()->update();
}
    
answered by 03.05.2018 / 14:58
source