tags:

views:

46

answers:

1

given an edge enum such as this:

none, top, left, bottom, right,

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;
}

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?

Thanks

+1  A: 

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;
Oli Charlesworth
My above algorithm checks each edge one by one, givenTopLeftA TopLeftB BottomRightA BottomRightB, how could I make a function I only need to call once?
Milo
@Milo Have a function that calls all 4 functions, and returns the first one that succeeds.
PigBen
yea but 4 * 4 = 16 times, the above function needs to be called 16 times, is there a way to reduce this?
Milo