草庐IT

c++ - 我的交集检查算法有什么问题?

coder 2024-02-23 原文

我知道有很多网站都介绍了如何检查两条线的交点,但我发现为这样一个简单的数学任务复制和粘贴代码非常无聊。我越让我沮丧的是我无法让我的代码工作。我知道“我的代码有什么问题?”的问题。很愚蠢,但我不知道我的数学/代码到底出了什么问题,我的代码也有很好的文档记录(除了公认的错误变量命名),所以我想应该有人对它背后的数学感兴趣:

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++ 代码时,它说它们不相交

最佳答案

(你可以说我是 Nerd ,但术语很重要)

如果您想查看线线段是否相交,则依赖于您的两个线段的参数表示,在两个参数中求解系统,看看是否两个解决方案都适用于参数在[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 你得到 a,在 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

求解 t1t2 中的系统。如果 t1[0,1] 范围内,则交点位于 ab 之间,则对于 cdt2 也是如此。

留给读者作为练习,研究在以下条件之上会对系统产生什么影响,以及应该为稳健算法实现哪些检查:

  1. 片段退化 - 一个或两个片段重合
  2. 具有非空白重叠的共线段。有一个重叠点的特殊情况(必要的,那个点将是其中一个端点)
  3. 没有重叠的共线段
  4. 平行片段

关于c++ - 我的交集检查算法有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39426589/

有关c++ - 我的交集检查算法有什么问题?的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  3. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  4. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  5. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  6. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  7. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  8. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  9. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  10. ruby - 检查数组是否在增加 - 2

    这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife

随机推荐