Reputation: 33435
QPolygonF has methods to union, intersect, and subtract with other QPolygonFs but I need to perform an intersection test with a QLineF. This appears to be missing from the API.
I suppose I could do something like this:
if (polygon .containsPoint (line .p1 ()) != polygon .containsPoint (line .p2 ())
return true;
QPointF a = polygon .back ();
foreach (QPointF b, polygon)
{
if (QLineF :: BoundedIntersection == line .intersect (QPointF (a, b))
return true;
a = b;
}
return false;
There are probably some numerical or edge-case surprises lurking in the above, so I'd rather not.
Is there a provided method somewhere in the Qt API that I can't see?
Upvotes: 4
Views: 6717
Reputation: 8231
The implementation of QPolygonF::intersected is:
QPainterPath subject; subject.addPolygon(*this);
QPainterPath clip; clip.addPolygon(r);
return subject.intersected(clip).toFillPolygon();
i.e. QPolygonF uses QPainterPath methods to do the intersections. So you could do:
I DID NOT TRY THIS! You have to check this yourself. I am not sure if this is better in regard to numerical stability and performance than your solution, so you should write some tests.
Upvotes: 5