Mel
|
IntroductionThis tutorial addresses the issue of writing recursive procedures. A procedure is recursive when it calls or invokes itself. As a simple example consider the following procedure. proc foo(){ sphere; foo(); // call ourself }
The color coded sequence shown below indicates what happens when
the procedure is executed. Each time |
|
Terminating RecursionRecursive procedures must always be given a way of stopping the recursion process. Before they recursively call themself they test if a terminal condition is true or false. For example. proc foo(int $depth) { sphere; // terminate the recursion... if($depth == 0) return; foo($depth - 1); // call ourself }
In a mel script this new version of the for(int $n = 0; $n < 5; $n++) sphere; So why bother with recursion? Why not use a for-loop to repeat a particular action - such as creating a sphere? |
Why Use Recursion?
Recursion is particulary helpful when modeling branching and/or
nested structures that consist of self-similar shapes.
For example, the space filling Hilbert curve shown in fig 1 was created
with a recursive procedure.
Example 1 - knobby sphereThe cluster of spheres in figure 1 was created with the procedure shown in listing 1.
Listing 1 (spheres.mel)
Line 7 includes the -esw flag (endSweep) flag so that hemi-spheres
are produced. In addition the y coordinte is set to 0.0 to ensure the
top edges of the spheres are aligned to the x-z plane. In this way
it is easy to see that some "children" spheres are enclosed within
the inner "parent" spheres.
|
© 2002- Malcolm Kesson. All rights reserved.