Sometimes it is necessary to obtain the intersection between a straight line and various graphics, including polygon and various QT graphic boxes.
For example, to keep the connection line on the side of a polygon and point to the center of the polygon, we need to obtain the intersection of the AB and polygon of the line.
1. intersection of a polygon (qgraphicspolympus gonitem) and a straight line (qlinef)
1234567891011121314151617 |
// Known points and polygon // points A and B can be obtained through boundingrect (). Width ()/2 of polygon; QPointF A; QPointF B; QGraphicsPolygonItem yellowItem; QLineF lineAB(A,B); // AB connection qpointf Arrowpoint; // you can specify the intersection. QPolygonF yellowtPolygon = yellowItem->polygon(); QPointF p1 = yellowtPolygon.first() + yellowItem->pos(); // Traverse each edge line for (INT I = 1; I <yellowtpolygon. count (); ++ I) {qpointf P2 = yellowtpolygon. at (I) + yellowitem-> pos (); QLineF polyLine = QLineF(p1, p2); // Core: Determine whether the intersection is qlinef: intersecttype = polyline. Intersect (lineab, & Arrowpoint); If (intersecttype = qlinef: boundedintersection) break; p1 = p2; } // Arrowpoint is the intersection. |
From the code above, we can see that the intersection of a polygon and a straight line is to traverse the connection between a straight line and all edges. by extension, all QT graphical components, such as qpushbutton and qqgraphicstextitem, the intersection of a graphic component with a boundary can be obtained. That is, you can traverse the intersection of all edges and straight lines.
2. Intersection of QT graphical components and qlinef
1234567891011121314151617181920212223242526272829303132333435 |
QPointF A; QPointF B; QLineF lineAB(A,B); // AB connection Q: common component m_commentitem; qreal commentWidth = m_CommentItem->boundingRect().width(); qreal commentHeight = m_CommentItem->boundingRect().height(); QPointF intersectPoint; // Four Edges-four lines qlinef line1 (, commentwidth, 0 ); QLineF line2( 0,0,0 ,commentHeight); QLineF line3(commentWidth, 0 ,commentWidth,commentHeight); QLineF line4( 0 ,commentHeight,commentWidth,commentHeight); QList<QLineF> lineList; lineList.append(line1); lineList.append(line2); lineList.append(line3); lineList.append(line4); // Traverse four lines of foreach (QLineF oneline,lineList) { QLineF::IntersectType intersectType = oneline.intersect(lineAB, &intersectPoint); if (intersectType == QLineF::BoundedIntersection) break ; } // Intersectpoint is the intersection point |
3. Example:
Qt examples and Demos-> graphics view-> dimo-scene
From Weizhi note (wiz)