Given two simple, rectangles:
class Rectangle
{
int x;
int y;
int width;
int height;
}
Rectangle a;
Rectangle b;
and the following enumeration:
[Flags]
enum Edges
{
None,
Top,
Bottom,
Left,
Right,
Inside,
}
What is the quickest way to detect the edges on rectangle a which are collided with by rectangle b?
Edges e = EdgeDetect(a, b);
First of all, you have to defines explicitly values of your enum in order to have flags working correctly. In you case
Left == Top + Bottom + None. Here is a possible declaration :Next, a possible implementation of edge collision detection. Note that I use the builtin
System.Drawing.Rectangleinstead of rewriting the class. The immediate advantage is the availability of theIntersectmethod. :Here is a set of tests that validates this implementation :