检查QPainterPath中是否存在点

check if point exists in QPainterPath

本文关键字:存在 是否 QPainterPath 检查      更新时间:2023-10-16

我有一个应用程序,可以在场景中放置多条曲线。我一直在寻找一种简单的方法来检测用户是否按下了线路。当我画多条线时,CCD_ 1和CCD_。所以我做了这个函数,它像做梦一样工作,除非线条是垂直的。

selectionMargin是一个由用户设置的全局变量(默认值=0.5)。它可以调整选择检查的准确度。名称基于每条子线的线性函数y = ax + b。Pos是来自mousePressEvent的位置。

bool GraphApp::pointInPath(QPainterPath path, QPointF pos)
{
    qreal posY = pos.y();
    qreal posX = pos.x();
    for (int i = 0; i < path.elementCount()-1; ++i) {
        if (posX < path.elementAt(i + 1).x && posX > path.elementAt(i).x) {
            qreal dy = path.elementAt(i + 1).y - path.elementAt(i).y;
            qreal dx = path.elementAt(i + 1).x - path.elementAt(i).x;
            qreal a = dy / dx;
            qreal b = path.elementAt(i).y - (path.elementAt(i).x * a);
            if (selectionMargin == 0.0)
                selectionMargin = 0.5;
            qreal lowerBound = (a * posX + b) + selectionMargin;
            qreal upperBound = (a * posX + b) - selectionMargin;
            if (posY < lowerBound && posY > upperBound)
                return true;
        }
    }
    return false;
}

因此,当我从垂直线覆盖的区域发送mousePressEvent时,这个函数似乎返回false。我的第一个想法是if语句:

if (posX < path.elementAt(i + 1).x && posX > path.elementAt(i).x)

关于如何在没有if语句的情况下实现这一点,还有其他想法吗?

我也见过其他人在努力寻找一种很好的方法来检查QPainterPath是否包含没有boundingRect()intersects()函数的点,所以这可能也适用于其他人:)

编辑:据我所知,contains()使用boundingRect()。所以我不认为这是一个合适的解决方案

我曾经需要一些与你相似的东西。我需要测试两条路径的相似性。因此,我从点列表中创建了一个路径(我希望您不需要更复杂的路径,因为这个解决方案对于普通的QPaintingPaths来说会变得非常困难)。该路径是使用给定的"公差"构建的,这是您的boundingRect()0。

该函数返回一个QPainterPath,它"围绕给定的多段线绘制一个区域"。然后可以填充该区域,并将产生与使用圆帽和圆连接选项使用tolerance笔宽绘制原始多段线相同的图像。

你也可以,这就是你想要做的,检查给定点是否包含在这个路径中。注意,QPainterPath::contains检查点是否位于路径定义的闭合区域内。例如,这个闭合区域对于一个线段是空的,对于两个线段是三角形,所以如果您直接在路径上使用contains,这不是您想要的(正如我在问题的第三条评论中提到的)。

QPainterPath intersectionTestPath(QList<QPointF> input, qreal tolerance)
{
    //will be the result
    QPainterPath path;
    //during the loop, p1 is the "previous" point, initially the first one
    QPointF p1 = input.takeFirst(); 
    //begin with a circle around the start point
    path.addEllipse(p1, tolerance, tolerance); 
    //input now starts with the 2nd point (there was a takeFirst)
    foreach(QPointF p2, input) 
    {
        //note: during the algorithm, the pair of points (p1, p2)
        //      describes the line segments defined by input.
        //offset = the distance vector from p1 to p2
        QPointF offset = p2 - p1;
        //normalize offset to length of tolerance
        qreal length = sqrt(offset.x() * offset.x() + offset.y() * offset.y());
        offset *= tolerance / length;
        //"rotate" the offset vector 90 degrees to the left and right
        QPointF leftOffset(-offset.y(), offset.x());
        QPointF rightOffset(offset.y(), -offset.x());
        //if (p1, p2) goes downwards, then left lies to the left and
        //right to the right of the source path segment
        QPointF left1 = p1 + leftOffset; 
        QPointF left2 = p2 + leftOffset;
        QPointF right1 = p1 + rightOffset;
        QPointF right2 = p2 + rightOffset;
        //rectangular connection from p1 to p2
        {
            QPainterPath p;
            p.moveTo(left1);
            p.lineTo(left2);
            p.lineTo(right2);
            p.lineTo(right1);
            p.lineTo(left1);
            path += p; //add this to the result path
        }
        //circle around p2
        {
            QPainterPath p;
            p.addEllipse(p2, tolerance, tolerance);
            path += p; //add this to the result path
        }
        p1 = p2;
    }
    //This does some simplification; you should use this if you call
    //path.contains() multiple times on a pre-calculated path, but
    //you won't need this if you construct a new path for every call
    //to path.contains().
    return path.simplified();
}