【发布时间】:2016-09-10 13:33:44
【问题描述】:
我知道有很多网站解释了如何检查两条线的交叉点,但我发现仅仅复制和粘贴代码来完成如此简单的数学任务非常无聊。我越是让我的代码无法工作而感到沮丧。我知道“我的代码有什么问题?”的问题。很愚蠢,但我不知道我的数学/代码到底出了什么问题,我的代码也被很好地记录了(除了公认的错误变量命名),所以我想应该有人对它背后的数学感兴趣:
bool segment::checkforIntersection(QPointF a, QPointF b) { //line 1: a+bx, line 2: c+dx, note that a and c are called offset and bx and dx are called gradients in this code
QPointF bx = b-a;
double firstGradient = bx.y() / bx.x(); //gradient of line 1
//now we have to calculate the offset of line 1: we have b from a+bx. Since QPointF a is on that line, it is:
//a + b * a.x = a.y with a as free variable, which yields a = a.y - b*a.x.
//One could also use the second point b for this calculation.
double firstOffset = a.y() - firstGradient * a.x();
double secondGradient, secondOffset;
for (int i = 0; i < poscount-3; i++) { //we dont check with the last line, because that could be the same line, as the one that emited intersection checking
QPointF c = pos[i];
QPointF d = pos[i+1];
QPointF dx = d-c;
secondGradient = dx.y() / dx.x(); //same formula as above
secondOffset = c.y() - secondGradient * c.x();
//a+bx=c+dx <=> a-c = (d-b)x <=> (a-c)/(d-b) = x
double x = (firstOffset - secondOffset) / (secondGradient - firstGradient);
//we have to check, if those lines intersect with a x \in [a.x,b.x] and x \in [c.x,d.x]. If this is the case, we have a collision
if (x >= a.x() && x <= b.x() && x >= c.x() && x <= d.x()) {
return true;
}
}
return false;
}
所以这是做什么的,它有 4 个点 a、b、c、d(第 1 行:a--b,第 2 行:c--d)(忽略 for 循环),它们具有绝对的 x 和 y 值.首先,它通过计算 deltay/deltax 来计算线的梯度。然后它通过使用点 a(或分别为 c)在线上的事实来计算偏移量。通过这种方式,我们将 4 个点转换为这些线的数学表示,如方程 a+bx,而 x 为 0 表示我们在第一个点 (a / c),而 x 为 1 表示我们在第二个点 (b /d)。接下来我们计算这两条线的交点(基本代数)。之后,我们检查交叉点的 x 值是否有效。据我了解,这一切都是正确的。有人看到错误吗?
根据经验检查这是不正确的。该代码没有给出任何误报(说有一个交集,但实际上没有),但它给出了假阴性(说没有交集,实际上有)。所以当它说有交叉点时它是正确的,但是如果它说没有交叉点,你不能总是相信我的算法。
再次,我在网上查了一下,但算法不同(有一些定位技巧等),我只是想提出自己的算法,如果有人能提供帮助,我会很高兴。 :)
编辑:这是一个最小的可重现的不工作示例,这次没有 Qt,但只有 C++:
#include <iostream>
#include <math.h>
using namespace std;
class Point {
private:
double xval, yval;
public:
// Constructor uses default arguments to allow calling with zero, one,
// or two values.
Point(double x = 0.0, double y = 0.0) {
xval = x;
yval = y;
}
// Extractors.
double x() { return xval; }
double y() { return yval; }
Point sub(Point b)
{
return Point(xval - b.xval, yval - b.yval);
}
};
bool checkforIntersection(Point a, Point b, Point c, Point d) { //line 1: a+bx, line 2: c+dx, note that a and c are called offset and bx and dx are called gradients in this code
Point bx = b.sub(a);
double firstGradient = bx.y() / bx.x(); //gradient of line 1
//now we have to calculate the offset of line 1: we have b from a+bx. Since Point a is on that line, it is:
//a + b * a.x = a.y with a as free variable, which yields a = a.y - b*a.x.
//One could also use the second point b for this calculation.
double firstOffset = a.y() - firstGradient * a.x();
double secondGradient, secondOffset;
Point dx = d.sub(c);
secondGradient = dx.y() / dx.x(); //same formula as above
secondOffset = c.y() - secondGradient * c.x();
//a+bx=c+dx <=> a-c = (d-b)x <=> (a-c)/(d-b) = x
double x = (firstOffset - secondOffset) / (secondGradient - firstGradient);
//we have to check, if those lines intersect with a x \in [a.x,b.x] and x \in [c.x,d.x]. If this is the case, we have a collision
if (x >= a.x() && x <= b.x() && x >= c.x() && x <= d.x()) {
return true;
}
return false;
}
int main(int argc, char const *argv[]) {
if (checkforIntersection(Point(310.374,835.171),Point(290.434,802.354), Point(333.847,807.232), Point(301.03,827.172)) == true) {
cout << "These lines do intersect so I should be printed out\n";
} else {
cout << "The algorithm does not work, so instead I do get printed out\n";
}
return 0;
}
因此,我以 ~ (310,835) -- (290,802) 和 (333,807) -- (301,827) 为例。这些线确实相交:
\documentclass[crop,tikz]{standalone}
\begin{document}
\begin{tikzpicture}[x=.1cm,y=.1cm]
\draw (310,835) -- (290,802);
\draw (333,807) -- (301,827);
\end{tikzpicture}
\end{document}
Proof of intersection 但是在运行上面的 C++ 代码时,它说它们不相交
【问题讨论】:
-
调试器是解决此类问题的正确工具。 在询问 Stack Overflow 之前,您应该逐行逐行检查您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 [编辑] 您的问题,以包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
-
这不是我没有尝试自己调试它。我尝试调试,但半小时后没有任何线索,我最终放弃了。也许我只是误解了一些数学,这就是问题所在。真正尝试过自己解决后问有什么问题?
-
What is the problem of asking after genuinely having tried solving it by your own?你仍然缺少minimal reproducible example -
@m-s 我确实忘记了一个算法不起作用的点元组的例子。对于给您带来的不便,我深表歉意。我编辑了原始帖子以符合您的要求。现在您所要做的就是复制代码并在其上运行 g++ 或 clang++。我希望现在你能提供帮助:)
标签: c++ algorithm qt math intersection