/* Fractal_Poligon */ // Program Fractal_Poligon; /*--------------------------------------------------------------------- // {--------------------------------------------------------------------- Poligonul din 3*4^n laturi se construieste pe baza triunghiurilor // Poligonul din 3*4^n laturi se construieste pe baza triunghiurilor echilaterale. Fiecare latura a triunghiului se imparte in 3 segmente, // echilaterale. Fiecare latura a triunghiului se imparte in 3 segmente, pe cel din mijloc se construieste un alt triunghi echilateral, iar // pe cel din mijloc se construieste un alt triunghi echilateral, iar segmentul se elimina. La fel se procedeaza cu poligonul nou format // segmentul se elimina. La fel se procedeaza cu poligonul nou format de n ori. // de n ori. ---------------------------------------------------------------------*/ // ---------------------------------------------------------------------} // Uses #include // {System,} #include #include #include // CRT, #include // Graph; /*---------------------------------------------------------------------*/ // {---------------------------------------------------------------------} /* compatibility */ // {compatibility} #define round(x) floor( x + (x < 0 ?-0.5 : 0.5) ) int upcase(int c){ int x = c&~0x20; return (x>0x40)&(x<0x5B)?x:c; } /*#define upcase(c) ((c)&~0x20)*/ char buf[20]; /*---------------------------------------------------------------------*/ // {---------------------------------------------------------------------} // const const char* comenzi = "+ / - : Nr de recursii | * : Directia | , X : Iesire"; // comenzi = '+ / - : Nr de recursii | * : Directia | , X : Iesire'; #define mult_exact 300000 /*1...4284967295*/ /*Exactitatea calculelor*/ // mult_exact = 30000; { 1..65535 } {Exactitatea calculelor} /*---------------------------------------------------------------------*/ // {---------------------------------------------------------------------} #define lamda1 ( 1 ) // lamda1 = 1; #define lamda2 ( 0.5) /* 1 / 2 == 0 */ // lamda2 = 1 / 2; { 1 div 2 = 0 } #define lamda3 (-1.5) /* -3 / 2 == -1 */ // lamda3 = -3 / 2; { -3 div 2 = -1 } #define sqrt3 ((double)(1.7320508075688772935274463415059)) // sqrt3 = 1.7320508075688772935274463415059; #define color RED // color = Red; const char Blue01[8] = {153, 66, 36, 24, 90, 24, 36, 66}; // Blue01: FillPatternType = (153, 66, 36, 24, 90, 24, 36, 66); /*---------------------------------------------------------------------*/ // {---------------------------------------------------------------------} // Type typedef long TCoord; // TCoord = longint; /*---------------------------------------------------------------------*/ // {---------------------------------------------------------------------} // Var int GD, GM; // GD, GM: integer; char ch; // ch: char; char nr_rec; // nr_rec: byte; int directie; // directie: integer; /*---------------------------------------------------------------------*/ // {---------------------------------------------------------------------} void drow_triunghi(TCoord cx, TCoord cy, double r, char n_, int dir_); // procedure drow_triunghi(cx, cy: TCoord; r: real; n: byte; dir: integer); char n;int dir; // var TCoord gx, gy, tx, ty; // gx, gy, tx, ty: TCoord; int ex, ey; // ex, ey: integer; /*---------------------------------------------------------------------*/ // {---------------------------------------------------------------------} TCoord raport(TCoord x1, TCoord x2, double r){return round((x1+r*x2) / (1+r));}; // function raport(x1, x2: TCoord; r: real): TCoord; begin raport := round((x1+r*x2) / (1+r)); end; char varf_ekil(TCoord x1,TCoord y1,TCoord x2,TCoord y2,TCoord* xv,TCoord* yv,int coef) // function varf_ekil(x1, y1, x2, y2: TCoord; var xv, yv: TCoord; coef: integer): boolean; { // begin if((x1!=x2)||(y1!=y2)){ // if(x1<>x2)or(y1<>y2) then begin *xv = round(( coef*(y1-y2)*sqrt3 + x2+x1 ) / 2); // xv := round(( coef*(y1-y2)*sqrt3 + x2+x1 ) / 2); *yv = round(( coef*(x2-x1)*sqrt3 + y2+y1 ) / 2); // yv := round(( coef*(x2-x1)*sqrt3 + y2+y1 ) / 2); return 1; // varf_ekil := true; } else { // end else begin *xv = x1; // xv := x1; *yv = x2; // yv := x2; return 0; // varf_ekil := false; }; // end; }; // end; /*---------------------------------------------------------------------*/ // {---------------------------------------------------------------------} void SetXYPoz(double x, double y) // procedure SetXYPoz(x, y: real); { // begin gx = round(x*mult_exact); // gx := round(x*mult_exact); gy = round(y*mult_exact); // gy := round(y*mult_exact); }; // end; /*---------------------------------------------------------------------*/ // {---------------------------------------------------------------------} void drow_line_to(TCoord x, TCoord y) // procedure drow_line_to(x,y: TCoord); { // begin line(ex+(gx / mult_exact),ey+(gy / mult_exact),ex+(x / mult_exact),ey+(y / mult_exact)); // line(ex+(gx div mult_exact),ey+(gy div mult_exact),ex+(x div mult_exact),ey+(y div mult_exact)); gx = x; // gx := x; gy = y; // gy := y; }; // end; /*---------------------------------------------------------------------*/ // {---------------------------------------------------------------------} void drow_line(TCoord x, TCoord y) // procedure drow_line(x, y: TCoord); // { (x,y) - coordonatele punctului destinatie } { // begin if(n<=0){ // if n<=0 then begin // { Sfarsitul recursiei. Desen: ___ } tx = gx; // tx := gx; { Pastrare pozitia de pornire } ty = gy; // ty := gy; drow_line_to(x, y); // drow_line_to(x, y); { Doar aici se deseneaza linia } } else { // end else begin // { Liniile 1, 2, 3 si 4 vor fi desenate respectiv in felul urmator: _ / \_ } n--; // dec(n); drow_line(raport(gx, x, lamda2), raport(gy, y, lamda2)); // drow_line(raport(gx, x, lamda2), raport(gy, y, lamda2)); { Linia 1 } if(!varf_ekil(gx, gy, raport(gx,x,1), raport(gy,y,1), &tx, &ty, dir)) n =0; // if (not varf_ekil(gx, gy, raport(gx,x,1), raport(gy,y,1), tx, ty, dir)) then n :=0; drow_line(tx, ty); // drow_line(tx, ty); { Linia 2 } drow_line(raport(tx, x, lamda1), raport(ty, y, lamda1)); // drow_line(raport(tx, x, lamda1), raport(ty, y, lamda1)); { Linia 3 } drow_line(x, y); // drow_line(x, y); { Linia 4 } tx = raport(x, tx, lamda3); // tx := raport(x, tx, lamda3); { Pastrare pozitia de pornire } ty = raport(y, ty, lamda3); // ty := raport(y, ty, lamda3); n++; // inc(n); }; // end; // { (tx,ty) - coordonatele punctului de pornire } }; // end; /*---------------------------------------------------------------------*/ // {---------------------------------------------------------------------} void drow_triunghi(TCoord cx, TCoord cy, double r, char n_, int dir_) { // begin n = n_; dir = dir_; // { / _\ } dir = (dir>0)?1:-1; // if (dir>0) then dir := 1 else dir := -1; ex = round(cx); // ex := round(cx); ey = round(cy); // ey := round(cy); SetXYPoz(-dir * r * sqrt3 / 2, dir * r / 2 ); // SetXYPoz(- dir * r * sqrt3 / 2, dir * r / 2 ); drow_line(-gx, gy); // drow_line(-gx, gy); { _ } cx = tx; // cx := tx; cy = ty; // cy := ty; varf_ekil(tx, ty, gx, gy, &tx, &ty, -1); // varf_ekil(tx, ty, gx, gy, tx, ty, -1); drow_line(tx, ty); // drow_line(tx, ty); { \ } drow_line(cx, cy); // drow_line(cx, cy); { / } }; // end; /*---------------------------------------------------------------------*/ // {---------------------------------------------------------------------} int main(void) // { // BEGIN clrscr(); // ClrScr; GD = DETECT; // GD := detect; initgraph(&GD, &GM, ""); // InitGraph(GD, GM, ''); if(graphresult() != grOk) { // if GraphResult <> grOk then begin GM = graphresult(); // GM := GraphResult; closegraph(); // closegraph; printf("Erroare initializare modul grafic!\n"); // Writeln('Erroare initializare modul grafic!'); printf("%d: %s", graphresult(), grapherrormsg(GM)); // Writeln(GraphResult, ': ', GraphErrorMsg(GM)); getch(); // readkey; return 1; // halt(1); }; // end; // { Initializare valori } nr_rec = 3; // nr_rec := 3; directie = 1; // directie := 1; // do{ // repeat clearviewport(); // ClearViewPort; rectangle(0, 0, getmaxx(), getmaxy()); // Rectangle(0, 0, GetMaxX, GetMaxY); sprintf(buf, "nr rec: %02u", nr_rec); outtextxy(10, 10, buf); // OutTextXY(10, 10, 'nr rec: '+chr(nr_rec div 10+48)+chr(nr_rec mod 10+48)); outtextxy((getmaxx()-strlen(comenzi)*8) / 2, getmaxy()-13, comenzi); // OutTextXY((GetMaxX-length(comenzi)*8)div 2, GetMaxY-13, comenzi); // drow_triunghi(getmaxx() / 2, getmaxy() / 2, getmaxy() / 7 * 3, nr_rec, directie); // drow_triunghi(GetMaxX div 2, GetMaxY div 2, GetMaxY div 7 * 3, nr_rec, directie); setfillpattern(Blue01,color); // SetFillPattern(Blue01,color); floodfill(getmaxx() / 2, getmaxy() / 2, getcolor()); // FloodFill(GetMaxX div 2, GetMaxY div 2, getcolor); // ch = getch(); // ch := ReadKey; switch(ch){ // case ch of case '+': if(nr_rec < 12) nr_rec++; break; // '+': if nr_rec < 12 then inc(nr_rec); case '-': if(nr_rec > 0) nr_rec--; break; // '-': if nr_rec > 0 then dec(nr_rec); case '*': directie = -directie; break; // '*': directie := -directie; }; // end; }while( !(ch==27 || ch==13 || upcase(ch)=='X') ); // until (ch = #27) or (ch = #13) or (upcase(ch) = 'X'); { or } // closegraph(); // closegraph; return 0; // halt(0); } // END.