Fractals Final

Post on 05-Jan-2016

263 views 0 download

Tags:

description

learn fractals in C

transcript

Fractals

Fractals

• A fractal is an object that displays self-similarity, in a somewhat technical sense, on all scales.

• The object need not exhibit exactly the same structure at all scales, but the same "type" of structures must appear on all scales.

Fractals

• Fractals are geometric objects.

• Many real-world objects like ferns are shaped like fractals.

• Fractals are formed by iterations or by recursion.

• Fractals are self-similar.

• In computer graphics, we use fractal functions to create complex objects.

Koch Fractals (Snowflakes)

Iteration 0 Iteration 1 Iteration 2 Iteration 3

Generator1/3 1/3

1/3 1/3

1

Fractal Tree

Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5

Generator

Fractal Fern

Generator

Iteration 0 Iteration 1 Iteration 2 Iteration 3

Drawing of a line using recursion

(x1,y1)

(x1+1,y1+1)

(x2,y2)

(x1+n,y1+n)

Drawing of a line using recursion#include<stdio.h>#include<conio.h>#include<graphics.h>int rec(int x1, int y1, int x2, int y2){

if(x1==x2 && y1 == y2)return 0;

else{

putpixel(x1, y1, 125);rec(x1+1,y1+1, x2, y2);

}return 0;

}

Drawing of a line using recursion

void main(){

int a1=100, a2=100, a3=150, a4=150;int gd=DETECT, gm;clrscr();initgraph(&gd, &gm, "c:\\tc\\bgi");rec(a1, a2, a3, a4);getch();

}

Drawing of rectangles within rectangle

Iteration=1, do nothingIteration =2,

Iteration =3,

Iteration =4,

Drawing of rectangles within rectangle

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<dos.h>int p(int x , int y , int n, int l);void main(){

int gd=DETECT,gm,x=20,y=30,n,l;initgraph(&gd,&gm,"c:\\tc\\bgi"); printf("enter the level(1-5):"); scanf("%d",&n); printf("enter the length of the sqr(>50):"); scanf("%d",&l); p(x,y,n,l); getch(); closegraph();

}

p(int x , int y , int n, int l){

setcolor(3); if(n==1){

return 0; } else {

p(x+l/4,y+l/4,n-1,l/2);rectangle(x,y,x+l,y+l);delay(50);

}return 0;

}

Sierpinski Triangle

Sierpinski Triangle

For i=1, do nothing

For I =2, (x1,y1)

(x2,y2) (x3,y3)

Sierpinski Triangle

For I =3, Inside this triangle, draw a smaller upside down triangle.

It's corners should be exactly in the centers of the sides of the large triangle:

(x1,y1)

(x2,y2) (x3,y3)

(x1+x2)/2,(y1+y2)/2(x1+x3)/2,(y1+y3)/2

(x2+x3)/2,(y2+y3)/2

Sierpinski Triangle

For i=4, Now, draw 3 smaller triangles in each of the 3 triangles that are pointing upwards, again with the corners in the centers of the sides of the triangles that point upwards

Sierpinski Triangle

• For I =5, Now there are 9 triangles pointing upwards. In each of these 9, draw again smaller upside down triangles:

Sierpinski Triangle

• For i=6, In the 27 triangles pointing upwards, again draw 27 triangles pointing downwards:

Sierpinski Triangle#include<stdio.h>#include<conio.h>#include<graphics.h>#include<dos.h>p(int , int , int , int , int, int, int);void main(){ int gd=DETECT,gm,x1,y1,x2,y2,x3,y3,n; initgraph(&gd,&gm,"c:\\tc\\bgi");

printf("enter the level(1-5):"); scanf("%d",&n); printf("Enter coordinates of triangle"); scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3); p(x1,y1,x2,y2,x3,y3,n); getch(); closegraph();}

Sierpinski Trianglep(int x1, int y1 , int x2, int y2, int x3, int y3,int n){ setcolor(3); if(n==1)

{return 0;

} else { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1);

p(x1,y1,(x1+x2)/2, (y1+y2)/2,(x1+x3)/2,(y1+y3)/2,n-1);p((x1+x2)/2, (y1+y2)/2,x2,y2,(x2+x3)/2,(y2+y3)/2,n-1);p((x1+x3)/2,(y1+y3)/2,x3,y3,(x2+x3)/2,(y2+y3)/2,n-1);delay(50);

}}

Another example of fractal

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<dos.h>p(int x , int y , int n , float l);void main(){

int gd=DETECT,gm,x=20,y=30,n,l; initgraph(&gd,&gm,"c:\\tc\\bgi"); printf("enter the level(1-5):"); scanf("%d",&n); printf("enter the length of the sqr(>50):"); scanf("%d",&l); p(x,y,n,l); getch(); closegraph();

}

p(int x , int y , int n, float l ){

setcolor(3); if(n==1) {

rectangle(x,y,x+l,y+l); }

else {

p(x,y,n-1,l/3);p(x + (2*l/3),y,n-1,l/3);p(x,y + (2*l)/3,n-1,l/3);p(x + (2*l)/3,y + (2*l/3),n-1,l/3);p(x + l/3,y + l/3,n-1,l/3);delay(50);

}return 0;

}

Koch Curve

The Koch Curve FractalThe Koch Curve is a fractal that starts with a simple pattern made of a line that is divided into 3 equal parts.

Erase the middle segment and replace it with an upsidedown “V” shape, and now the whole pattern is made up of four line segments.

The Koch Curve Fractal

Next, we do the same thing again. Each of those four lines is divided in thirds, and the middle segment is replaced with a “V”. There are now 4x4 or 16 line segments.

The Koch Curve FractalSo next, we’ll replace each of the 16 line

segments with the same pattern again.

The Koch Curve#include<stdio.h>#include<conio.h>#include<graphics.h>#include<dos.h>#include<math.h>void p(int x , int y ,int x1, int y1, int n);void main(){

int gd=DETECT,gm,xl=100,yl=100,xr=300,yr=100,n,l; initgraph(&gd,&gm,"c:\\tc\\bgi"); printf("enter the level(1-5):"); scanf("%d",&n); p(xl,yl,xr,yr,n); getch(); closegraph();

}

void p(int xl , int yl , int xr, int yr, int n){ int x1,y1,x2,y2,x3,y3,xc,yc; setcolor(3); if(n==1)

{line(xl,yl,xr,yr); }

else {x1=(2*xl+xr)/3;y1=(2*yl+yr)/3;x2=(xl+2*xr)/3;y2=(yl+2*yr)/3;xc=(xl+xr)/2;yc=(yl+yr)/2;x3=xc+ sqrt(3)*(y2-yc);y3=yc+ sqrt(3)*(xc-x2);p(xl,yl,x1,y1,n-1);p(x1,y1,x3,y3,n-1);p(x3,y3,x2,y2,n-1);p(x2,y2,xr,yr,n-1);delay(500);

}}

Assignment 2

Draw koch snowflake for rectangle.