如何修复此JS函数,因此在检查两个行段是否相交时它不会返回误报?

发布于 2025-02-11 03:31:48 字数 1581 浏览 2 评论 0原文

我正在研究一个映射项目(使用Google Maps SDK)。在地图上绘制线段之前,我检查一下它是否与任何现有线相交。

我正在体验下面的代码报告线段正在相交的情况,但事实并非如此。虽然,他们确实有一个终点。

也许此Intersect()函数并不是要与地理坐标一起使用,我需要其他功能。我在此处找到了此InterSects()函数:测试/a>

在这个项目中,我有大约150行,其中约有10-15行被检测为相交,尽管它们与报道称为相交的线路共有共同的终点)。

以下是如何在Google Maps上绘制线条以获得更好的视觉效果的屏幕截图。红线是将第二条传递到Intersects()函数,并被检测到与第一行相交。有趣的是,如果我将传递给Intersects()的线反向,则不会发现它们正在相交。

let crosses = intersects(
        39.018223, -76.75899, 39.018387, -76.758773,
        39.018387, -76.758773, 39.019813, -76.757388,
);
      
console.log('Intersects:', crosses);

// returns true if the line from (a,b)->(c,d) intersects with (p,q)->(r,s)
function intersects(a,b,c,d,p,q,r,s) {
        var det, gamma, lambda;
        det = (c - a) * (s - q) - (r - p) * (d - b);
        if (det === 0) {
                console.log('det is zero');
                return false;
        } else {
                lambda = ((s - q) * (r - a) + (p - r) * (s - b)) / det;
                gamma = ((b - d) * (r - a) + (c - a) * (s - b)) / det;
                return (0 < lambda && lambda < 1) && (0 < gamma && gamma < 1);
        }
};

I'm working on a mapping project (with the Google Maps SDK). Before drawing a line segment on the map I check to see if it intersects with any existing lines.

I'm experiencing a situation where the code below is reporting the line segments are intersecting, but they're not. Although, they do share an endpoint.

Perhaps this intersect() function isn't meant to be used with Geo coordinates and I need a different function. I found this intersects() function here: Test if two lines intersect - JavaScript function

In this project I have about 150 lines and about 10-15 of them are detected as intersecting, and they're not (although they share a common endpoint with the line that is reported as intersecting).

Below is a screenshot of how the lines are drawn on Google Maps for a better visual. The red line is the second one passed to the intersects() function and is detected as intersecting the first line. Interestingly, if I reverse the lines passed to intersects() it does not find them to be intersecting.

let crosses = intersects(
        39.018223, -76.75899, 39.018387, -76.758773,
        39.018387, -76.758773, 39.019813, -76.757388,
);
      
console.log('Intersects:', crosses);

// returns true if the line from (a,b)->(c,d) intersects with (p,q)->(r,s)
function intersects(a,b,c,d,p,q,r,s) {
        var det, gamma, lambda;
        det = (c - a) * (s - q) - (r - p) * (d - b);
        if (det === 0) {
                console.log('det is zero');
                return false;
        } else {
                lambda = ((s - q) * (r - a) + (p - r) * (s - b)) / det;
                gamma = ((b - d) * (r - a) + (c - a) * (s - b)) / det;
                return (0 < lambda && lambda < 1) && (0 < gamma && gamma < 1);
        }
};

Two lines

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

躲猫猫 2025-02-18 03:31:48

您需要考虑数值精度。正如该帖子评论中提到的那样,您可以测试端点,但它会增加更多的计算。另一种方法是通过在比较中添加epsilon来考虑最终测试的精度:

let epsilon = 1e-6;
return (epsilon < lambda && lambda < 1-epsilon) &&
       (epsilon <  gamma && gamma  < 1-epsilon);

You need to account for numerical precision. As mentioned in a comment of the post, you could test for endpoints but it would add a lot more computation. Another method is to account for precision in the final test by adding an epsilon to the comparison:

let epsilon = 1e-6;
return (epsilon < lambda && lambda < 1-epsilon) &&
       (epsilon <  gamma && gamma  < 1-epsilon);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文