Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 76

Prodi Informatika

Fakultas Teknologi Industri


Universitas Atma Jaya Yogyakarta

Grafika Komputer
Pertemuan 8

Prof. Suyoto
This Week
• Approaches to Infinity
–Fractals and Self-Similarity
–Iterative Function Systems
–Lindenmayer Systems
–Curves
–Natural Images (trees, landscapes..)
• Mandelbrot and Julia Sets
• Generating realistic landscapes
Introduction
What is a Fractal?
– A fractal is an image with self-similar
properties produced by recursive or iterative
algorithmic means.
“anything which has a substantial measure of exact or statistical self-similarity”

– Mandelbrot coined the term from the latin


fractus meaning “fragmented” or “irregular”
Introduction
Why use fractals in Computer Graphics?
• Most real world objects are inherently
smooth.
• Most real world objects cannot be
represented by simple prisms and ellipsoids.
• Most real world objects cannot best be
described by fixed mathematical curves
(e.g. sin, cos etc..)
Introduction
• Although curves can represent natural
phenomena they can become very complex
e.g. Trees, Mountains, Water, Clouds etc...
Clouds are not spheres,
coastlines are not circles, bark
is not smooth, nor does
lightning travel in straight
lines. -Mandelbrot
Introduction
• Fractals are useful for representing natural
shapes such as trees, coastlines, mountains,
terrain and clouds.
• Magnification of these things review
smaller self-similar copies of the entire
image.
Branches are
self-similar

Roots are self-similar


Fractal Curve Refinement
• Very complex curves can be fashioned
recursively by repeatedly refining the curve.

• Koch Curve: subdivide each segment of Kn into three equal


parts, and replace the middle part with a bump in the shape
of an equilateral triangle.
The Koch Snowflake
Fractal Curve Refinement
//dir - turtle angle
//len - length of line segment
//n - number of iterations
void drawKoch(double dir, double len,int n)
{
double dirRad = 0.0174533 * dir;
// in radians
if(n == 0)
cvs.forward(len,1);
else{
n--; // reduce the order
len /= 3; // and the length
drawKoch(dir, len, n);
dir += 60;
cvs.turnTo(dir);
drawKoch(dir, len, n);
dir -= 120;
cvs.turnTo(dir);
drawKoch(dir, len, n);
dir += 60;
cvs.turnTo(dir);
drawKoch(dir, len, n);
}
}
Lindenmayer Systems
• An L-System works by giving the turtle a
string sequence where each symbol in the
sequence gives turtle instructions.
‘F’ -> go forward 1 step
‘+’ -> turn right by x degrees
‘-’ -> turn left by x degrees
where x is set and predetermined.
Lindenmayer Systems
• The string F+F-F means go forward turn
right, go forward, turn left and go forward.
Lindenmayer Systems
• L-Systems are produced based on a production
rule.
• This rule is iteratively applied to the string.
• e.g. F -> “F+F” means that all ‘F’s in the string
should be replaced with “F+F”
• therefore, F+F-F becomes:

F+F+F+F-F+F
L-Systems
Starting with:
F+F+F+F
and the production rule:
F -> F+F-F-FF+F+F-F

After one iteration the following string would


result
F+F-F-FF+F+F-F + F+F-F-FF+F+F-F + F+F-F-
FF+F+F-F + F+F-F-FF+F+F-F
L-Systems
After 2 iterations the string would be:
F+ F-F-FF+ F+ F-F+ F+ F-F-FF+ F+ F-F-F+ F-F-FF+ F+ F-F-F+
F-F-FF+ F+ F-FF+ F-F-FF+ F+ F-F+ F+ F-F-FF+ F+ F-F+ F+ F-
F-FF+ F+ F-F-F+ F-F-FF+ F+ F-F+ F+ F-F-FF+ F+ F-F+ F+ F-
F-FF+ F+ F-F-F+ F-F-FF+ F+ F-F-F+ F-F-FF+ F+ F-FF+ F-F-
FF+ F+ F-F+ F+ F-F-FF+ F+ F-F+ F+ F-F-FF+ F+ F-F-F+ F-F-
FF+ F+ F-F+ F+ F-F-FF+ F+ F-F+ F+ F-F-FF+ F+ F-F-F+ F-F-
FF+ F+ F-F-F+ F-F-FF+ F+ F-FF+ F-F-FF+ F+ F-F+ F+ F-F-
FF+ F+ F-F+ F+ F-F-FF+ F+ F-F-F+ F-F-FF+ F+ F-F+ F+ F-F-
FF+ F+ F-F+ F+ F-F-FF+ F+ F-F-F+ F-F-FF+ F+ F-F-F+ F-F-
FF+ F+ F-FF+ F-F-FF+ F+ F-F+ F+ F-F-FF+ F+ F-F+ F+ F-F-
FF+ F+ F-F-F+ F-F-FF+ F+ F-F
L-Systems
• Programming L-Systems
produceString(char *rule, int iterations)
{
FILE *ifp, *ofp;

for(int i = 0; i < iterations; i++)


{
if( (ifp = fopen("ldata.txt","r")) == NULL || (ofp = fopen("ltemp.txt","w")) == NULL )
exit(-1); //cannot open files

int ch;
while((ch = fgetc(ifp)) != -1)
{
switch(ch)
{
case 'F': fprintf(ofp,"%s",rule);
break;
default: fprintf(ofp,"%c",ch);
break;
}
}
fclose(ifp);
fclose(ofp);
remove("ldata.txt");
rename("ltemp.txt", "ldata.txt");
}
}
L-Systems
• Programming L-Systems
drawString(int len, float angle)
{
FILE *ifp;
if( (ifp = fopen("ldata.txt","r")) == NULL )
exit(-1); //cannot open files

int ch;
while( (ch = fgetc(ifp)) != -1)
{
switch(ch)
{
case 'F': cvs.forward(len, 1);
break;
case '+': cvs.turn(-angle);
break;
case '-': cvs.turn(angle);
break;
}
}
}
L-Systems
L-Systems
• Programming L- 1 iteration
Systems
– The more iterations you
do, the bigger the curve
will get..
– Therefore you need to
3 iterations
modify the length of the
sides depending on the
number of iterations.
L-Systems
• There is a limit to the number of shapes that
can be drawn with just and ‘F’ directive.
• L-Systems need to be restricted to just F,
you can use however many replacement
letters and strings you like.
L-Systems
• For example, F, X and Y:
• F -> ‘F’
• X -> ‘X+YF+’
• Y -> ‘-FX-Y’
• atom = “X” (starting string)
• But the turtle only draws with F
– This of course is no rule, you could make X and
Y draw as well… it is up to you!!!
L-Systems
The Dragon Curve

F -> ‘F’
X -> ‘X+YF+’
Y -> ‘-FX-Y’
atom = “X”
12 iterations
L-Systems
Koch Island

F -> ‘F+F-F-FF+F+F-F’
X -> ‘’
Y -> ‘’
atom = “F+F+F+F”
5 iterations
L-Systems
• If you look at a tree you will notice that it is
made up of smaller copies of itself.
• e.g. A tree branch is just a smaller version
of a tree.
• Being self-similar doesn’t mean each
smaller version has to be EXACTLY the
same.
L-Systems
• Lets look at a tree
F But that can’t
F be right?

F
F

F -> F+F-F
L-Systems
• Lets look at a tree
F
F

F return here
F

start here
F -> F+F-F
L-Systems
• Lets look at a tree
F
F

F return here
F

start here
F -> F[+F]-F
L-Systems
• Lets look at a tree

F -> F[+F][-F]

push the turtle location pop the turtle location


L-Systems
• Lets look at a tree
F -> F[+F][-F]
atom “F”
L-Systems
• Lets look at a tree
F -> FF-[-F+F+F]+[+F-F-F]
atom “F”
L-Systems
• Lets look at a tree
• Some L-System trees can look
a little ‘calculated’, therefore
random angles and lengths can
be introduced.
• This is the same tree (above)
and below with random
lengths and angles.
L-Systems
• …or you can modify the
thickness or length of the
branch (lines) depending on
the level at which it appears
in the tree.
Affine Transformations
• For example, take these:
 x' 1 / 2 0  1 / 2  x   x '  1 / 2 0  1 / 2  x   x '  1 / 2 0 1/ 2   x
 y '   0 1 / 2  1 / 2. y   y '   0 1 / 2 1 / 2 . y   y '   0 1 / 2  1 / 2. y 
              
 1   0 0 1   1   1   0 0 1   1   1   0 0 1   1 

original image (1x1)

What will it look like after


the transformations??
Affine Transformations
• For example, take these:
 x' 1 / 2 0  1 / 2  x   x '  1 / 2 0  1 / 2  x   x '  1 / 2 0 1/ 2   x
 y '   0 1 / 2  1 / 2. y   y '   0 1 / 2 1 / 2 . y   y '   0 1 / 2  1 / 2. y 
              
 1   0 0 1   1   1   0 0 1   1   1   0 0 1   1 
Affine Transformations
Affine Transformations

• An my personal favourite:
• Affine Transformations:
• T {a,b,c,d,e,f}
 x'  a b c   x
 y '  d e f . y 
  
 1   0 0 1   1 

T1 {0,0,0,0,0.16,0}
T2 {0.2,-0.26,0,0.23,0.22,1.6}
T3 {-0.15,0.28,0,0.26,0.24,0.44}
T4 {0.75,0.04,0,-0.04,0.85,1.6}
Affine Transformations
• An my personal favourite:
Affine Transformations
• An my personal favourite:
Affine Transformations
• An my personal favourite:
Iterative Function Systems
•An iterative function system (IFS) takes a set
of affine transformations and transforms a
point through them based on a random
selection of the transformation.

An IFS is a collection of N affine


transformations Ti, for I = 1,2,…,N
Iterative Function Systems
• Generating an IFS
– Chaos Game
select a random point
do {
select a random transformation
run point through transformation
plot new point
set old point to new point
}while (!bored)
Iterative Function Systems
• Generating an IFS
– Chaos Game
select a random point
do {
select a random transformation
run point through transformation
plot new point
set old point to new point
}while (!bored)
Iterative Function Systems
• The idea: All points on the attractor (final
image) are reachable by applying a long
sequence of affine transformations.
• The random selection of transformations is
invoked to ensure the system is “fully
exercised”
Fractal Compression
• Looking for self-similar sections of an
image and using them to reconstruct the
whole image.
• Affine Mappings are used to reconstruct the
image.
Fractal Compression
Fractal Compression
• Looking for self-similar sections of an
image and using them to reconstruct the
whole image.
• Affine Mappings are used to reconstruct the
image.
The Mandelbrot Set
• Julia and Mandelbrot sets arise from
iteration theory.
• Iteration Theory:
– What happens when a function is iterated
endlessly?
• Mandelbrot used computer graphics to
perform essential experiments.
The Mandelbrot Set
• The Mandelbrot set uses the IFS:
f(z) = z2 + c
– where c is some constant.
• The system produces each “output” by
squaring its “input” and adding c.
• The ORBIT of the input determines how a
value is plotted.
The Mandelbrot Set
• An orbit is the set of values “output” as the
function is iterated.
• For example: f(z) = z2 + 2 with starting
value 0 the sequence of output is:
2, 6, 38, 1 446, 2 090 918, …
• This orbit is said to be infinite as its values
are approaching infinity (called a infinite
orbit).
The Mandelbrot Set
• A finite orbit is one where the output values
settle down around a single value.
• For example: f(z) = z2 - 1 with starting
value 0 the sequence of output is:
0, -1, 0, -1, 0, -1
• These values cycles endlessly and are
considered finite!
The Mandelbrot Set
• Definition: The Mandelbrot set, M, is the
set of all complex numbers, c, that produce
a finite orbit of 0.
• This means that with a starting value of 0,
which values for c (using complex
numbers) produce finite sequences of
outputs.
The Mandelbrot Set
• The first few values
would be:
0, c, c2+c, (c2+c)+c, …
• where c = x + yi
• A plot of the values
for c reveals the
Mandelbrot set.
The Mandelbrot Set
• If you plot the orbits
for values of c inside
the set you will see the
orbits forming.
• For points with c
outside the set, the
plots reveal lines
going off into infinity.

• -mandelbrot_orbit.exe
Julia Sets
• Julia sets are extremely complicated sets of
points in the complex plane.
• There is a different Julia set for each value
of c.
• One version is the Filled Julia Set:
– the set of all starting points whose orbits are
finite.
Julia Sets

c = -0.5+0.58i
Julia Sets

c = -0.76+0.147i
Julia Sets

c = 0.32019+0.25694i
Julia Sets

c = -0.74+0.14i
Fractal Landscapes
Fractal Landscapes
Fractal Landscapes
Divide and Perturb
Fractal Landscape
Fractal Landscape
Fractal Landscape
Fractal Landscape
Fractal Landscape
Fractal Landscapes
• Brownian Motion
– divide and lift
– randomly cut a line in two, randomly lift that
point, then smooth surrounding area.
Fractal Landscapes
• Step One: Randomly
select a spot on a mesh
and raise height by a
random amount.
Fractal Landscapes
• Step Two: Slope from
tip of adjusted height
to edge of map.
Fractal Landscapes
• Step Three: Do this for
all edges of the map.
Fractal Landscapes
• Step Four: Smooth
sharp ridges by raising
surrounding surface to
an even slope.
Fractal Landscapes
• Step Five: Do this for
all four ridges.
Fractal Landscapes
• Step Six: Perform the
lifting and smooth
procedure as many
times as you like.
Fractal Landscapes
• Step Seven: Now
decide on a maximum
height and chop the
highest peaks off.

• You can also do this


for low points to
create areas for water.
Fractal Landscapes
• Step Eight: Smooth
peak caps by
averaging the height
with the neighbouring
vertices.
Fractal Landscapes
• Step Eight: Colour as
you see fit.

You might also like