嗨,我尝试了一个问题来检查两个矩形是否相交如果矩形平行于 x 轴,我已经编写了代码
struct point
{
int x;
int y;
};
struct rect
{
struct point left;
struct point right;
};
//1 - intersection
// 0- no intersection
int rectintersectioncheck(struct rect r1,struct rect r2)
{
int x_check = (r1.left.x > r2.right.x || r2.left.x > r1.right.x);
int y_check = (r1.right.y > r2.left.y || r2.right.y > r1.left.y);
if(x_check && y_check )
{
return 0;
}
return 1;
}
它在这种情况下工作正常,但我对算法感到困惑,因为矩形不平行于 x 轴,因为只有左上角,右下角点,请帮忙?