QPainterPath from list of points
-
@Joel-Bodenmann
ok, so you must have an actual polygon to return
for hit-testing/handles to work. -
@Joel-Bodenmann
Let me first clarify something, you want to get an envelope around your path, that is larger (respectful to the lines) by some fixed pixel offset? Or you're talking about enveloping a polygon?Right, I read your post again and understand what you mean now. I believe you should implement that yourself, at least I don't know any easy way. I assume your points are ordered, so it's a simple linear algebra problem. You take the points by pairs, and from that pair you can easily calculate the alongside and normal vector for each line. From those two vectors (after normalization) you can build up your envelope. I hope that helps.
Kind regards.
-
@kshegunov You are correct, I need an envelope around the path that has a few pixels of padding on each side of the line. Like the green line in this crude drawing that I made earlier: http://paste.ugfx.org/sores/7505d64a54e0/264c3e10630e.jpg
-
@Joel-Bodenmann
Sorry for updating a previous post, I didn't realize you were writing at that same time. Look up my suggestion, and if there's something unclear I'll try to expand.Kind regards.
-
@kshegunov
Thanks for your answer. I guess I'll have to figure out how to properly implement this in the next couple of days then. I'll definitely publish results here so people don't have to do that in the future themselves :) -
@Joel-Bodenmann
Also QPainterPathStroker::createStroke coupled with QPainterPath::toFillPolygon might be the easiest way to get what you're after. -
@kshegunov
Can you elaborate? I still need to get the outline of the stroked polygon somehow, no? -
@Joel-Bodenmann
Hello,
Surely, note however that I've not tried this. Set the pen width with your desired offset. Create the stroke, with thecreateStroke
method. The outline of the stroke is supposed to be (as per the documentation) the painter path you get from it. Convert the path to a polygon with thetoFillPolygon
method. If it doesn't work, you can always revert to implementing it yourself, but if it does, well, it looks simpler. :)Kind regards.
-
@kshegunov
Thanks for the explanation. The trouble I am having is getting theQPainterPath
(that needs to be passed toQPainterPathStroker::createStroke()
from my list of lines. The only reasonable option I see isQPainterPath::addPolygon()
but that I can't use as I don't have a polygon, I only have a polyline (like a non-closed polygon).Any thoughts?
-
@Joel-Bodenmann
Hello,
One thing is to create the path from a polygon (no one says it has to be a closed one ;)). You could also perhaps get it by constructing the path from your points withQPainterPath::moveTo
andQPainterPath::lineTo
. The latter would be my choice if I were supposed to compose a painter path.Kind regards.
-
As mentioned above you can use QPainterPathStroker. The problem is it will create tiny loops on the inside of the joints, so you need to simplify the path afterwards.
Sample code:QVector<QPoint> points { QPoint(30,30), QPoint(100, 100), QPoint(200,50) }; QPainterPath basePath; basePath.addPolygon(QPolygon(points)); QPainterPathStroker str; str.setCapStyle(Qt::RoundCap); str.setWidth(10.0); QPainterPath resultPath = str.createStroke(basePath).simplified();
-
Wow, I am deeply impressed. Using the code shown by @Chris-Kawa gave exactly the desired result:
Thank you very much for your help, guys. Very appreciated!
-
well what can we say ?
Chris rules :) -
@mrjj Well, thanks to you and @kshegunov too. You guys are amazing!