Qt obtains the intersection of a polygon (qgraphicspolympus gonitem) or a QT graphical component and a straight line (qlinef ).

Source: Internet
Author: User
Tags polyline


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 connectionQ: 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.