function ccw ( $p0x, $p0y, $p1x, $p1y, $p2x, $p2y){
$dx1 = $p1x - $p0x;
$dy1 = $p1y - $p0y;
$dx2 = $p2x - $p0x;
$dy2 = $p2y - $p0y;
if( $dx1 * $dy2 > $dy1 * $dx2 ) return 1;
else if( $dx1 * $dy2 < $dy1 * $dx2) return -1;
else{
if( $dx1 * $dx2 < 0 || $dy1 * $dy2 < 0 ) return -1;
else if( $dx1 * $dx1 + $dy1 * $dy1 >= $dx2 * $dx2 + $dy2 * $dy2 ) return 0;
else return 1;
}
}
function intersect( $p1x, $p1y, $p2x, $p2y, $p3x, $p3y, $p4x, $p4y){
return( ccw ( $p1x, $p1y, $p2x, $p2y, $p3x, $p3y) * ccw ( $p1x, $p1y, $p2x, $p2y, $p4x, $p4y) <= 0 ) && ( ccw( $p3x, $p3y, $p4x, $p4y, $p1x, $p1y) * ccw( $p3x, $p3y, $p4x, $p4y, $p2x, $p2y) <= 0 );
}