Jump to content
  • Advertisement
Sign in to follow this  
P0jahn

Quickest way to iterate through a rectangles lines?

This topic is 2031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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
}

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!