Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Code get ridiculousy slow if I call a function from the main . . .

$
0
0
Hi guys,
I'm trying to implement an algorithm that given a rectangle and a number of polygons decided by the user, can recognize whether they are inside, outside or intersect the rectangle and provides the number of said polygons.
I coded an algorithm and it works, but I noticed that after right after the compilation it takes at least 20seconds to start ( this won't happen if I start it a second, third or other time ).
Trying to figure out what was slowing my code so much, I noticed that the program runs instantly if I delete the call to the function that determines polygon's position in relation to the rectangle.
I tried to find something wrong but found nothing
Here it is
// struct used in the function
struct Polygon
{
	int ** points;
	int vertices;
};
// inside, outside and over are the number of polygons that are inside, outside or intersect the rectangle,
// they're initialized to 0 in the main.
// down_side, up_side are the y_coordinate of the two horizontals sides.
// left_side, right_side are the x_coordinate of the two vertical sides.
void checkPolygons( Polygon * polygon, int & inside, int & outside, int & over, unsigned int polygons, const unsigned int down_side, const unsigned int up_side, const unsigned int left_side, const unsigned int right_side )
{
	for ( unsigned int pol = 0; pol < polygons; ++pol )
	{
		unsigned int insideVertices = 0;
		unsigned int vertices = polygon[ pol ].vertices;

		for ( unsigned int point = 0; point < vertices; ++point )
		{
			unsigned int x_coordinate = polygon[ pol ].points[ point ][ 0 ];
			unsigned int y_coordinate = polygon[ pol ].points[ point ][ 1 ];

			if ( ( x_coordinate <= right_side ) and ( x_coordinate >= left_side ) and ( y_coordinate <= up_side ) and ( y_coordinate >= down_side ) )
			{
				insideVertices++;
			}
				
		}
				
		if ( insideVertices == 0 )
			++outside;
		else if ( insideVertices == vertices ) 
			++inside;
		else
			++over;
	}
}

What do you thing it's wrong with it?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>