## Microsoft Interview Question

Software Engineer / DevelopersThere is a big problem in all the above answers, whether finding intersection or union, whatever you want to call it. It'll fail when none of the corners of the rectangles is inside the other.

for example

--------

| |

-----|--- |

| | | |

-----|--- |

--------

or even more difficult

----

| |

-------------

| | | |

-------------

| |

----

comments?

The fefinition of the "union of rectangles" is accurate in the 1st post:

this is the smallest rectangle which includes both our rectangles within it.

Given:

R1: (x1,y1),(x2,y2);

R2: (x3,y3),(x4,y4);

No assumptions about their orientations and coordinate system is needed (i.e. x1>x2 or vise versa), still we have:

left: min(x1,x2,x3,x4);

right: max(x1,x2,x3,x4);

top: min(y1,y2,y3,y4);

bottom: max(y1,y2,y3,y4).

Union: (left,bottom), (right, top)

.

Definition:

- srihari January 06, 2006===========

The union of two rectangles is the smallest single rectangle that completely covers both of the areas covered by the two given rectangles.

Let,

Co-ordinates of Rectangle 1 be,

left top (x1,y1),

right bottom (x2,y2)

Co-ordinates of Rectangle 2 be,

left top (x3,y3),

right bottom (x4,y4)

And x increases when going right an y increases when going up

Co-ordinate of the new union'ed rectangle,

left top (min(x1,x3),max(y1,y3))

right bottom (max(x2,x4),min(y2,y4))