Skip to content

Instantly share code, notes, and snippets.

@aikar
Created August 13, 2010 20:06
Show Gist options
  • Save aikar/523476 to your computer and use it in GitHub Desktop.
Save aikar/523476 to your computer and use it in GitHub Desktop.
<?php
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 = $line2end['lat'];
$p3_y = $line2end['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;
// s2_x = p3_x - p2_x;
// s2_y = p3_y - p2_y;
$s3_x = (double) $p0_x - (double) $p2_x;
$s3_y = (double) $p0_y - (double) $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 + ($s * $s1_x),
'lng' => $p0_y + ($s * $s1_y)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment