Program Fractal_Poligon; {--------------------------------------------------------------------- Poligonul din 3*4^n laturi se construieste pe baza triunghiurilor echilaterale. Fiecare latura a triunghiului se imparte in 3 segmente, pe cel din mijloc se construieste un alt triunghi echilateral, iar segmentul se elimina. La fel se procedeaza cu poligonul nou format de n ori. ---------------------------------------------------------------------} Uses CRT, Graph; {---------------------------------------------------------------------} Const mult_exact = 10000; { 1..65535 } {Exactitatea calculelor} comenzi = '+/- : Nr de recursii | * : Directia | , X : Iesire'; {---------------------------------------------------------------------} lamda1 = 1; lamda3 = -3 / 2; lamda2 = 1 / 2; sqrt3 = 1.7320508075688772935274463415059; color = red; Blue01: FillPatternType = ($00, $11, $22, $33, $44, $55, $77, $88); {---------------------------------------------------------------------} Type TCoord = longint; {---------------------------------------------------------------------} Var GD, GM: integer; ch: char; nr_rec: byte; directie: integer; {---------------------------------------------------------------------} procedure drow_triunghi(cx, cy: TCoord; r: real; n: byte; dir: integer); var gx, gy, tx, ty: TCoord; ex, ey: integer; {---------------------------------------------------------------------} function raport(x1, x2: TCoord; r: real): TCoord; begin raport := round((x1+r*x2) / (1+r)); end; function varf_ekil(x1, y1, x2, y2: TCoord; var xv, yv: TCoord; coef: integer): boolean; begin if(x1<>x2)or(y1<>y2) then begin xv := round(( coef*(y1-y2)*sqrt3 + x2+x1 )/2); yv := round(( coef*(x2-x1)*sqrt3 + y2+y1 )/2); varf_ekil := true; end else begin xv := x1; yv := x2; varf_ekil := false; end; end; {---------------------------------------------------------------------} procedure SetXYPoz(x, y: real); begin gx := round(x*mult_exact); gy := round(y*mult_exact); end; {---------------------------------------------------------------------} procedure drow_line_to(x,y: TCoord); begin line(ex+(gx div mult_exact),ey+(gy div mult_exact),ex+(x div mult_exact),ey+(y div mult_exact)); gx := x; gy := y; end; {---------------------------------------------------------------------} procedure drow_line(x, y: TCoord); { (x,y) - coordonatele punctului destinatie } begin if n<=0 then begin { Sfarsitul recursiei. Desen: ___ } tx := gx; { Pastrare pozitia de pornire } ty := gy; drow_line_to(x, y); { Doar aici se deseneaza linia } end else begin { Liniile 1, 2, 3 si 4 vor fi desenate respectiv in felul urmator: _/\_ } dec(n); drow_line(raport(gx, x, lamda2), raport(gy, y, lamda2)); { Linia 1 } if (not varf_ekil(gx, gy, raport(gx,x,1), raport(gy,y,1), tx, ty, dir)) then n :=0; drow_line(tx, ty); { Linia 2 } drow_line(raport(tx, x, lamda1), raport(ty, y, lamda1)); { Linia 3 } drow_line(x, y); { Linia 4 } tx := raport(x, tx, lamda3); { Pastrare pozitia de pornire } ty := raport(y, ty, lamda3); inc(n); end; { (tx,ty) - coordonatele punctului de pornire } end; {---------------------------------------------------------------------} begin { /_\ } if (dir>0) then dir := 1 else dir := -1; ex := round(cx); ey := round(cy); SetXYPoz(- dir * r * sqrt3 / 2, dir * r / 2 ); drow_line(-gx, gy); { _ } cx := tx; cy := ty; varf_ekil(tx, ty, gx, gy, tx, ty, -1); drow_line(tx, ty); { \ } drow_line(cx, cy); { / } end; {---------------------------------------------------------------------} BEGIN ClrScr; GD := detect; InitGraph(GD, GM, ''); if GraphResult <> grOk then begin GM := GraphResult; closegraph; Writeln('Erroare initializare modul grafic!'); Writeln(GraphResult, ': ', GraphErrorMsg(GM)); readkey; halt(1); end; { Initializare valori } nr_rec := 3; directie := 1; repeat ClearViewPort; Rectangle(0, 0, GetMaxX, GetMaxY); OutTextXY(10, 10, 'nr rec: '+chr(nr_rec div 10+48)+chr(nr_rec mod 10+48)); OutTextXY((GetMaxX-length(comenzi)*8)div 2, GetMaxY-13, comenzi); drow_triunghi(GetMaxX div 2, GetMaxY div 2, GetMaxY div 7 * 3, nr_rec, directie); {SetFillPattern(Blue01,color); FloodFill(GetMaxX div 2, GetMaxY div 2, getcolor);} ch := ReadKey; case ch of '+': if nr_rec < 12 then inc(nr_rec); '-': if nr_rec > 0 then dec(nr_rec); '*': directie := -directie; end; until (ch = #27) or (ch = #13) or (upcase(ch) = 'X'); { or } closegraph; END.