-2

I am trying to find whether two rectangles overlap each other or not. I have the following rectangles represented as [x1,x2] x [y1,y2]

Rect 1 = [0.0, 1.0] x [0.0, 1.0]
Rect 2 = [0.7, 1.2] x [0.9, 1.5]

I just need a pseudo code that I can implement to find the whether rectangles overlap each other or not.

phuclv
  • 32,499
  • 12
  • 130
  • 417

2 Answers2

0
  if ((Rect1.topLeft.x  >  Rect2.bottomRight.x) || (Rect1.bottomRight.x  <  Rect2.topLeft.x)  || (Rect1.topLeft.y > Rect2.bottomRight.y) || (   Rect1.bottomRight.y  <  Rect2.topLeft.y)):
            Then NOT OVERLAPPING
  else:
            OVERLAPPING
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Vishal Chhodwani Apr 19 '18 at 09:04
0

If your rectangle represented as [x1,x2] x [y1,y2]:

if (a.x1 > b.x2 || b.x1 > a.x2) return false; // check x-axis
if (a.y1 > b.y2 || b.y1 > a.y2) return false; // check y-axis
return true;

Condition a.x1 > b.x2 checks if the left border of a lies to the right of the right border of b, if that is true then rectangles don't overlap. Other three conditions are similar.

Yola
  • 17,545
  • 11
  • 60
  • 100