<?php
/*
p0----------p1
| |
| x |
| |
p2----------p3
x|y|z
p0 = 0|0|0
p1 = 5|0|0
p2 = 0|5|0
p3 = 5|5|0
x = 3|3|0
*/
$a = array('x' => 0, 'y' => 0);
$b = array('x' => 5, 'y' => 0);
$c = array('x' => 0, 'y' => 5);
$d = array('x' => 5, 'y' => 5);
function determinant($vec1,$vec2){
return $vec1['x']*$vec2['y'] - $vec1['y']*$vec2['x'];
}
//one edge is a-b, the other is c-d
function edgeIntersection($a, $b, $c, $d){
$det = (double) determinant(
array(
'x' => $b['x'] - $a['x'],
'y' => $b['x'] - $a['x']
),
array(
'x' => $c['x'] - $d['x'],
'y' => $c['y'] - $d['y']
)
);
$t = (double) determinant(
array(
'x' => $c['x'] - $a['x'],
'y' => $c['x'] - $a['x']
),
array(
'x' => $c['x'] - $d['x'],
'y' => $c['y'] - $d['y']
)
)/$det;
$u =(double) determinant(
array(
'x' => $b['x'] - $a['x'],
'y' => $b['x'] - $a['x']
),
array(
'x' => $c['x'] - $a['x'],
'y' => $c['y'] - $a['y']
)
)/$det;
if (($t<0)||($u<0)||($t>1)||($u>1))return true;
return false;
return $a * (1-$t)+$t*$b;
}
if(edgeIntersection($a, $b, $c, $d) != true)
{
print('Die Objekte überlagern sich (-)'."\n");
$return = edgeIntersection($a, $b, $c, $d);
print_r($return);
}
?>