Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // https://www.geeksforgeeks.org/find-two-rectangles-overlap/
- // 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
- //bool doOverlap(l1.x, l1.y, r1.x, r1.y, l2.x, l2.y, r2.x, r2.y)
- function rectilinear_count(int $k, int $l, int $m, int $n, int $p, int $q, int $r, int $s) {
- $rectA = ($m - $k) * ($n - $l);
- $rectB = ($r - $p) * ($s - $q);
- $totalRect = $rectA + $rectB;
- // If one rectangle is above other
- // if (l1.y < r2.y || l2.y < r1.y)
- // RectA.Left < RectB.Right && RectA.Right > RectB.Left && RectA.Top > RectB.Bottom && RectA.Bottom < RectB.Top
- $intersectionX1 = max($k, $p);
- $intersectionX2 = min($m, $r);
- $intersectionY1 = max($l, $q);
- $intersectionY2 = min($n, $s);
- $intersectionArea = ($intersectionX2 - $intersectionX1) * ($intersectionY2 - $intersectionY1);
- if ($intersectionArea > 0)
- $totalRect = $totalRect - $intersectionArea;
- if ($totalRect < 0)
- return -1;
- return $totalRect;
- }
- echo rectilinear_count(-4, 1, 2, 6, 0, -1, 4, 3);
- //echo rectilinear_count(-4, -4, 3, 4,-2,-1, 1, 2);
- //echo rectilinear_count(-4, -4, 3, 4, 4,-2, 7, 2);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement