0

I'm working on finding maximum overlap rectangle.

I've tried using following lines of code but it returns with that rectangle is overlaping to other or not

public boolean isOverlapping(Rect r1, Rect r2) {
       if (r1.top < r2.top || r1.left > r2.left) {
           return false;
       }
       if (r1.width() < r2.width() || r1.height() > r2.height()) {
           return false;
       }
       return true;
   }

I expect output that rectangle 3 is most overlaping to given rectangle. Not list or number of rectangles that overlaping to given rectangle.

1 Answers1

1

A bit of pseudo code to get you going:

for each rect in Rectangle list
  overlap = compuateOverlap(rect, givenRect)

In other words: it is relatively easy to actually compute the overlap area for two rectangles. Just do that, and compare the results, and isolate the maximum.

In case you need more guidance how to compute that overlap, see this answer for some inspiration.

Or here, there you find even the exact formula to use to compute the overlap area of two rectangles!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • @DharmeshPrajapati Thanks for the comeback. And please consider to delete no longer required comments, so that future readers can focus on the technical content ;-) – GhostCat May 16 '19 at 06:13