查找边缘,一个矩形触及另一个矩形矩形、边缘

由网友(棒棒糖的初恋)分享简介:给定边枚举像这样的:none, top, left, bottom, right,由于2矩形,我怎么能找到矩形A的矩形B的交叉哪些优势?我并不需要知道其中B边缘打中甲的边缘,我只需要知道其中的一个使得b击中边。 Given 2 rectangles, how could I find which edge of r...

给定边枚举像这样的:

none, top, left, bottom, right,

由于2矩形,我怎么能找到矩形A的矩形B的交叉哪些优势?我并不需要知道其中B边缘打中甲的边缘,我只需要知道其中的一个使得b击中边。

Given 2 rectangles, how could I find which edge of rectangle A that rectangle B is intersecting? I do not need to know which edge of B hit an edge of A, I just need to know which edge of A that B hit.

我发现这个算法,但它并没有返回的具体优势:

I found this algorithm but it does not return the specific edge:

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,我怎么可能让一个函数我只需要调用一次?

My above algorithm checks each edge one by one, given TopLeftA TopLeftB BottomRightA BottomRightB, how could I make a function I only need to call once?

感谢

推荐答案

假设你已经使用 edgeIntersection 来确定发生了交集,那么:

Assuming you've used edgeIntersection to determine that an intersection has occurred, then:

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;
阅读全文

相关推荐

最新文章