开发者

How to test proximity to the outline of a QGraphicsPathItem?

开发者 https://www.devze.com 2022-12-19 22:15 出处:网络
I\'m trying to test whether a given point开发者_StackOverflow社区 (x, y) is located on or close to the outline of a QGraphicsPathItem.

I'm trying to test whether a given point开发者_StackOverflow社区 (x, y) is located on or close to the outline of a QGraphicsPathItem.

Using QGraphicsItem.contains() or .collidesWithItem() / Path() will not do: those also return True if the point is contained within the area on the interior of the path, while I want to test only for points on the outline. How can I accomplish that?


After posting the question I found the following solution:

    path = QPainterPath(...)   # Path we are testing against
    point = QPointF(...)       # Current position

    stroker = QPainterPathStroker()
    stroker.setWidth(10)        # Distance which we consider "on" the path 
    stroke = stroker.createStroke(path)

    if stroke.contains(point):
        # point is on path
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号