QPainterPath 与直线的交点(通过 x 求 QPainterPath y)

Fun*_*unt 5 qt point line-intersection qpainter

我有 QPainterPath。我需要通过 x 找到 QPainterPath 的 y 坐标。

我在 QPainterPath 中找到了 intersected() 方法。因此,我创建了新的 QPainterPath,它是具有 x 坐标的从路径边界矩形的左边缘到右边缘的线,以查找作为交集结果的点。

intersects() 方法返回 true。但 intersected() 返回空路径。

如果我使用高度 = 1 的矩形而不是直线,一切正常。

也许您有更好的想法如何找到 QPainterPath 与线的交点?

bus*_*ter 2

根据文档:

QPainterPath QPainterPath::相交 ( const QPainterPath & p ) const

返回一条路径,该路径是该路径的填充区域和 p 的填充区域的交集。由于贝塞尔曲线相交的数值不稳定,贝塞尔曲线可能会展平为线段。

由于您的线条没有填充区域,因此该功能似乎不适合您。

如果您使用 QGraphicsScene 来显示 QPainterPath,您可以使用 collidingItems 方法:

QList QGraphicsScene::collidingItems ( const QGraphicsItem * item, Qt::ItemSelectionMode 模式 = Qt::IntersectsItemShape ) const

返回与项目发生碰撞的所有项目的列表。碰撞是通过调用 QGraphicsItem::collidesWithItem() 来确定的;碰撞检测由模式决定。默认情况下,返回形状与 item 相交或包含在 item 形状内的所有项目。项目按降序堆叠顺序返回(即列表中的第一项是最上面的项目,最后一个项目是最下面的项目)。

不幸的是,QPainter 似乎没有相同的功能。我认为您创建长矩形的方法可能是一种更简单的方法。