rectangles overlap
2010-10-03 23:03 wansishuang 阅读(187) 评论(0) 收藏 举报rectangles are given. As a struct having {bottomleft-x, bottomleft-y, topright-x, topright-y}
you are given two rectangles R1, R2? determine if they intersect
struct rect
{
int x;
int y;
int width;
int height;
};
bool valueInRange(int value, int min, int max)
{ return (value >= min) && (value <= max); }
bool rectOverlap(rect A, rect B)
{
bool xOverlap = valueInRange(A.x, B.x, B.x + B.width) ||
valueInRange(B.x, A.x, A.x + A.width);
bool yOverlap = valueInRange(A.y, B.y, B.y + B.height) ||
valueInRange(B.y, A.y, A.y + B.height);
return xOverlap && yOverlap;
}
浙公网安备 33010602011771号