c – 我的交叉检查算法有什么问题?

我知道有很多网站可以解释如何检查两条线的交集,但我发现只需复制和粘贴代码就可以完成这么简单的数学任务.我越不能让我的代码无法工作.我知道“我的代码中有什么问题?”是愚蠢的,但我不知道我的数学/代码到底有什么问题,我的代码也很好地记录了(除了不可否认的变量命名),所以我猜应该有人对它背后的数学感兴趣:

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,而ax为0表示我们位于第一个点(a / c),ax为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代码时,它表示它们不相交

最佳答案 (你可以称我为学究,但术语很重要)

如果要查看线段是否相交,则依赖于两个线段的参数表示,在两个参数中求解系统,并查看两个参数的解都是否属于[0,1]范围.

段[a,b]的参数表示,分量方式

{x, y}(t) = {(1-t)*ax+t*bx, (1-t)*ay+t*by} with t in the [0,1] range

快速检查 – 在t = 0你得到一个,在t = 1你得到b,表达式是线性的t,所以你有它.

所以,你的(a,b)(c,d)交叉问题变成:

// for some t1 and t2, the x coordinate is the same
(1-t1)*ax+t*bx=(1-t2)*cx+t2*dx; 
(1-t1)*ay+t*by=(1-t2)*cy+t2*dy; // and so is the y coordinate

在t1和t2中解决系统问题.如果t1在[0,1]范围内,则交点位于a和b之间,对于c和d,t2也是如此.

对于读者来说,研究在以下条件之上对系统会产生什么影响以及对强大算法应该执行哪些检查是一种练习:

>段退化 – 一个或两个段的重合末端
>具有非空隙重叠的共线段.特殊情况下,有一个重叠点(必要时,该点将是其中一个端点)
>没有重叠的共线段
>平行段

点赞