zoukankan      html  css  js  c++  java
  • Qt获取多边形(QGraphicsPolygonItem)或Qt图形组件与直线(QLineF)的交点


    有时需要获取直线与各种图形的交点,包括多边形和各种Qt图形框。

    例如上图中,要想使连接线始终在多边形的边上,且能指向多边形中心,那么我们就要获取连线AB与多边形的交点。

    1.多边形(QGraphicsPolygonItem)与直线(QLineF)的交点

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    //已知点和多边形//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)的交点

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    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





    如果,感到此时的自己很辛苦,那告诉自己:容易走的都是下坡路。坚持住,因为你正在走上坡路,走过去,你就一定会有进步。如果,你正在埋怨命运不眷顾,开导自己:命,是失败者的借口;运,是成功者的谦词。命运从来都是掌握在自己的手中,埋怨,只是一种懦弱的表现;努力,才是人生的态度。
  • 相关阅读:
    angularjs的$on、$emit、$broadcast
    angularjs中的路由介绍详解 ui-route(转)
    ionic入门教程-ionic路由详解(state、route、resolve)(转)
    Cocos Creator 加载使用protobuf第三方库,因为加载顺序报错
    Cocos Creator 计时器错误 cc.Scheduler: Illegal target which doesn't have uuid or instanceId.
    Cocos Creator 构造函数传参警告 Can not instantiate CCClass 'Test' with arguments.
    Cocos Creator 对象池NodePool
    Cocos Creator 坐标系 (convertToWorldSpaceAR、convertToNodeSpaceAR)
    Cocos Creator 常驻节点addPersistRootNode
    Cocos Creator 配合Tiled地图的使用
  • 原文地址:https://www.cnblogs.com/superit/p/3831808.html
Copyright © 2011-2022 走看看