Skip to content

Instantly share code, notes, and snippets.

@aikar
Created August 14, 2010 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aikar/523938 to your computer and use it in GitHub Desktop.
Save aikar/523938 to your computer and use it in GitHub Desktop.
<?php
function intersectPoint($line1start, $line1end, $line2start, $line2end)
{
$x1 = (double) $line1start['lat'];
$y1 = (double) $line1start['lng'];
$x2 = (double) $line1end['lat'];
$y2 = (double) $line1end['lng'];
$x3 = (double) $line2start['lat'];
$y3 = (double) $line2start['lng'];
$x4 = (double) $line2end['lat'];
$y4 = (double) $line2end['lng'];
$div = (($y4 - $y3) * ($x2 - $x1)) - (($x4 - $x3) *($y2 - $y1));
if(!$div) return null;
$ua = ((($x4 - $x3) * ($y1 - $y3)) - (($y4 - $y3) * ($x1 - $x3))) / $div;
$ub = ((($x2 - $x1) * ($y1 - $y3)) - (($y2 - $y1) * ($x1 - $x3))) / $div;
if($ua >= 0 && $ua <= 1 && $ub >= 0 && $ub <= 1)
{
AppCommUtility::echof("ua $ua ub $ub div $div");
return array(
'lat' => $x1 + ($ua * ($x2 - $x1)),
'lng' => $y1 + ($ua * ($y2 - $y1))
);
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment