Skip to content

Instantly share code, notes, and snippets.

@aikar
Created August 14, 2010 04:13
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/523964 to your computer and use it in GitHub Desktop.
Save aikar/523964 to your computer and use it in GitHub Desktop.
<?php
function intersectPoint($line1start, $line1end, $line2start, $line2end)
{
bcscale(20);
$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'];
AppCommUtility::echof("===============\n".'Checking intersect' . print_r(func_get_args(),true));
$den =
bcsub(
bcmul(bcsub($y4,$y3), bcsub($x2,$x1)),
bcmul(bcsub($x4,$x3), bcsub($y2,$y1))
);
if((double) $den == 0)
{
AppCommUtility::echof("den == 0");
return null;
}
$ua1 = bcmul(bcsub($x4, $x3), bcsub($y1, $y3));
$ua2 = bcmul(bcsub($y4, $y3), bcsub($x1, $x3));
$num_ua =
bcsub(
$ua1,
$ua2
);
if((double) $num_ua == 0)
{
AppCommUtility::echof("num_ua == 0");
}
$ua =
bcdiv(
$num_ua,
$den
);
$ub1 = bcmul(bcsub($x2,$x1), bcsub($y1, $y3));
$ub2 = bcmul(bcsub($y2, $y1), bcsub($x1, $x3));
$num_ub =
bcsub(
$ub1,
$ub2
);
if((double) $num_ub == 0)
{
AppCommUtility::echof("num_ub == 0");
}
$ub =
bcdiv(
$num_ub,
$den
);
AppCommUtility::echof("ua $ua <ua1 $ua1, ua2 $ua2> ub $ub <ub1 $ub1, ub2 $ub2> div $den");
if($ua >= 0 && $ua <= 1 && $ub >= 0 && $ub <= 1)
{
return array(
'lat' => (double) bcadd($x1, bcmul($ua, bcsub($x2, $x1))),
'lng' => (double) bcadd($y1, bcmul($ua, bcsub($y2, $y1)))
);
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment