Skip to content

Instantly share code, notes, and snippets.

@aikar aikar/gist:523873
Created Aug 14, 2010

Embed
What would you like to do?
<?php
function intersectPoint($line1start, $line1end, $line2start, $line2end)
//($p0_x, $p0_y, $p1_x, $p1_y, $p2_x, $p2_y, $p3_x, $p3_y)
{
bcscale(8);
$p0_x = (string) $line1start['lat'];
$p0_y = (string) $line1start['lng'];
$p1_x = (string) $line1end['lat'];
$p1_y = (string) $line1end['lng'];
$p2_x = (string) $line2start['lat'];
$p2_y = (string) $line2start['lng'];
$p3_x = (string) $line1end['lat'];
$p3_y = (string) $line1end['lng'];
AppCommUtility::echof("p0_x $p0_x p0_y $p0_y p1_x $p1_x p1_y $p1_y p2_x $p2_x p2_y $p2_y p3_x $p3_x p3_y $p3_y");
$s1_x = bcsub($p1_x, $p0_x);
$s1_y = bcsub($p1_y, $p0_y);
// s1_x = p1_x - p0_x;
// s1_y = p1_y - p0_y;
$s2_x = bcsub($p3_x, $p2_x);
$s2_y = bcsub($p3_y, $p2_y);
// s2_x = p3_x - p2_x;
// s2_y = p3_y - p2_y;
$s3_x = bcsub($p0_x, $p2_x);
$s3_y = bcsub($p0_y, $p2_y);
$div = bcadd(
bcmul(-$s2_x, $s1_y),
bcmul($s1_x, $s2_y)
);
AppCommUtility::echof("div: %s - -s2_x: %s s1_y: %s s1_x: %s s2_y: %s",
$div, -$s2_x,
$s1_y, $s1_x, $s2_y);
if((double) $div === 0)
{
return null;
}
$s = bcdiv(
bcadd(
bcmul(-$s1_y,$s3_x),
bcmul($s1_x, $s3_y)
),
$div
);
$t = bcdiv(
bcsub(
bcmul( $s2_x,$s3_y),
bcmul($s2_y, $s3_x)
),
$div
);
$s_c1 = !(bccomp($s,0) === -1);
$s_c2 = !(bccomp($s,1) === 1);
$t_c1 = !(bccomp($t,0) === -1);
$t_c2 = !(bccomp($t,1) === 1);
AppCommUtility::echof("s1 %d s2 %d s3 %d s4 %d", $s_c1, $s_c2, $t_c1, $t_c2);
if ($s_c1 && $s_c2 && $t_c1 && $t_c2)
{
AppCommUtility::echof(" FUNC RETURNED TRUE $s >= 0 && $s <= 1 && $t >= 0 && $t <= 1");
// Collision detected
return array(
'lat' => (double) bcadd($p0_x, bcmul($t, $s1_x)),
'lng' => (double) bcadd($p0_y, bcmul($t, $s1_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'];
// default to no collision
$s = -1;
$t = -1;
$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;
$denom = (double) (-$s2_x * $s1_y + $s1_x * $s2_y));
if ($denom == 0) // colinear or parallel
{
if ((-$s1_y * $s3_x + $s1_x * $s3_y) == 0) // colinear
{
$s = 0;
$t = 0;
}
}
else // should come here most often
{
$s = (double) ((double)(-$s1_y * $s3_x + $s1_x * $s3_y) / $denom;
$t = (double) ((double)( $s2_x * $s3_y - $s2_y * $s3_x) / $denom;
}
// 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.