Jump to content

  • Log In with Google      Sign In   
  • Create Account

Quickest way to iterate through a rectangles lines?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 P0jahn   Members   -  Reputation: 272

Like
0Likes
Like

Posted 22 May 2013 - 02:48 PM

I have a rectangle, with an x and y coordinate which represent the top-left corner of the rectangle. I also have the width and the height.

 

I need a piece of code that can iterate through all the four lines of the rectangle fast.

 

My crap code works but have 4 for-loops o_0



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8601

Like
0Likes
Like

Posted 22 May 2013 - 03:01 PM

Just emit the four corners by hand: (x, y), (x+w, y), (x+w, y+h) and (x, y+h). You already know the shape, the origin and the size, and the number of points to calculate is so trivial that hand calculation is easy enough.



#3 P0jahn   Members   -  Reputation: 272

Like
0Likes
Like

Posted 22 May 2013 - 03:46 PM

yeah, but that would require four for-loops, which is inefficient. Performance is critical.



#4 radioteeth   Prime Members   -  Reputation: 1137

Like
0Likes
Like

Posted 22 May 2013 - 04:25 PM

are you trying to do something like this ?

 

#define MAX_POLYVERTS    4
#define MASK_POLYVERTS    (MAX_POLYVERTS - 1)

int rect_pts[MAX_POLYVERTS][2] = { {x, y}, {x+w, y}, {x+w, y+h}, {x, y+h}};
int a, b;

for(a = 0; a <= MAX_POLYVERTS; a++)
{
    point1_x = rect_pts[a][0];
    point1_y = rect_pts[a][1];

    // get coords of next point, use bitmask to 'wrap' value back to zero
    point2_x = rect_pts[(a+1)&MASK_POLYVERTS][0];
    point2_y = rect_pts[(a+1)&MASK_POLYVERTS][1];

    // ... do something here with points 1 and 2
}



#5 Khatharr   Crossbones+   -  Reputation: 3038

Like
0Likes
Like

Posted 22 May 2013 - 04:42 PM

Can you post what you have so that we have a clearer idea of what you're talking about? I'd tend to assume that you're trying to iterate through pixel positions, but...


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#6 Brother Bob   Moderators   -  Reputation: 8601

Like
0Likes
Like

Posted 22 May 2013 - 04:44 PM


yeah, but that would require four for-loops, which is inefficient. Performance is critical.


Emitting four trivial vertices requires zero loops. Even radioteeth's solution has only one loop, an that loop can easily be expanded and eliminated alltogether if you don't want it.

#7 L. Spiro   Crossbones+   -  Reputation: 14257

Like
0Likes
Like

Posted 22 May 2013 - 06:35 PM

I think he is trying to draw the rectangle dot-by-dot.

Why not just draw the lines for each side?  Why draw the dots manually?

 

 

L. Spiro


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#8 P0jahn   Members   -  Reputation: 272

Like
0Likes
Like

Posted 23 May 2013 - 07:51 AM

I am checking if the rectangle is intersecting with solid tile.

 

Here is my previous code(it was two for loops instead of one:

		int x = entity.x;
		int y = entity.y;
		int width =  entity.width;
		int height = entity.height;
		
		//Check the top and bottom line
		for(int i = 0; i < width; i++)
			if(foo(i, y) || foo(i, y + height))
				...
			
                //Check left and right line
		for(int i = 0; i < height; i++)
			if(foo(x, i) || foo(x, i + width))
				...

Edited by P0jahn, 23 May 2013 - 07:52 AM.


#9 Jompe_71   Members   -  Reputation: 155

Like
0Likes
Like

Posted 23 May 2013 - 09:18 AM

I'm sure that whatever language you are using (your code snippet can be several) there is a Rectangle implementation in the core API which has an intersects( anotherRectangle ) method?



#10 DT....   Members   -  Reputation: 487

Like
0Likes
Like

Posted 23 May 2013 - 11:05 AM

I am checking if the rectangle is intersecting with solid tile.

 

Here is my previous code(it was two for loops instead of one:

		int x = entity.x;
		int y = entity.y;
		int width =  entity.width;
		int height = entity.height;
		
		//Check the top and bottom line
		for(int i = 0; i < width; i++)
			if(foo(i, y) || foo(i, y + height))
				...
			
                //Check left and right line
		for(int i = 0; i < height; i++)
			if(foo(x, i) || foo(x, i + width))
				...

 

 

AABB vs AABB :

 

http://www.miguelcasillas.com/?p=30



#11 Hawkblood   Members   -  Reputation: 725

Like
1Likes
Like

Posted 23 May 2013 - 03:13 PM

I am checking if the rectangle is intersecting with solid tile.

Are your "tiles" in a rectangular shape? If so, you don't need to test the pixils, you can use math to see if they are intersecting.

for (int t=0;t<NUMBER_OF_TILES;t++){
	int px=entity.x-tile[t].x;
	int py=entity.y-tile[t].y;
	bool xCollide=false,yCollide=false;
	if (entity.sizeX<=tile.sizeX){
		if (((px>=0)&&(px<tile[t].sizeX))||((px+entity.sizeX>=0)&&(px+entity.sizeX<tile[t].sizeX))) xCollide=true;
	}
	else{
		//if the entity size is greater than the tile size, px needs to be reversed
		px=tile[t].x-entity.x;
		if (((px>=0)&&(px<entity.sizeX))||((px+tile[t].sizeX>=0)&&(px+tile[t].sizeX<entity.sizeX))) xCollide=true;
	}
	if (entity.sizeY<=tile[t].sizeY){
		if (((py>=0)&&(py<tile[t].sizeY))||((py+entity.sizeY>=0)&&(py+entity.sizeY<tile[t].sizeY))) yCollide=true;
	}
	else{
		//if the entity size is greater than the tile size, py needs to be reversed
		py=tile[t].y-entity.y;
		if (((py>=0)&&(py<entity.sizeY))||((py+tile[t].sizeY>=0)&&(py+tile[t].sizeY<entity.sizeY))) yCollide=true;
	}
	if (xCollide && yCollide){//they are colliding
		//do something......
	}
}

This code checks the bounds of x and y values to determine whether the rectangle of the "entity" is colliding with the rectangle of the "tile". If it collides in both x and y, then at least some part of the entity is colliding with that tile.

 

If your "entity" is not solid, then you can just check the linear bounds of x and y seperately


Edited by Hawkblood, 23 May 2013 - 03:15 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS