Qt获取多边形(QGraphicsPolygonItem)或Qt图形组件与直线(QLineF)的交点


有时需要获取直线与各种图形的交点,包括多边形和各种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

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在`QGraphicsView`中为两条相交的直线添加圆弧,可以使用`QGraphicsPathItem`来绘制路径,然后设置笔刷和颜色来渲染路径。以下是一个简单的示例,演示如何在两条相交的线段交点处添加圆弧: ```cpp QPointF p1(0, 0); QPointF p2(100, 100); QPointF p3(150, 50); QPointF p4(50, 150); // 计算交点 QPointF intersection; QLineF::IntersectType intersectType = QLineF(p1, p2).intersect(QLineF(p3, p4), &intersection); if (intersectType == QLineF::BoundedIntersection) { // 创建路径 QPainterPath path; path.moveTo(p1); path.lineTo(intersection); path.arcTo(QRectF(intersection.x() - 5, intersection.y() - 5, 10, 10), 0, 90); path.lineTo(p2); path.moveTo(p3); path.lineTo(intersection); path.arcTo(QRectF(intersection.x() - 5, intersection.y() - 5, 10, 10), 90, 90); path.lineTo(p4); // 创建路径项并添加到场景中 QGraphicsPathItem* item = new QGraphicsPathItem(path); item->setPen(QPen(Qt::red, 2)); item->setBrush(QBrush(Qt::yellow)); scene->addItem(item); } ``` 在上面的代码中,我们首先使用`QLineF`类的`intersect()`方法来计算两条线段的交点。如果交点存在,我们创建一个`QPainterPath`对象,并使用`moveTo()`和`lineTo()`方法来描绘出两条线段和交点。然后我们使用`arcTo()`方法在交点处添加圆弧。最后,我们将路径设置为`QGraphicsPathItem`对象的形状,并将其添加到场景中。我们还设置了路径项的笔刷和颜色,以便在场景中呈现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值