fractals - cs.csi.cuny.edunatacha/teachspring_12/csc470/notes/fractal… · csc 470 computer...

Post on 11-Mar-2018

227 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

CSC 470 Computer GraphicsCSC 470 Computer Graphics

FractalsFractals

2

This WeekThis Week

•• Approaches to InfinityApproaches to InfinityFractals and SelfFractals and Self--SimilaritySimilarityIterative Function SystemsIterative Function SystemsLindenmayerLindenmayer SystemsSystemsCurvesCurvesNatural Images (trees, landscapes..)Natural Images (trees, landscapes..)

3

IntroductionIntroduction

What is a Fractal?What is a Fractal?A fractal is an image with selfA fractal is an image with self--similar similar properties produced by recursive or iterative properties produced by recursive or iterative algorithmic means.algorithmic means.

“anything which has a substantial measure of exact or statistica“anything which has a substantial measure of exact or statistical selfl self--similarity”similarity”

Mandelbrot coined the term from the Mandelbrot coined the term from the latinlatinfractusfractus meaning “fragmented” or “irregular”meaning “fragmented” or “irregular”

4

IntroductionIntroduction

Why use fractals in Computer Graphics?Why use fractals in Computer Graphics?•• Most real world objects are inherently Most real world objects are inherently

smooth.smooth.•• Most real world objects cannot be Most real world objects cannot be

represented by simple prisms and represented by simple prisms and ellipsoids.ellipsoids.

•• Most real world objects cannot best be Most real world objects cannot best be described by fixed mathematical curves described by fixed mathematical curves (e.g. sin, (e.g. sin, coscos etc..)etc..)

5

IntroductionIntroduction

•• Although curves can represent natural Although curves can represent natural phenomena they can become very phenomena they can become very complex e.g. Trees, Mountains, Water, complex e.g. Trees, Mountains, Water, Clouds etc...Clouds etc...

Clouds are not spheres, Clouds are not spheres, coastlines are not circles, bark coastlines are not circles, bark is not smooth, nor does is not smooth, nor does lightning travel in straight lightning travel in straight lines.lines. --MandelbrotMandelbrot

6

IntroductionIntroduction

•• Fractals are useful for representing natural Fractals are useful for representing natural shapes such as trees, coastlines, shapes such as trees, coastlines, mountains, terrain and clouds.mountains, terrain and clouds.

•• Magnification of these things review Magnification of these things review smaller selfsmaller self--similar copies of the entire similar copies of the entire image.image.

7

Branches are self-similar

Roots are self-similar

8

Fractal Curve RefinementFractal Curve Refinement

The Koch Snowflake

• 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.

9

Fractal Curve RefinementFractal Curve Refinement//dir //dir -- turtle angleturtle angle////lenlen -- length of line segmentlength of line segment//n //n -- number of iterationsnumber of iterationsvoid void drawKoch(doubledrawKoch(double dir, double dir, double len,intlen,int n)n){{

double double dirRaddirRad = 0.0174533 * dir; = 0.0174533 * dir; // in radians// in radians

if(nif(n == 0)== 0)cvs.forward(len,1);cvs.forward(len,1);

else{else{nn----;; // reduce the order// reduce the orderlenlen /= 3;/= 3; // and the length// and the lengthdrawKoch(dirdrawKoch(dir, , lenlen, n);, n);dir += 60;dir += 60;cvs.turnTo(dircvs.turnTo(dir););drawKoch(dirdrawKoch(dir, , lenlen, n);, n);dir dir --= 120;= 120;cvs.turnTo(dircvs.turnTo(dir););drawKoch(dirdrawKoch(dir, , lenlen, n);, n);dir += 60;dir += 60;cvs.turnTo(dircvs.turnTo(dir););drawKoch(dirdrawKoch(dir, , lenlen, n);, n);

}}}}

10

LindenmayerLindenmayer SystemsSystems

•• An LAn L--System works by giving the turtle a System works by giving the turtle a string sequence where each symbol in the string sequence where each symbol in the sequence gives turtle instructions.sequence gives turtle instructions.

‘F’ ‘F’ --> go forward 1 step> go forward 1 step‘+’ ‘+’ --> turn right by x degrees> turn right by x degrees‘‘--’ ’ --> turn left by x degrees> turn left by x degreeswhere x is set and predetermined. where x is set and predetermined.

11

LindenmayerLindenmayer SystemsSystems

•• The string F+FThe string F+F--F means go forward turn F means go forward turn right, go forward, turn left and go forward.right, go forward, turn left and go forward.

12

LindenmayerLindenmayer SystemsSystems

•• LL--Systems are produced based on a Systems are produced based on a production rule.production rule.

•• This rule is iteratively applied to the string.This rule is iteratively applied to the string.•• e.g. F e.g. F --> “F+F” means that all ‘F’s in the > “F+F” means that all ‘F’s in the

string should be replaced with “F+F”string should be replaced with “F+F”•• therefore, F+Ftherefore, F+F--F becomes:F becomes:

F+F+F+FF+F+F+F--F+FF+F

13

LL--SystemsSystems

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

14

LL--SystemsSystemsAfter 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

15

LL--SystemsSystems•• Programming LProgramming L--SystemsSystems

produceString(charproduceString(char *rule, *rule, intint iterations)iterations){{

FILE *FILE *ifpifp, *, *ofpofp;;

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

if( (if( (ifpifp = = fopen("ldata.txt","rfopen("ldata.txt","r")) == NULL || (")) == NULL || (ofpofp = = fopen("ltemp.txt","wfopen("ltemp.txt","w")) == NULL )")) == NULL )exit(exit(--1); //cannot open files1); //cannot open files

intint chch;;while((chwhile((ch = = fgetc(ifpfgetc(ifp)) != )) != --1)1){{

switch(chswitch(ch)){{

case 'F': case 'F': fprintf(ofp,"%s",rulefprintf(ofp,"%s",rule););break;break;

default: default: fprintf(ofp,"%c",chfprintf(ofp,"%c",ch););break;break;

}}}}fclose(ifpfclose(ifp););fclose(ofpfclose(ofp););remove("ldata.txtremove("ldata.txt");");rename("ltemp.txtrename("ltemp.txt", "", "ldata.txtldata.txt");");

}}}}

16

LL--SystemsSystems

•• Programming LProgramming L--SystemsSystemsdrawString(intdrawString(int lenlen, float angle), float angle){{

FILE *FILE *ifpifp;;if( (if( (ifpifp = = fopen("ldata.txt","rfopen("ldata.txt","r")) == NULL )")) == NULL )

exit(exit(--1); //cannot open files1); //cannot open files

intint chch;;while( (while( (chch = = fgetc(ifpfgetc(ifp)) != )) != --1)1){{

switch(chswitch(ch)){{

case 'F': case 'F': cvs.forward(lencvs.forward(len, 1);, 1);break;break;

case '+': case '+': cvs.turncvs.turn((--angle);angle);break;break;

case 'case '--': ': cvs.turn(anglecvs.turn(angle););break;break;

}}}}

}}

17

LL--SystemsSystems

•• Programming LProgramming L--SystemsSystemsvoid void myDisplay(voidmyDisplay(void)){{

cvs.clearScreencvs.clearScreen();();glLineWidth(3);glLineWidth(3);cvs.moveTo(cvs.moveTo(--50.0,0.0);50.0,0.0);produceString("FproduceString("F--F++FF++F--F", 3);F", 3);drawString(20,60);drawString(20,60);glFlushglFlush();();

}}

18

LL--SystemsSystems

•• Programming LProgramming L--SystemsSystems

The more iterations you The more iterations you do, the bigger the curve do, the bigger the curve will get..will get..Therefore you need to Therefore you need to modify the length of the modify the length of the sides depending on the sides depending on the number of iterations.number of iterations.

1 iteration

3 iterations

19

LL--SystemsSystems

•• There is a limit to the number of shapes There is a limit to the number of shapes that can be drawn with just and ‘F’ that can be drawn with just and ‘F’ directive.directive.

•• LL--Systems need to be restricted to just F, Systems need to be restricted to just F, you can use however many replacement you can use however many replacement letters and strings you like.letters and strings you like.

20

LL--SystemsSystems

•• For example, F, X and Y:For example, F, X and Y:•• F F --> ‘F’> ‘F’•• X X --> ‘X+YF+’> ‘X+YF+’•• Y Y --> ‘> ‘--FXFX--Y’Y’•• atom = “X” (starting string)atom = “X” (starting string)•• But the turtle only draws with FBut the turtle only draws with F

This of course is no rule, you could make X This of course is no rule, you could make X and Y draw as well… it is up to you!!!and Y draw as well… it is up to you!!!

21

LL--SystemsSystems

The Dragon CurveThe Dragon Curve

F F --> ‘F’> ‘F’X X --> ‘X+YF+’> ‘X+YF+’Y Y --> ‘> ‘--FXFX--Y’Y’atom = “X”atom = “X”12 iterations12 iterations

22

LL--SystemsSystems

Koch IslandKoch Island

F F --> ‘F+F> ‘F+F--FF--FF+F+FFF+F+F--F’F’X X --> ‘’> ‘’Y Y --> ‘’> ‘’atom = “F+F+F+F”atom = “F+F+F+F”5 iterations5 iterations

23

LL--SystemsSystems

•• If you look at a tree you will notice that it is If you look at a tree you will notice that it is made up of smaller copies of itself.made up of smaller copies of itself.

•• e.g. A tree branch is just a smaller version e.g. A tree branch is just a smaller version of a tree.of a tree.

•• Being selfBeing self--similar similar doesn’tdoesn’t mean each mean each smaller version has to be EXACTLY the smaller version has to be EXACTLY the same.same.

24

LL--SystemsSystems

•• Lets look at a treeLets look at a tree

FF

FF

F -> F+F-F

But that can’t be right?

25

LL--SystemsSystems

•• Lets look at a treeLets look at a tree

FF

FF

F -> F+F-Fstart here

return here

26

LL--SystemsSystems

•• Lets look at a treeLets look at a tree

FF

FF

F -> F[+F]-Fstart here

return here

27

LL--SystemsSystems

•• Lets look at a treeLets look at a tree

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

push the turtle location pop the turtle location

28

LL--SystemsSystems

•• Lets look at a treeLets look at a tree

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

atom “F”atom “F”

29

LL--SystemsSystems

•• Lets look at a treeLets look at a treeF F --> FF> FF--[[--F+F+F]+[+FF+F+F]+[+F--FF--F]F]

atom “F”atom “F”

30

LL--SystemsSystems

•• Lets look at a treeLets look at a tree•• Some LSome L--System trees can System trees can

look a little ‘calculated’, look a little ‘calculated’, therefore random angles and therefore random angles and lengths can be introduced.lengths can be introduced.

•• This is the same tree (above) This is the same tree (above) and below with random and below with random lengths and angles.lengths and angles.

31

LL--SystemsSystems

•• ……or you can modify the or you can modify the thickness or length of the thickness or length of the branch (lines) depending on branch (lines) depending on the level at which it appears the level at which it appears in the tree.in the tree.

32

Affine TransformationsAffine Transformations

•• For example, take these:For example, take these:

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

⎡−−

=⎥⎥⎥

⎢⎢⎢

1.

1002/12/102/102/1

1''

yx

yx

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

⎡ −=

⎥⎥⎥

⎢⎢⎢

1.

1002/12/102/102/1

1''

yx

yx

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

⎡−=

⎥⎥⎥

⎢⎢⎢

1.

1002/12/10

2/102/1

1''

yx

yx

original image (1x1)

What will it look like after the transformations??

33

Affine TransformationsAffine Transformations

•• For example, take these:For example, take these:

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

⎡−−

=⎥⎥⎥

⎢⎢⎢

1.

1002/12/102/102/1

1''

yx

yx

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

⎡ −=

⎥⎥⎥

⎢⎢⎢

1.

1002/12/102/102/1

1''

yx

yx

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

⎡−=

⎥⎥⎥

⎢⎢⎢

1.

1002/12/10

2/102/1

1''

yx

yx

34

Affine TransformationsAffine Transformations

35

Affine TransformationsAffine Transformations

•• An my personal An my personal favouritefavourite::

•• Affine Transformations:Affine Transformations:•• T {T {a,b,c,d,e,fa,b,c,d,e,f}}

T1 {0,0,0,0,0.16,0}T1 {0,0,0,0,0.16,0}T2 {0.2,T2 {0.2,--0.26,0,0.23,0.22,1.6}0.26,0,0.23,0.22,1.6}T3 {T3 {--0.15,0.28,0,0.26,0.24,0.44}0.15,0.28,0,0.26,0.24,0.44}T4 {0.75,0.04,0,T4 {0.75,0.04,0,--0.04,0.85,1.6}0.04,0.85,1.6}

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

⎡=

⎥⎥⎥

⎢⎢⎢

1.

1001''

yx

fedcba

yx

36

Affine TransformationsAffine Transformations

•• An my personal An my personal favouritefavourite::

37

Affine TransformationsAffine Transformations

•• An my personal An my personal favouritefavourite::

38

Affine TransformationsAffine Transformations

•• An my personal An my personal favouritefavourite::

39

Iterative Function SystemsIterative Function Systems

••An iterative function system (IFS) takes a An iterative function system (IFS) takes a set of affine transformations and transforms set of affine transformations and transforms a point through them based on a random a point through them based on a random selection of the transformation.selection of the transformation.

An IFS is a collection of N affine An IFS is a collection of N affine transformations Ttransformations Tii, for I = 1,2,…,N, for I = 1,2,…,N

40

Iterative Function SystemsIterative Function Systems

•• Generating an IFSGenerating an IFSChaos GameChaos Gameselect a random pointselect a random pointdo {do {

select a random transformationselect a random transformationrun point through transformation run point through transformation plot new pointplot new pointset old point to new pointset old point to new point

}while (!bored)}while (!bored)

41

Iterative Function SystemsIterative Function Systems

•• Generating an IFSGenerating an IFSChaos GameChaos Gameselect a random pointselect a random pointdo {do {

select a random transformationselect a random transformationrun point through transformation run point through transformation plot new pointplot new pointset old point to new pointset old point to new point

}while (!bored)}while (!bored)

42

Iterative Function SystemsIterative Function Systems

•• The idea: All points on the attractor (final The idea: All points on the attractor (final image) are reachable by applying a long image) are reachable by applying a long sequence of affine transformations.sequence of affine transformations.

•• The random selection of transformations is The random selection of transformations is invoked to ensure the system is “fully invoked to ensure the system is “fully exercised”exercised”

43

44

Next WeekNext Week

•• More FractalsMore Fractals•• Mandelbrot and Julia Mandelbrot and Julia

SetsSets•• Generating realistic Generating realistic

landscapeslandscapes

top related