sierpinski carpet recursive::Simply put, recursion is where a function calls itself a number of times until a certain condition is met sierpinski carpet recursive
sierpinski carpet recursive
sierpinski carpet recursive
sierpinski carpet recursive::Simply put, recursion is where a function calls itself a number of times until a certain condition is met.With every new call, it will change one or more of the functions parameters depending on their current values.
The factorial of 5 is 1 x 2 x 3 x 4 x 5, which equals 120.
So how does it work?
When we examine what is going on we can see that: hopefully, you are beginning to understand the process.
However, if you do not put in a condition, the function will continually call itself forever and blitz will give you a stack overflow error.
It is advisable to put in a threshold so the function will only call itself a maximum number of times.
The sierpinski carpet is a fractal named after the polish mathematician: now, you may be thinking that this looks quite complicated.
However, if we break it down in to easy steps, you will see that it is actually a simple repeating pattern a perfect candidate for recursion.
Start by drawing a filled white square the size of the carpet.
Divide this square into nine equal squares and fill the middle one black.
Repeat step 2 on each of the eight remaining squares.
The diagram below shows 3 iterations.
The pattern grows exponentially at each stage.
So, we can see on diagram 1 that we have divided the square into nine squares and filled the middle one.
Now, in the third iteration, a further eight squares has been created for each of the previous eight squares drawn in the previous iteration.
Hopefully, you see how this works, and why this is a perfect job for recursion.
It does this by dividing the size by 3.
Notice how the offset value is used to draw the rectangle.
Once this is done, we need to check our threshold value.
If we have reached our threshold then we have finished.
Otherwise, we set our new threshold value.
Then, we simply call the function 8 times using the new size and new threshold values.
We also have to specify different x and y values for each of the 8 remaining squares.
As explained above, there must be a finite number of iterations to your recursion routine so it may be able to cope with what you want it to do.
For example, it is relatively easy to do a flood fill routine using recursion, but on high resolution bitmaps you may get a stack overflow error due to the sheer number of times the function calls itself.
I would not recommend having a threshold of more than a few thousand.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home