Processing Speed of Rectangle Intersect Function

Feb 20, 2014 at 2:34am
I'm trying to find the fastest method to find whether or not two rectangles intersect in an effort to streamline my code. I've built this function into a rectangle class I created below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
	struct rectangle{
	rectangle() :X1(0), Y1(0), X2(0), Y2(0) {};
	rectangle(int X1, int Y1, int X2, int Y2)
		:X1(X1), Y1(Y1), X2(X2), Y2(Y2) {};
	int X1;
	int Y1;
	int X2;
	int Y2;
	int GetWidth() { return (X2-X1+1); }
	int GetHeight() { return (Y2-Y1+1); }
	bool CollidesWith(rectangle rec);
	rectangle OverlapRegion(rectangle rec);
	void Draw(ALLEGRO_BITMAP *Surface, ALLEGRO_COLOR Color, int Thickness = 1, bool Filled = false)
	{
		if (currentSurface != Surface) { currentSurface = Surface; al_set_target_bitmap(currentSurface); }
		if (Filled == false) al_draw_rectangle(X1, Y1, X2, Y2, Color, Thickness);
		if (Filled == true) al_draw_filled_rectangle(X1, Y1, X2, Y2, Color);
	}
};
	bool rectangle::CollidesWith(rectangle rec)
	{
	if (Y1 > rec.Y2) return false;
	if (Y2 < rec.Y1) return false;
	if (X1 > rec.X2) return false;
	if (X2 < rec.X1) return false;
	return true;
	}
	rectangle rectangle::OverlapRegion(rectangle rec)
	{
		rectangle regionRec(-1,-1,-1,-1);
		if (CollidesWith(rec) == false) return regionRec;
		regionRec.X1 = GetMin(X1, rec.X1) + abs(X1 - rec.X1);
		regionRec.Y1 = GetMin(Y1, rec.Y1) + abs(Y1 - rec.Y1);
		regionRec.X2 = GetMax(X2, rec.X2) - abs(X2 - rec.X2);
		regionRec.Y2 = GetMax(Y2, rec.Y2) - abs(Y2 - rec.Y2);
		return regionRec;
	}



So my question is which method of determining collision between two rectangles process faster...

This one...
1
2
3
4
5
6
7
8
bool rectangle::CollidesWith(rectangle rec)
	{
	if (Y1 > rec.Y2) return false;
	if (Y2 < rec.Y1) return false;
	if (X1 > rec.X2) return false;
	if (X2 < rec.X1) return false;
	return true;
	}


or this one...
1
2
3
4
5
6
bool rectangle::CollidesWith(rectangle rec)
	{
	if ((Y1 > rec.Y2) || (Y2 < rec.Y1) ||
	   (X1 > rec.X2) || (X2 < rec.X1)) return false;
	return true;
	}

...or do they execute at the same speed?
Feb 20, 2014 at 12:03pm
the first one might be possibly faster because it doesn't always need to check all conditions.

But if that has any measurable effect is another question...
Feb 20, 2014 at 5:40pm
@coder777 I think you forgot that short-circuit evaluation exists. Both code segments should be the same as:
1
2
3
4
bool rectangle::CollidesWith(rectangle rec)
{
    return !(Y1 > rec.Y2 || Y2 < rec.Y1 || X1 > rec.X2 || X2 < rec.X1);
}
Last edited on Feb 20, 2014 at 5:41pm
Feb 21, 2014 at 8:42am
L B wrote:
I think you forgot that short-circuit evaluation exists.
It doesn't matter for the execution speed. In the worst case (all condition are false) the second example adds the or operators. And you add another operator.

Not considering possible optimization of the compiler the first example should have at least the same or in worst case better performance (speed wise)
Topic archived. No new replies allowed.