计算地球上两条纬度/经度线段的交点

发布于 2024-09-14 18:05:32 字数 1760 浏览 6 评论 0原文

我一直在尝试多种功能,包括 2D 功能,试图让它在某种程度上发挥作用,但还没有运气......

我在地球上有 2 条经纬度端点的线段,我想知道这两条线是否相交以及在哪里相交。

我目前正在研究这个,一位物理学专业的学生说应该为二维平面完成这项工作,但事实并非如此。 它总是返回 true

对于 intersect [code] 函数 intersectPoint($line1start, $line1end, $line2start, $line2end) //($p0_x, $p0_y, $p1_x, $p1_y, $p2_x, $p2_y, $p3_x, $p3_y) { $p0_x = $line1start['纬度']; $p0_y = $line1start['lng']; $p1_x = $line1end['纬度']; $p1_y = $line1end['lng']; $p2_x = $line2start['纬度']; $p2_y = $line2start['lng']; $p3_x = $line1end['纬度']; $p3_y = $line1end['lng'];

$s1_x = (double) $p1_x - (double) $p0_x;
$s1_y = (double) $p1_y - (double) $p0_y;

// s1_x = p1_x - p0_x; // s1_y = p1_y - p0_y; $s2_x = (双精度) $p3_x - (双精度) $p2_x; $s2_y = (双精度) $p3_y - (双精度) $p2_y; $s3_x = (双精度) $p0_x - (双精度) $p2_x; $s3_y = (双精度) $p0_y - (双精度) $p2_y; // s2_x = p3_x - p2_x; // s2_y = p3_y - p2_y;

$s = (double) ((double)(-$s1_y * $s3_x + $s1_x * $s3_y) / (double) (-$s2_x * $s1_y + $s1_x * $s2_y));
$t = (double) ((double)( $s2_x * $s3_y - $s2_y * $s3_x) / (double) (-$s2_x * $s1_y + $s1_x * $s2_y));

// s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y); // t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);

if ($s >= 0 && $s <= 1 && $t >= 0 && $t <= 1)
{
    AppCommUtility::echof(" FUNC RETURNED TRUE $s >= 0 && $s <= 1 && $t >= 0 && $t <= 1");
    // Collision detected
    return array(
        'lat' => $p0_x + ($t * $s1_x),
        'lng' => $p0_y + ($t * $s1_y)
    );
}

return null; // No collision

}[/代码]

i've been trying multiple functions including 2D ones to try to get this somewhat working, but no luck yet...

I have 2 line segments of latlng endpoints on earth, and i want to know if and where the 2 lines intersect.

I'm currently working with this which a physics major says should be doing the job for a 2d plane but its not. it always returns true for intersect

[code]function intersectPoint($line1start, $line1end, $line2start, $line2end)
//($p0_x, $p0_y, $p1_x, $p1_y, $p2_x, $p2_y, $p3_x, $p3_y)
{
$p0_x = $line1start['lat'];
$p0_y = $line1start['lng'];
$p1_x = $line1end['lat'];
$p1_y = $line1end['lng'];
$p2_x = $line2start['lat'];
$p2_y = $line2start['lng'];
$p3_x = $line1end['lat'];
$p3_y = $line1end['lng'];

$s1_x = (double) $p1_x - (double) $p0_x;
$s1_y = (double) $p1_y - (double) $p0_y;

// s1_x = p1_x - p0_x;
// s1_y = p1_y - p0_y;
$s2_x = (double) $p3_x - (double) $p2_x;
$s2_y = (double) $p3_y - (double) $p2_y;
$s3_x = (double) $p0_x - (double) $p2_x;
$s3_y = (double) $p0_y - (double) $p2_y;
// s2_x = p3_x - p2_x;
// s2_y = p3_y - p2_y;

$s = (double) ((double)(-$s1_y * $s3_x + $s1_x * $s3_y) / (double) (-$s2_x * $s1_y + $s1_x * $s2_y));
$t = (double) ((double)( $s2_x * $s3_y - $s2_y * $s3_x) / (double) (-$s2_x * $s1_y + $s1_x * $s2_y));

// s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
// t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);

if ($s >= 0 && $s <= 1 && $t >= 0 && $t <= 1)
{
    AppCommUtility::echof(" FUNC RETURNED TRUE $s >= 0 && $s <= 1 && $t >= 0 && $t <= 1");
    // Collision detected
    return array(
        'lat' => $p0_x + ($t * $s1_x),
        'lng' => $p0_y + ($t * $s1_y)
    );
}

return null; // No collision

}[/code]

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

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

发布评论

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

评论(1

香橙ぽ 2024-09-21 18:05:32

假设:您的线段是大圆弧。

任何一对不同的大圆都恰好相交两次。因此,您可以:

  1. 找到两个交点。
  2. 查看交点是否位于圆弧内。

这里是对此方法的讨论。

Assumption: your line segments are great circle arcs.

Any pair of distinct great circles intersects exactly twice. So, you can:

  1. Find the two points of intersection.
  2. See if the intersection points lie in your arcs.

Here is a discussion of this method.

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