Intersection point of QPainterPath and line (find QPainterPath y by x)

谁说我不能喝 提交于 2019-12-10 16:45:32

问题


I have QPainterPath. I need to find y coordinate of QPainterPath by x.

I found intersected() method in QPainterPath. So, I created new QPainterPath, which is line from left to right edge of my path's bounding rect with x coordinate, to find point as result of intersection.

intersects() method returns true. But intersected() returns empty path.

Everything works If I use rect with height = 1 instead of line.

Maybe you have a better idea how to find intersection of QPainterPath with line?


回答1:


According to the documentation:

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.

Since your line has no fill area, it would seem like this function would not work for you.

If you are using a QGraphicsScene to display your QPainterPath you can use the method 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).

Unfortunately, QPainter does not seem to have the same function. I think that your method of creating a long rectangle might be an easier way of doing this.



来源:https://stackoverflow.com/questions/9393672/intersection-point-of-qpainterpath-and-line-find-qpainterpath-y-by-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!