View Full Version : Draw only a portion of Qt Curved Path

11th August 2015, 13:10

I want to draw only the portion of the QPainter Curved Path. I have the following code:

QPointF points[5];
points[0] = QPoint(100, 200);
points[1] = QPoint(200, 60);
points[2] = QPoint(500, 180);
points[3] = QPoint(600, 100);

QPainter painter(this);
painter.scale(1,-1); painter.translate(0, -250);
QPen pen;

float a = 1.0 / 6.0;
cPath.cubicTo(p[1], -a*p[0] + p[1] + a*p[2], a*p[1] + p[2] -a*p[3], p[2]);

painter.strokePath(cPath, pen);

This draws the bezier curve just fine. But now I want to draw only to draw the portion of this whole curve, for example only the portion between points[1] and points[2].

How can I achieve this?

12th August 2015, 19:13
You could try QPainterPath::toSubpathPolygons(), which might get you back the polyline. Otherwise, you could either find some bezier curve generating code online or look into the QPainterPath source code and extract the algorithm from there. Then you can create an array of the points for as much of the curve as you want.

13th August 2015, 00:55
Interestingly, a cubic Bézier curve has its two middle control points (1 and 2, from 0, 1, 2, 3) exactly in thirds (0.33333...) in terms of parameter value.

So if you want to draw just the piece of the curve from section 1 to section 2, you need to break that curve apart (with math) at points t1=0.333 and t2=0.666.

To find the new control points of this piece of curve it's (relatively) easy: you run the De Casteljau's algorithm in reverse, like the following illustrates.
The De Casteljau algorithm is simple, it uses proportional lengths of connected lines:

13th August 2015, 16:26
You could try QPainterPath::toSubpathPolygons(), which might get you back the polyline.

Thank you, your suggestion helped. :)

I got the list of QPolygonF from which I got a single QPolygonF. And then I created a new QPolygonF in which I only inserted those QPointF which were needed. And then I drew that.