qt – QPainterPath和line的交点(通过x找到QPainterPath y)

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

我在QPainterPath中找到了intersectcted()方法.所以,我创建了新的QPainterPath,它是从我的路径的边界矩形与x坐标的左边缘到右边缘,以找到交点的结果.

intersects()方法返回true.但是intersectcted()返回空路径.

一切正常如果我使用rect = height = 1而不是line.

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

最佳答案 根据文件:

QPainterPath QPainterPath::intersected ( const QPainterPath & p ) const

Returns a path which is the intersection of this path’s fill area and p’s fill area.
Bezier curves may be flattened to line segments due to numerical instability of doing bezier curve intersections.

由于你的生产线没有填充区域,看起来这个功能对你不起作用.

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

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

Returns a list of all items that collide with item. Collisions are determined by calling QGraphicsItem::collidesWithItem(); the collision detection is determined by mode. By default, all items whose shape intersects item or is contained inside item’s shape are returned.
The items are returned in descending stacking order (i.e., the first item in the list is the uppermost item, and the last item is the lowermost item).

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

点赞