Microsoft Interview Question
Software Engineer in Testsfind distance betweeen the centers of two rectangles(let it be D)
find distance of centre of R2 from edge of R1(let it be s1)
" " " " " R1 from edge of R2(let it be s2)
if s1+s2!= D then rectangles overlap
For any Rectangle find its four corresponding lines using y-y1/(x-x1) = y2-y1/x2-x1
You got four lines for Rectangle A.
You got four points for rectangle B.
For A or B to intersect ( I am assuming this is what overlap means), the value of one point should be > 0 and < 0 for two parallel lines and < 0 and > 0 for the other two parallels lines. ( or the other way around)
In case not they would have the same sign. ( both > 0 and both < 0 or other way around).
Sorry for the bad sketch, I was trying to draw this
+---------------+
| |
| +------+ |
| | | |
| +------+ |
+---------------+
I am not sure what does overlap means here. Is it that its contained in an array or is it that it cuts the boundary or something.
In case of former( which is the figure you have drawn).
For each point of Rect A( inside Rect).
The product of (topline * bottomLine) < 0 and ( leftLine * RightLine < 0)
This should be true for all points.
Overlapping = Atleast one vertex of a rectangle contained within/on the vertices of the other.
My method - Check each vertex of each rectangle for containment within the other.
bool isContained( rect r, point p)
{
if( (p.x > r.left && p.x < r.right) //Check if x is within rect's bounds
&& (p.y < r.top && p.y > r.bottom) )
{
return true;
}
else
return false;
}
bool isOverlapped( rect bg_rect, rect fg_rect)
{
for each (vertex v in fg_rect)
{
if (isContained(bg_rect, v))
{
return true;
}
}
return false;
}
bool checkMutualOverlap(rect r1, rect r2)
{
return (isOverlapped(r1,r2) || isOverlapped(r2,r1));
}
doesn't work in the case
+-----------+
+-----|-----------|-------+
| | | |
+-----|-----------|-------+
+-----------+
as no vertex is contained inside other rectangle.
typedef struct
{
double x;
double y;
double width;
double height;
} rectangle;
bool overlap(rectangle *rect_a, rectangle *rect_b)
{
bool var = false;
if (rect_a->x<=rect_b->x && rect_a->y<=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_a->width &&
abs(rect_a->y-rect_b->y)<=rect_a->height)
var = true;
}
else if (rect_a->x>=rect_b->x && rect_a->y<=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_b->width &&
abs(rect_a->y-rect_b->y)<=rect_a->height)
var = true;
}
else if (rect_a->x>=rect_b->x && rect_a->y>=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_b->width &&
abs(rect_a->y-rect_b->y)<=rect_b->height)
var = true;
}
else if (rect_a->x<=rect_b->x && rect_a->y>=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_a->width &&
abs(rect_a->y-rect_b->y)<=rect_b->height)
var = true;
}
else
{
var = true;
}
return var;
}
Can someone help to debug/test/improve my logic? Thanks.
typedef struct
{
double x;
double y;
double width;
double height;
} rectangle;
bool overlap(rectangle *rect_a, rectangle *rect_b)
{
bool var = false;
if (rect_a->x<=rect_b->x && rect_a->y<=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_a->width &&
abs(rect_a->y-rect_b->y)<=rect_a->height)
var = true;
}
else if (rect_a->x>=rect_b->x && rect_a->y<=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_b->width &&
abs(rect_a->y-rect_b->y)<=rect_a->height)
var = true;
}
else if (rect_a->x>=rect_b->x && rect_a->y>=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_b->width &&
abs(rect_a->y-rect_b->y)<=rect_b->height)
var = true;
}
else if (rect_a->x<=rect_b->x && rect_a->y>=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_a->width &&
abs(rect_a->y-rect_b->y)<=rect_b->height)
var = true;
}
else
{
var = true;
}
return var;
}
Can someone help to debug/test/improve my logic? Thanks.
return ! ( (R1.right<=R2.left) | (R2.right<=R1.left) | (R1.bottom<=R2.top) | (R2.bottom<=R1.top) )
- N October 17, 2010