4 Sided Shape drawing function
Re: 4 Sided Shape drawing function
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.
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
Re: 4 Sided Shape drawing function
Hi all, I tried to replicate Lux's thing and here is my solution: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)
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 );
}
}
Re: 4 Sided Shape drawing function
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
Re: 4 Sided Shape drawing function
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
Re: 4 Sided Shape drawing function
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
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
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk
Re: 4 Sided Shape drawing function
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.
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.
ʕ •ᴥ•ʔ