Draw only the portion of the QPainter Path bezier curve

1.8k views Asked by At

I want to draw only the portion of the QPainter Curved Path. I have the path as this:

QPointF p[4];
p[0] = QPoint(100, 200);
p[1] = QPoint(200,  60);
p[2] = QPoint(500, 180);
p[3] = QPoint(600, 100);
cPath.cubicTo(p[1], -a*p[0] + p[1] + a*p[2], a*p[1] + p[2] -a*p[3], p[2]);

Now this draws the path as shown in the Fig 1.

enter image description here

But now I only want to draw the curve between the 2 points, let's say p(1) and p(2).

How can I draw only the portion of the bezier curve?

1

There are 1 answers

0
Gurjot Bhatti On BEST ANSWER

Here's how I solved this.

I used QPainterPath::toSubpathPolygons to return a list of polygons and got the QPolygonF.

const QMatrix m = QMatrix();
QPolygonF cPoly = cPath.cubicTo(p[1], -a*p[0] + p[1] + a*p[2], a*p[1] + p[2] -a*p[3], p[2]).toSubpathPolygons(m).first();

Then I iterated over this QPolygonF and checked if the points in this polygon lie between the two points that I want. And then added those points to a new polygon.

QPolygonF nPoly;
QPolygonF::iterator i;
for (i = cPoly.begin(); i != cPoly.end(); ++i){
    QPointF pnt = *i;
    if (pnt.rx() >= p[1].rx() && pnt.rx() <= p[2].rx())
    nPoly << pnt;
}

And now this polygon could be drawn with path.