I receive positions on the plane and I want to draw the route. It is not a line, but a path... that is, I receive each point and generate a polygon that has a "working width" of 7, then each point I receive, I generate a polygon with a width of 7 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 comes out cut off... I need it to come out neat and the curves more CURVED. I leave you an image of how it looks to see if someone can guide me to improve it.
areas
is aQVector<QPolygonF>
, which I use to accumulate the polygons and add the area that the machine covers.points
is aQVector<QPointF>
, that I accumulate the points that I receive to generate the polygons between the point that I receive and the last one that is stored inpoints
.path
It is aQPainterPath
that there I throw all the 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 that there would be something like this (It doesn't have the same shape because I did it in paint):
The polygons are independent of each other. If what you want is for the path to be continuous, you have to draw the route point to point: