Activitatea 4Generarea fractalilor

Generarea fractalilor 1.

Linia lui Koch-Algoritm de generare in C++
Vom descrie o functie recursiva denumita fractal(), care va genera o linie de coasta Koch,plecand de la un segment dat. Functia va primi ca parametri coordonatele extremitatilor segmentului, precum si numarul pasului curent.In functia main() vom initializa modul grafic si vom apela functia fractal(): void main() { cout<<"L="; cin>>L; cout<<"n="; cin>>n; cin.get(); radical=sqrt(3)/2; //initializez modul grafic; int gdriver=DETECT, gmode; int eroare; initgraph(&gdriver, &gmode, "c:\\borlandc\\bgi"); eroare=graphresult(); if(eroare==grOk) //am initializat corect modul grafic { //stabilesc culoarea de fundal si cea de desenare setkcolor(BLACK); setcolor(RED); line(0, 200, L, 200, 1); fractal(0, 200, L, 200, 1); cin.get(); } closegraph(); //inchid modul grafic }

In functia fractal(x1,y1,x2,y2,nr) verificam in primul rand conditia de terminare.DAca numarul de nr nu depaseste n(numarul de pasi pe care trebuie sa il efetuam), determinam coordonatele punctelor A(xa,ya) si B(xb,yb) care impart segmentul [(x1,y,1),(x2,y2)] in trei parti egale, apoi construim triunghiul echilateral de latura AB.Apelam apoi recursiv functia fractal(), pentru fiecare din cele 4 segmente obtinute. void fractal(int x1, int y1, int x2, int y2, int nr) { //generez un fractal pornind de la segmentul [(x1,y1),(x2,y2)] int xa, ya, xb, yb, xc, yx, xm, ym, cadran; float latura2, m1; { if(nr<=n) //daca numarul pasului curent nu depaseste n {
1

else cadran=3. } } } 2. xb. yc=ym+(int)(cos(M_PI/3)*latura2*radical). ym=(ya+yb)/2. else yc=ya+(int)(latura2*radical). xb=(x1+2*x2)/3.x. yc=ym-(int)(cos(M_PI/3)*latura2*radical). yc=ym+(int)(cos(M_PI/3)*latura2*radical). ya. ya=(2*y1+2)/3. yb). if(xa<xb) yc=ya-(int)(latura2*radical). break. case 3: xc=xm-(int)(sin(M_PI/3)*latura2*radical). cu 3 parametri: . case 2: xc=xm-(int)(sin(M_PI/3)*latura2*radical). if(m1>0) if(yb<ya) cadran=2.r-latura patratului central. yc=ym-(int)(cos(M_PI/3)*latura2*radical).xa=(2*x1+2)/3.y-coordonatele centrului. xm=(xa+xb)/2. switch(cadran) { case 1: xc=xm+(int)(sin(M_PI/3)*latura2*radical). } else { m1=(yb-ya)/(xb-xa). if(m1<0) if(yb>ya) cadran=1. Patratul(covorul) lui Sierpinski-Algoritm de generare in C++ -descrierea algoritmului: vom considera o functie recursiva denumita fractal(). latura2=distanta2 (xa. if(latura2>=2) { //construiesc triunghiul echilateral cu latura AB if(ya==yb) { xc=(xa+xb)/2. vom apela recursiv functia fractal() de 4 ori. case 4: xc=xm+(int)(sin(M_PI/3)*latura2*radical). yb=(y1+2*y2)/3. . daca r>0.else cadran=4. break. break. break. pentru a desena fractalii corespunzatori 2 .

y-r. } } int main() { cout<<"l=".(yb+yc)/2).RED).grmode. setcolor(1+i%15).(xb+xc)/2. line((xa+xb)/2. Triunghiul lui Sierpinski-Algoritm de generare in C++ Triunghiul lui Sierpinski porneste de la un triunghi care se imparte in 4 triunghiuri egale.ya.h> int l.Scrieti un program recursiv care sa citeasca de la tastatura coordonatele varfurilor triunghiului initia. //se inchide modul grafic return 0. y+r.y) #include <iostream.yb.(ya+yc)/2). getch(). 3 .h> #include <graphics. r/2).Procedeul continua la infinit. int r) {if(r>0) {//se deseneaza fractalii din cele 4 colturi fractal(x-r. cin>>l grdriver=DETECT. int y.xc. x+r/2. fractal(getmaxx()/2. eroare=graphresult(). fractal(x+r.(ya+yb)/2. &grmode. y-r.(ya+yb)/2."c:\\borlandc\\bgi"). r/2).h> #include <conio. //se deseneaza deasupra patratului de latura r bar(x-r/2. r/2).eroare.(xa+xc)/2. r/2). Procedura Divide(i. si afiseaza in mod grafic un triunghi Sierpinski obtinut dupa n iteratii(n citit de asemenea de la tastatura). //se initializeaza modul grafic initgraph(&grdriver. y-r/2. } closegraph().yc) { daca(i<=n) atunci { delay(0).celor 4 colturi ale patratului de latura r si cu centrul in punctul de coordonate (x.Fiecare din cele 4 triunghiuri obtinute se imparte la randul lui in 4 triunghiuri egale. if (eroare==grok) //am initializat corect modul grafic {//stabilesc culoarea de fundal si culoarea patratelor setbkcolor(WHITE). line((xa+xb)/2. getmaxy()/2.xa. fractal(x+r. fractal(x-r. void fractal(int x. l). } 3. setfillstyle(SOLID_FILL.xb. y+r.grdriver. y+r/2).

yc).n.(yb+yc)/2).(xb+xc)/2.(xa+xc)/2.xa.ya.xb. cout<<"Dati numarul de iteratii:". fractali(1.(xa+xb)/2.xb.(xa+xc)/2. divide(i+1.(xa+xb)/2.xc. divide(i+1.(xc+xb)/2.(ya+yb)/2.xb.(xa+xc)/2.line((xc+xb)/2.ya.gm.cin>>xc. setcolor(1+i%15).(xa+xb)/2.(xc+xb)/2. gd=DETECT. } } void main() { cout<<"Dati xa:".yb).h> #include<stdlib.h> #include<stdio.xb.(ya+yc)/2).yb.(xb+xc)/2.(ya+yc)/2).(xa+xc)/2.ya. fractali(i+1.(ya+yb)/2.yb.(yb+yc)/2. void fractali(i.(yc+yb)/2. line((xa+xb)/2.(xa+xc)/2.yc. line(xa.xc.(xa+xc)/2. cout<<"Dati yc:".(yb+yc)/2).yb.ya.cin>>xa.(ya+yc)/2).(ya+yc)/2).(yb+yc)/2.(ya+yc)/2). cout<<"Dati xb:".ya. line(xb. cout<<"Dati ya:". cout<<"Dati yb:".h> #include<conio.h> #include<graphics.(xa+xc)/2.cin>>ya.yb.cin>>xb.(ya+yb)/2.yc) { if(i<=n) { delay(2). } } */ #include<iostream. initgraph (&gd.(ya+yb)/2.(ya+yc)/2).cin>>yb.(xa+xb)/2.(ya+yc)/2).xa.xb.(xa+xc)/2.(ya+yb)/2.ya.xc.yc).(ya+yb)/2.yc).h> int i.yc. // clrscr().xa.(xb+xc)/2.(ya+yc)/2).h> #include<dos.cin>>yc. closegraph().yc.(yc+yb)/2.(ya+yb)/2.(xa+xb)/2.(ya+yb)/2. divide(i+1.(xa+xb)/2.(yb+yc)/2).(yc+yb)/2.(xa+xc)/2. cout<<"Dati xc:". fractali(i+1.xa.xb.gd.xc. line((xc+xb)/2.xc. divide(i+1.yb.(ya+yc)/2). line((xa+xb)/2.cin>>n. fractali(i+1.xa. } 4 ."C:\\BORLANDC\\bgi").ya. line(xa.(xb+xc)/2.yb. fractali(i+1.xc.&gm.(yc+yb)/2.(xb+xc)/2.xc.

} } void main() { int x=320.x-1. bar(x+1. } 5. Fractal obtinut prin iterarea unui romb-Algoritm de generare in C++ Procedura Divide(x.random(15)). daca(r>10) atunci 5 . y-l/2.gm.x-1. patrat(x+l/2. patrat(x.y.y-l/2. patrat(x.int y.h> #include<dos. y+l/2).l/2).y-l/2+1.y. if(l>0) { patrat(x.y. delay(1). y-l/2.y.l/2).h> #include<iostream.y+l/2-1)."c:\\utils\\borlandc\\bgi"). x.l) { daca(l>0) atunci { divide(x.l/2).y+l/2. delay(1).y-l/2+1. delay(1). rectangle(x.y.y.l/2). setfillstyle(SOLID_FILL. x. setcolor(random(15)).l/2).l).y+l/2.y-l/2. initgraph (&gd. divide(x+l/2.l/2).l=200. divide(x-l/2.h> int gd.4. void patrat (int x. delay(1). patrat(x-l/2. divide(x. rectangle(x.style. Fractal obtinut prin iterarea unui cerc-Algoritm de generare in C++ Procedura Divide(x. y+l/2).y. delay(1). bar(x+1. randomize().l/2). } } */ #include<graphics.r) { randomize(). delay(1).h> #include<stdlib.int l) { int color.&gm.y=240. delay(1).l/2). delay(1).y+l/2-1).

delay(1).y-r/2.r). initgraph (&gd.r/2).y.y+r/2. randomize(). } } #include<graphics.y=240.style. } 6 .int r) { int color.r/2).h> int gd. circle(x.h> #include<iostream. cerc(x. divide(x.r/2).r).r/2). delay(1). delay(1). setcolor(YELLOW).int y.y. cerc(x+r/2.r/2).r/2).h> #include<dos. } } void main() { int x=320.y. circle(x.y.y+r/2.r=200.&gm.gm. delay(1).r).r/2).h> #include<stdlib.y. if(r>10) { cerc(x-r/2. delay(1). divide(x. delay(1). cerc(x.r/2). delay(1). cerc(x."c:\\borlandc\\bgi"). delay(1).{ divide(x-r/2.y-r/2.y. divide(x+r/2.y. void cerc (int x.

Sign up to vote on this title
UsefulNot useful