Qt获取多边形(QGraphicsPolygonItem)或Qt图形组件与直线(QLineF)的交点
来源:互联网 发布:艾瑞网怎么查数据 编辑:程序博客网 时间:2024/05/01 05:10
有时需要获取直线与各种图形的交点,包括多边形和各种Qt图形框。
例如上图中,要想使连接线始终在多边形的边上,且能指向多边形中心,那么我们就要获取连线AB与多边形的交点。
1.多边形(QGraphicsPolygonItem)与直线(QLineF)的交点
//已知点和多边形//A、B点可通过多边形的boundingRect().width()/2获取;QPointF A;QPointF B;QGraphicsPolygonItem yellowItem;QLineF lineAB(A,B); //AB连线QPointF arrowPoint; //设置交点QPolygonF yellowtPolygon = yellowItem->polygon();QPointF p1 = yellowtPolygon.first() + yellowItem->pos();//遍历各边连线for (int i = 1; i < yellowtPolygon.count(); ++i) {QPointF p2 = yellowtPolygon.at(i) + yellowItem->pos();QLineF polyLine = QLineF(p1, p2);//核心:判断是否相交QLineF::IntersectType intersectType =polyLine.intersect(lineAB, &arrowPoint);if (intersectType == QLineF::BoundedIntersection)break;p1 = p2;}//arrowPoint 即为交点
从上面代码可以看到,一个多边形和直线的交点的实现,就是遍历直线与所有边的联系,推而广之,所有Qt的图形化组件,比如QPushButton,QQGraphicsTextItem等,只要有边界的图形化组件都能获取其与直线的交点。即遍历所有边与直线的交点即可。
2.Qt图形组件与直线(QLineF)的交点
QPointF A;QPointF B;QLineF lineAB(A,B); //AB连线Q普通组件 m_CommentItem;qreal commentWidth = m_CommentItem->boundingRect().width();qreal commentHeight = m_CommentItem->boundingRect().height();QPointF intersectPoint;//四个边-四条线QLineF line1(0,0,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);//遍历四条线foreach(QLineF oneline,lineList){QLineF::IntersectType intersectType =oneline.intersect(lineAB, &intersectPoint);if (intersectType == QLineF::BoundedIntersection)break;}//intersectPoint 即为交点
3.可参考例子:
Qt Examples and Demos -> Graphics View ->Diagram Scene
- Qt获取多边形(QGraphicsPolygonItem)或Qt图形组件与直线(QLineF)的交点
- 如何获取直线与QPainterPath的交点
- hdu2528 Area 直线与多边形交点
- VTK 求多边形和直线的交点
- 多边形裁剪圆的实现细节之求直线段与圆的交点
- 直线与抛物线的交点
- 直线与圆的交点
- 空间直线与平面的交点
- 13.3直线与圆的交点
- 求空间直线与平面的交点
- poj1039 Pipe 线段与直线的交点
- Coverage 圆与直线的交点
- 三维空间中直线与平面的交点
- 斩bnuoj 直线与矩形的交点
- 空间直线与平面的交点
- 求直线与平面的交点
- [算法]直线与圆的交点程序设计
- 空间直线与平面的交点
- 事务隔离级别
- ios 应用内支付,沙盒测试时出现“此Apple ID尚未在iTunes Store使用过。。。”
- 前后台参数传递(&,%处理)
- struts配置文件
- ios学习笔记(转)
- Qt获取多边形(QGraphicsPolygonItem)或Qt图形组件与直线(QLineF)的交点
- 如何删除arcsde服务
- 内核风格缩进工具indent
- 一些重要网址
- Linux I2C设备读写应用程序
- AndEngine实现Sprite更换图片
- 土豆网8月24起在纳市摘牌 CEO王微宣布退休
- [English Homework] Persentaion about my company
- sssddrfrr