如何判断两个矩形是否存在交集

typedef struct Rect {
    int left;
    int top;
    int right;
    int bottom;
} Rect;

int min(int a, int b) {
    return (a<b) ? a : b;
}
int max(int a, int b) {
    return (a>b) ? a : b;
}

bool isEmpty(const Rect* r) {
    return ((r->right-r->left)<=0) || ((r->bottom-r->top)<=0);
}

bool intersect(const Rect* r1, const Rect* r2, Rect* result)
{
    result->left    = max(r1->left, r2->left);
    result->top     = max(r1->top, r2->top);
    result->right   = min(r1->right, r2->right);
    result->bottom  = min(r1->bottom, r2->bottom);

    return !(isEmpty(result));
}

你可能感兴趣的:(如何判断两个矩形是否存在交集)