检查QPainterPath中是否存在点
check if point exists in QPainterPath
我有一个应用程序,可以在场景中放置多条曲线。我一直在寻找一种简单的方法来检测用户是否按下了线路。当我画多条线时,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();
}
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 有了gcc,是否可以链接库,但前提是它存在
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- 如何检查QList中是否存在值
- 根据某个函数是否存在启用模板
- 地图计数确实很重要,或者只是检查是否存在
- C++中是否存在 std::conditional 的懒惰等价物?
- 无论如何,我可以确定构造函数是否存在吗?
- COM :是否可以查看是否存在对我的某个 COM 对象的进程外引用?我可以释放它吗?
- 堆分配的对象是否存在永不为空的唯一所有者?
- 扩展类中的可选 vir 函数,测试它在运行时是否存在
- 模板化检查是否存在带有参数列表的类成员函数?
- 是否存在包含负号的isdigit函数(过载)
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 检查编译时是否存在静态函数
- 向量是否存在行主要形式?
- 检查 TinyXML 中的元素是否存在
- 检测是否存在具有 C++17 的类成员
- 虚拟继承中是否存在多重继承?
- 我遇到了这个代码片段,不明白. 它递归检查 C++ 字符串中是否存在大写字符