给出这样的边缘枚举:
none, top, left, bottom, right,
给定2个矩形,我怎样才能找到矩形B的哪个边缘相交?我不需要知道B的哪个边缘击中A的边缘,我只需要知道B击中的A的哪个边缘。
我找到了这个算法,但它没有返回特定的边缘:
bool edgeIntersection( vector2f a, vector2f b, DOUBLEPOINT c, DOUBLEPOINT d )
{
//one edge is a-b, the other is c-d
vector2f bminusa;
vector2f cminusa;
vector2f cminusd;
bminusa.x = b.x - a.x;
bminusa.y = b.y - a.y;
cminusa.x = c.point[0] - a.x;
cminusa.y = c.point[1] - a.y;
cminusd.x = c.point[0] - d.point[0];
cminusd.y = c.point[1] - d.point[1];
double det=determinant(bminusa,cminusd);
double t=determinant(cminusa,cminusd)/det;
double u=determinant(bminusa,cminusa)/det;
if ((t<0)||(u<0)||(t>1)||(u>1))return false;
return true;
}
我的上述算法逐一检查每个边缘 TopLeftA TopLeftB BottomRightA BottomRightB,我怎样才能创建一个我只需要调用一次的函数?
由于
答案 0 :(得分:1)
假设您已使用edgeIntersection
确定交叉点已发生,则:
if (b.x < a.x) return left;
if (b.y < a.y) return top;
if (b.x+b.width > a.x+a.width) return right;
return bottom;