Advertisement
mhyusuf

rectilinear_count

Oct 16th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.30 KB | None | 0 0
  1. <?php
  2. // https://www.geeksforgeeks.org/find-two-rectangles-overlap/
  3. // https://www.glassdoor.com/Interview/1-Find-coordinates-of-intersection-A-rectangle-is-called-rectilinear-if-its-edges-are-all-parallel-to-coo-QTN_1272094.htm
  4.  
  5. //bool doOverlap(l1.x, l1.y, r1.x, r1.y, l2.x, l2.y, r2.x, r2.y)
  6. function rectilinear_count(int $k, int $l, int $m, int $n, int $p, int $q, int $r, int $s) {
  7.     $rectA = ($m - $k) * ($n - $l);
  8.     $rectB = ($r - $p) * ($s - $q);
  9.     $totalRect = $rectA + $rectB;
  10.    
  11.     // If one rectangle is above other
  12.    
  13.     // if (l1.y < r2.y || l2.y < r1.y)
  14.     //  RectA.Left < RectB.Right && RectA.Right > RectB.Left && RectA.Top > RectB.Bottom && RectA.Bottom < RectB.Top
  15.  
  16.         $intersectionX1 = max($k, $p);
  17.         $intersectionX2 = min($m, $r);
  18.         $intersectionY1 = max($l, $q);
  19.         $intersectionY2 = min($n, $s);
  20.         $intersectionArea = ($intersectionX2 - $intersectionX1) * ($intersectionY2 - $intersectionY1);
  21.    
  22.     if ($intersectionArea > 0)
  23.         $totalRect = $totalRect - $intersectionArea;
  24.    
  25.    
  26.     if ($totalRect < 0)
  27.         return -1;
  28.    
  29.    
  30.     return $totalRect;
  31. }
  32.  
  33. echo rectilinear_count(-4, 1, 2, 6, 0, -1, 4, 3);
  34. //echo rectilinear_count(-4, -4, 3, 4,-2,-1, 1, 2);
  35. //echo rectilinear_count(-4, -4, 3, 4, 4,-2, 7, 2);
  36.  
  37. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement