Iteration and Recursion.

Procedural Animation
Iteration and Recursion
Procedural modeling
•
Iteration and Recursion are two useful ways to
create complicated geometry.
•
•
Iteration means “repeat”.
Recursion means “one of the steps in a
procedure repeats the whole procedure itself
(usually with different scale)”.
•
Famous examples of “recursion”:
1. Fractal geometry
2. L-system
2
Fractals geometry
• Let’s start from this simple example:
– “Divide a straight line into three, and pull up
the middle part”
– Repeat the step for many times
3
A simple example
• Same rules, but use a different “starting
shape”:
4
Self-similarity
• Assume that you can repeat the steps infinite
number of times. If you zoom-in a small part of
the line, its pattern looks like the pattern in a
larger scale.
• Self-similarity
– A fractal looks similar to itself on a variety of scales.
– E.g.
• A little piece of mountain looks like a bigger piece of a
mountain.
• A smaller branch in a tree looks like a larger branch.
5
Create fractals using software
1. Choose a basic shape.
2. Create several copies, and then apply different
transformations to each copy.
3. Repeat step (2) on each of the created copy.
•
•
“Transformations” can be scale, rotate, and translation.
Every iteration is also called “one level of recursion”.
6
Constructing fractals
• We start from a simple shape:
Create 4 copies,
each transform
a little bit
7
Constructing fractals
• Repeat the process several times:
8
Constructing fractals
• Another example:
9
L-Systems
• Idea of L-systems is similar to fractal : model is
replaced by itself to obtain a finer model.
• L-systems further “formalize” the process: use
symbols to specify how to construct the
geometry.
• Works best for things like plants:
– Start with a stem
– Replace it with stem + branches
– Replace some stem with more stem + branches, and
so on.
10
Turtle Graphics
• L-system link symbols to drawing commands
using “Turtle Graphics”.
• Basic 2-D turtle commands: “F”, “f”, “+” and “-”
 “F” – Draw forward one unit
 “f” – Move forward (without draw) one unit
 “+” – Rotate turtle right (or turn clockwise)
 “–” – Rotate turtle left (or turn anti-clockwise)
• (Step size and rotation angle are variables that
can be set.)
11
Turtle Graphics
• Example: F+F--F+F (and set rotation angle=60)
12
L-system Rules
•
L-system use symbol substitution to create
recursion:
–
–
•
Example: F=F+F--F+F
Means replacing every F by F+F--F+F in each
iteration.
Note that all symbols are replaced
simultaneously.
1. F
2. F+F--F+F
3. F+F--F+F + F+F--F+F -- F+F--F+F + F+F--F+F
13
L-system rules
-+,- = 60o
F
F+F--F+F
F+F--F+F +
+ F+F--F+F
14
L-system Branching
• We can use F,f,+,- to
make branch – but quite
troublesome.
• A better way is to use
“[“ and “]” to model
branching structures:
– “[” – create a branch
– “]” – end the branch
F+F+F----f+F----f+…
vs.
F[+F[+F][-F]][-F[+F][-F]]
15
Branching Example
F = F[+F]F[-F]F
16
Stochastic L-Systems
• Conditional firing of rules, to create non-regular,
stochastic result.
• Example:
–
–
–
F = F[+F]F[-F]F : .33 (the chance to pick this rule is 0.33)
F = F[+F]F : .33
F = F[-F]F : .33
17
3-D Turtle Graphics
•
•
•
•
•
•
“+” – turn right
“-” – turn left
“&” – pitch up
“^” – pitch down
“\” – roll clockwise
“/” – roll anti-clockwise
18
3D example
F = F[^F][////^F][\\\\^F]
19
Using different symbols
Besides using F, by using different symbols
we can create more variations. Example:
•Start from B
•A = AFAF
•B = AF[-BF]AFAF[+BF]
You may think that
different symbol is
different kinds of
seed – it will grow
up to different shape
in the future.
20
Adding geometry
• Lastly, some software
allows you to render tube
for the branches, or even
put flower geometry at
the location specify by
the rules – suitable for
creating plants.
21