4 Sided Shape drawing function

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: 4 Sided Shape drawing function

Post by chili » July 4th, 2012, 3:26 am

Recursion can solve certain classes of problems more easily and elegantly than loops. There are times where they make sense and are the best option.

However, if given the choice between recursion and a loop, with no clear winner between the two, always choose the loop. Recursion may be sexy, but it is also more difficult to maintain and debug than a loop. In some other cases, you may need to choose a loop, even though recursion would be much simpler, for reasons of increased performance (a well-written loop will always be faster) or because of stack overflows.

Coincidentally, in the next lesson I will be demonstrating recursion, so hopefully you'll have a better understanding of the concept after you've watched the video.
Chili

User avatar
Chrajdal
Posts: 22
Joined: December 15th, 2012, 2:18 pm

Re: 4 Sided Shape drawing function

Post by Chrajdal » January 11th, 2013, 5:49 pm

Cool, but have a look at this, its only the debug. Try to replicate it : -P
The function to draw the object has 10 lines of code (excluding drawline and filling function)
Hi all, I tried to replicate Lux's thing and here is my solution:

Code: Select all

void Game::DrawRotating_N_Corners_Shape( float x, float y,const int nCorners, int SideLenght, float Angle, int red, int green, int blue)
{
	float CenterPoint [ 2 ] = { x, y };
	float AngleBySide = 360 / nCorners;
	float radius;
	radius = SideLenght / ( 2 * sin ( ( AngleBySide / 2 ) * PI/180 ) ) ;
	float pointN [ 300 ] [ 2 ];

	pointN [ nCorners ] [ 0 ] = + CenterPoint [ 0 ] + radius * sin ( (90 - Angle)*PI/180 ) ;
	pointN [ nCorners ] [ 1 ] = + CenterPoint [ 1 ] + radius * sin ( (     Angle)*PI/180 ) ;

	for( int i = 0 ; i < nCorners ; i++)
	{
		
		Angle = Angle + AngleBySide;
		pointN [ i ] [ 0 ] = + CenterPoint [ 0 ] + radius * sin ( (90 - Angle)*PI/180 ) ;
		pointN [ i ] [ 1 ] = + CenterPoint [ 1 ] + radius * sin ( (     Angle)*PI/180 ) ;

	}

	for( int i = 0; i < nCorners ; i++)
	{

			if( pointN [ i ] [ 0 ] >= 0 && pointN [ i ] [ 0 ] < SCREENWIDTH &&
				pointN [ i ] [ 1 ] >= 0 && pointN [ i ] [ 1 ] < SCREENHEIGHT )

		{

			/********************** D R A W I N G   L I N E S **************************************/
		
				gfx.DrawLine( pointN [ i ][ 0 ], pointN [ i ][ 1 ], pointN [ i + 1 ][ 0 ], pointN [ i + 1 ][ 1 ], red, green, blue );
			
			/********************** D R A W I N G   L I N E S **************************************/
		
		}
			gfx.DrawLine ( pointN [ 0 ][ 0 ], pointN [ 0 ][ 1 ], pointN [ nCorners ][ 0 ], pointN [ nCorners ][ 1 ], red, green, blue );
	}
}
Unfortunately I still don't really know how to fill it with color, by I will try to figure it out. Lux, how did you solve that tihng? :)

User avatar
codinitup
Posts: 112
Joined: June 27th, 2012, 7:43 am

Re: 4 Sided Shape drawing function

Post by codinitup » January 12th, 2013, 3:41 am

Well i never managed how to figure out the shape drawing thing, but I think that a few while loops could satisfy you're question with an answer. The new question is would it be as simple as finding the distance between the sides of the figure and filling it, or would you have to use a different while loop for each shape. I'm not really sure if the distance between the sides would work very well, but there has to be some way like that. Possibly even an easier way than I'm thinking of.
MOOOOOO

User avatar
Chrajdal
Posts: 22
Joined: December 15th, 2012, 2:18 pm

Re: 4 Sided Shape drawing function

Post by Chrajdal » January 12th, 2013, 4:16 pm

I am thinking about a way how to do it and it's quite easy in my head, but I will write a code and see if that works first. I will post it later on :)

User avatar
codinitup
Posts: 112
Joined: June 27th, 2012, 7:43 am

Re: 4 Sided Shape drawing function

Post by codinitup » January 13th, 2013, 5:52 am

Sweet bro, I look forward to seeing it
MOOOOOO

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: 4 Sided Shape drawing function

Post by Asimov » January 13th, 2013, 11:23 am

Hi all,

Well the way I thought about doing it is this. Whatever shape you have it is can be made of triangles. In fact most game engines will turn a 3D model into triangles. You might save out a model with polygons, but then it is triangles.

So if you think about it, most shapes can be made with triangles. A square is two triangles. In fact even a circle can be built with triangles.

So the obvious thing would be to divide all your shapes into a bunch of triangles and then write one routine to fill a triangle.

By the way this is the latest thing I have been working on if you have java activated. It is a sheep made from about 2000 of these triangles
http://www.asimoventerprises.co.uk/inte ... erface.php
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: 4 Sided Shape drawing function

Post by LuX » January 14th, 2013, 3:42 pm

Hmmm-hm

For the filling I originally used a for loop, much like when drawing a disc, but instead of checking for distance I used some vector theory to see on what side of the line the point is, if it's on the right side of all the lines of the object, it has to be within the object.

I recently started working on my original game again, where I would draw the line of sight. If there's a wall in front, it would cast a "shadow" for which I used a similar method, but again, it's really slow. So what I did is create a function that makes an irregular "for" loop, where I only need to feed the corner points of a shape and it creates a list of max and min values in an array an makes a loop. I can then use this loop to fill quickly shapes that I need. For this case there might be better ways as well.
ʕ •ᴥ•ʔ

Post Reply