Advertisement
Guest User

Untitled

a guest
Aug 25th, 2009
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.12 KB | None | 0 0
  1. <?php
  2.  
  3. function zoverlaps($first_start, $first_end, $second_start, $second_end) {
  4.     return !(
  5.         ($first_start < $second_start && $first_end < $second_start)
  6.         ||
  7.         ($first_start > $second_end && $first_end > $second_end)
  8.     );
  9. }
  10.  
  11. function overlaps($a,$b,$c,$d) {return !((($a>$b?$b:$a)<($c>$d?$d:$c)&&($b>$a?$b:$a)<($c>$d?$d:$c))||(($a>$b?$b:$a)>($d>$c?$d:$c)&&($b>$a?$b:$a)>($d>$c?$d:$c)));}
  12.  
  13. $tests = array(
  14.     array( 'data' => array(10,20,30,40), 'result' => FALSE ),
  15.     array( 'data' => array(30,40,10,20), 'result' => FALSE ),
  16.     array( 'data' => array(10,40,20,30), 'result' => TRUE ),
  17.     array( 'data' => array(20,30,10,40), 'result' => TRUE ),
  18.     array( 'data' => array(10,30,20,40), 'result' => TRUE ),
  19.     array( 'data' => array(20,40,10,30), 'result' => TRUE ),
  20. );
  21.  
  22.  
  23. foreach ($tests as $test) {
  24.     $foo = overlaps(
  25.         $test['data'][0],
  26.         $test['data'][1],
  27.         $test['data'][2],
  28.         $test['data'][3]
  29.     );
  30.  
  31.     echo ($foo == $test['result'] ? "ok\n" : "FAIL\n");
  32.  
  33.     $foo = overlaps(
  34.         $test['data'][1],
  35.         $test['data'][0],
  36.         $test['data'][3],
  37.         $test['data'][2]
  38.     );
  39.  
  40.     echo ($foo == $test['result'] ? "ok\n" : "FAIL\n");
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement