Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I once needed something similar than you. I needed to test two paths for similarity. Therefore I created a path from a list of points (I hope you don't need a more complex path since this solution would become extremely more difficult for general QPaintingPaths). This path is constructed using a given "tolerance", this is your <code>selectionMargin</code>.</p> <p>The function returns a QPainterPath which "draws a region around the given polyline". This region can then be filled and would result in the same image as drawing the original polyline using a pen width of <code>tolerance</code> using round cap and round join options.</p> <p>You can also, and this is what you want to do, check if a given point is contained in this path. Note that <code>QPainterPath::contains</code> checks for a point to lie within the closed region defined by the path. E.g., this closed region is empty for a single line segment and a triangle for two line segments, so this is not what you want if you use <code>contains</code> directly on your path (as I mentioned in the 3rd comment to your question).</p> <pre><code>QPainterPath intersectionTestPath(QList&lt;QPointF&gt; input, qreal tolerance) { //will be the result QPainterPath path; //during the loop, p1 is the "previous" point, initially the first one QPointF p1 = input.takeFirst(); //begin with a circle around the start point path.addEllipse(p1, tolerance, tolerance); //input now starts with the 2nd point (there was a takeFirst) foreach(QPointF p2, input) { //note: during the algorithm, the pair of points (p1, p2) // describes the line segments defined by input. //offset = the distance vector from p1 to p2 QPointF offset = p2 - p1; //normalize offset to length of tolerance qreal length = sqrt(offset.x() * offset.x() + offset.y() * offset.y()); offset *= tolerance / length; //"rotate" the offset vector 90 degrees to the left and right QPointF leftOffset(-offset.y(), offset.x()); QPointF rightOffset(offset.y(), -offset.x()); //if (p1, p2) goes downwards, then left lies to the left and //right to the right of the source path segment QPointF left1 = p1 + leftOffset; QPointF left2 = p2 + leftOffset; QPointF right1 = p1 + rightOffset; QPointF right2 = p2 + rightOffset; //rectangular connection from p1 to p2 { QPainterPath p; p.moveTo(left1); p.lineTo(left2); p.lineTo(right2); p.lineTo(right1); p.lineTo(left1); path += p; //add this to the result path } //circle around p2 { QPainterPath p; p.addEllipse(p2, tolerance, tolerance); path += p; //add this to the result path } p1 = p2; } //This does some simplification; you should use this if you call //path.contains() multiple times on a pre-calculated path, but //you won't need this if you construct a new path for every call //to path.contains(). return path.simplified(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload