计算地球上两条纬度/经度线段的交点
我一直在尝试多种功能,包括 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设:您的线段是大圆弧。
任何一对不同的大圆都恰好相交两次。因此,您可以:
这里是对此方法的讨论。
Assumption: your line segments are great circle arcs.
Any pair of distinct great circles intersects exactly twice. So, you can:
Here is a discussion of this method.