Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if two rectangles can be combined into a single rectangle

I'm looking for an algorithm that takes two rectangles defined by (xa1,ya1,xa2,ya2) and (xb1,yb1,xb2,yb2), checks if they can be combined into a single rectangle and if they can, returns the new rectangle. An example:

xa1=0,ya1=0,xa2=320,ya2=119
xb1=0,yb1=120,xb2=320,yb2=239

These two rectangles can be combined into the following rectangle:

xc1=0,yc1=0,xc2=320,yc2=240

How would you implement such an algorithm? Thanks!

like image 990
Andy Avatar asked Feb 23 '23 14:02

Andy


1 Answers

I'd draw the following pictures and would write it down as algorithm:

...xxxxxxx       xxxxxxx....
.  x  .  x       x   . x   .
.  x  .  x       x   . x   .
...xxxxxxx       xxxxxxx....

xxxxxxx          .......
x     x          .     .
x.....x          xxxxxxx
xxxxxxx          x.....x
.     .          x     x
.......          xxxxxxx

..........
.        .
. xxxx   .
. x  x   .
. x  x   .
. xxxx   .
..........

xxxxxxxxxxxxxx
x            x
x   .......  x
x   .     .  x
x   .     .  x
x   .......  x
x            x
xxxxxxxxxxxxxx

Check out for corner cases!

like image 116
Donotalo Avatar answered Apr 30 '23 01:04

Donotalo