I'm trying to make a formula to find the intersection of two segments drawn in the PHP GD library.
function intersection ( $ax1, $ay1, $ax2, $ay2, $bx1, $by1, $bx2, $by2 ) {
$a1 = $ay2 - $ay1;
$b1 = $ax1 - $ax2;
$c1 = ( $a1 * $ax1 ) + ( $b1 * $ay1 );
$a2 = $by2 - $by1;
$b2 = $bx1 - $bx2;
$c2 = ( $a2 * $bx1 ) + ( $b2 * $by1 );
$x = ( $b2 * $c1 ) - ( $b1 * $c2 );
$y = ( $a1 * $c2 ) - ( $a2 * $c1 );
return array ( 'x' => $x, 'y' => $y );
}
This would be the function I have performed. But at the time of entering the data, it does not return the correct position of the intersection.
$cb = intersection( 0, 163, 123, 256, 0, 133, 93, 256 );
The returned value is: X: 343170; Y: 1315710; The coordinates are not correct. Where is the failure? A greeting and thanks in advance. PD: I'm not a good mathematician.