我一直在尝试多种功能,包括 2D 功能,试图让它有点工作,但还没有运气......
我在地球上有 2 条 latlng 端点的线段,我想知道 2 条线是否相交以及在何处相交。
我目前正在研究这个物理专业的学生说应该为二维平面做这项工作,但事实并非如此。对于相交,它总是返回 true
[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 = (双) $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
}[/代码]