Draw only a portion of Qt Curved Path
Hello,
I want to draw only the portion of the QPainter Curved Path. I have the following code:
Code:
painter.scale(1,-1); painter.translate(0, -250);
pen.setWidth(2);
painter.setPen(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]);
pen.setColor(Qt::darkRed);
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?
Re: Draw only a portion of Qt Curved Path
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.
Re: Draw only a portion of Qt Curved Path
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.
http://pomax.github.io/bezierinfo/#moulding
The De Casteljau algorithm is simple, it uses proportional lengths of connected lines:
http://jeremykun.com/tag/de-casteljau/
Re: Draw only a portion of Qt Curved Path
Quote:
Originally Posted by
d_stranz
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.