Curs 04 Fractali Motive Iterate

download Curs 04 Fractali Motive Iterate

of 16

Transcript of Curs 04 Fractali Motive Iterate

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    1/16

    1

    Cursul 4

    Fractali pe baz de motive iterate

    Primii fractali studiai, mulimea lui Cantor, ptratul lui Sierpinski, curba lui von Koch,

    .a., au fost construii prin repetarea la infinit a unei anumite reguli de generare, prin care o figu-

    r iniial, numit baz, este nlocuit cu o alta, numit motiv, n care se regsete de mai multe

    ori figura de baz, eventual redus la scar, rotit, simetrizat sau translatat.

    De exemplu, construcia ptratului lui Sierpinski se ncadreaz n aceast schem: n

    fiecare etap nlocuim fiecare ptrat baz cu cte un motiv format din 8 ptrele cu latura

    micorat de trei ori. In etapa urmtoare, fiecare din cele 8 ptrele devine baz pentru aplicarea

    motivului.Analog, construcia curbei lui von Koch se obine prin nlocuirea repetat a fiecrui

    segment vechi cu patru segmente noi, dispuse dup un anumit model.

    Acest procedeu de construcie este foarte asemntor cu creterea organismelor vii, unde,

    de exemplu, fiecare celul (baza) se divide i formeaz alte dou celule (motivul). Spre deosebire

    de fractali, aici numrul de generaii este finit iar regula de generare se poate schimba pe parcurs.

    Un alt exemplu l dau plantele arborescente a cror cretere anual poate fi modelat cu

    urmtoarea regul de generare: pe fiecare ramur verde apare un nod din care pleac una sau mai

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    2/16

    2

    multe ramuri noi. Partea dintre nodul vechi i nodul nou se lemnific i nu mai particip la gene-

    rare.

    In continuare prezentm cteva clase C# care desenaz astfel de fractali. In toate exem-plele date aici baza este un segment iar motivul este format din dou sau mai multe segmente. In

    fiecare etap figura este format din succesiunea segmentelor de baz, ale cror capete sunt

    memorate ntr-o list de tip ComList.

    Toate clasele utilizate sunt dotate cu metoda transforma(refComList li) care par-

    curge lista primit, reconstinue succesiunea segmentelor de baz ale figurii, aplic pe fiecare

    baz motivul i construiete o nou list cu capetele noilor segmente. In final, lista nou este

    returnat prin referin, deci n locul celei vechi.

    1. Curba lui von Koch

    publicclassVonKoch : FractalForm

    {staticdouble theta = Math.PI / 6;staticdouble ro = 0.5 / Math.Cos(theta);staticComplex w = Complex.setRoTheta(ro, theta);staticdouble lambda = 1 / 3.0;

    void transforma(refComList li) //VonKoch{

    ComList rez = newComList();Complex z1, z2, delta;//Segmentul z1_z2 este inlocuit cu//z1_zA, zA_zB, zB_zC si zC_z2, unde//zA=z1 + lambda* (z2 - z1)

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    3/16

    3

    //zB=z1 + w * (z2 - z1))//zC=z1 + (1-lambda) * (z2 - z1).

    rez.nextZet = z1 = li.firstZet;for (z2 = li.nextZet; !li.ended; z2 = li.nextZet){

    delta = z2 - z1;rez.nextZet = z1 + lambda * delta;rez.nextZet = z1 + w * delta;rez.nextZet = z2 - lambda * delta;rez.nextZet = z2;z1 = z2;

    }li = rez;

    }

    void whiteScreen()

    {initScreen();for (int i = imin; i

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    4/16

    4

    Rezultat:

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    5/16

    5

    2. Curba lui von Koch n ptrat

    publicclassVonKoch4 : FractalForm{

    staticComplex i = newComplex(0, 1);staticdouble theta = Math.PI / 4.1;staticdouble ro = 0.5 / Math.Cos(theta);staticComplex w = Complex.setRoTheta(ro, theta);staticdouble lambda = 1 / 2.1;void transforma(refComList li) //VonKoch{

    ComList rez = newComList();Complex z1, z2, delta;//Segmentul z1_z2 este inlocuit cu//z1_zA, zA_zB, zB_zC si zC_z2, unde//zA=z1 + lambda* (z2 - z1)

    //zB=z1 + w * (z2 - z1))//zC=z1 + (1-lambda) * (z2 - z1).rez.nextZet = z1 = li.firstZet;for (z2 = li.nextZet; !li.ended; z2 = li.nextZet){

    delta = z2 - z1;rez.nextZet = z1 + lambda * delta;rez.nextZet = z1 + w * delta;rez.nextZet = z2 - lambda * delta;rez.nextZet = z2;z1 = z2;

    }li = rez;

    }void whiteScreen(){

    initScreen();for (int i = imin; i

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    6/16

    6

    fig.nextZet = 0;for (int k = 0; k

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    7/16

    7

    Rezultat pentru lambda = 1 / 2.0 i theta = Math.PI / 4.0:

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    8/16

    8

    3. Crabul

    publicclassCrab : FractalForm{

    staticComplex i = newComplex(0, 1);staticdouble theta = Math.PI / 4.0;staticdouble ro = 0.5 / Math.Cos(theta);staticComplex w = Complex.setRoTheta(ro, theta);

    void transforma(refComList li) //Crab{

    ComList rez = newComList();Complex z1, z2;//Segmentul z1_z2 este inlocuit cu z1_zA si zA_z2//unde zA=z1+w*(z2-z1).rez.nextZet = z1 = li.firstZet;for (z2 = li.nextZet; !li.ended; z2 = li.nextZet){

    rez.nextZet = z1 + w * (z2 - z1);rez.nextZet = z2;

    z1 = z2;}li = rez;

    }

    void whiteScreen(){

    initScreen();for (int i = imin; i

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    9/16

    9

    void traseaza(ComList li){

    Complex z1 = li.firstZet;

    int n = 1;whiteScreen();setAxis();for (Complex z2 = li.nextZet; !li.ended; z2 = li.nextZet){

    setLine(z1, z2, getColor(++n / 30));z1 = z2;

    }delaySec(0.5);

    }publicoverridevoid makeImage(){

    setXminXmaxYminYmax(-1, 2, -1.5, 1.5);ComList fig = newComList();

    //segmentul initialfig.nextZet = 0;fig.nextZet = 1;for (int k = 0; k < 19; k++){

    transforma(ref fig);traseaza(fig);if (!resetScreen()) return;

    }}

    }

    Rezultat:

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    10/16

    10

    4. Dragonul

    publicclassDragon : FractalForm{

    staticComplex i = newComplex(0, 1);staticdouble theta = Math.PI / 4.0;staticdouble ro = 0.5 / Math.Cos(theta);staticComplex w1 = Complex.setRoTheta(ro, theta);staticComplex w2 = Complex.setRoTheta(ro, -theta);

    void transforma(refComList li) //Dragon{

    ComList rez = newComList();Complex z1, z2, w;//Segmentul z1_z2 este inlocuit cu z1_zA si zA_z2//unde zA=z1+w*(z2-z1) cu w avand in mod//alternativ valoarea w1 sau w2.int sign = 1;rez.nextZet = z1 = li.firstZet;for (z2 = li.nextZet; !li.ended; z2 = li.nextZet){

    sign *= -1;w = (sign > 0 ? w2 : w1);

    rez.nextZet = z1 + w * (z2 - z1);rez.nextZet = z2;z1 = z2;

    }li = rez;

    }void whiteScreen(){

    initScreen();for (int i = imin; i

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    11/16

    11

    void traseaza(ComList li){

    Complex z1 = li.firstZet;

    int n = 1;whiteScreen();setAxis();for (Complex z2 = li.nextZet; !li.ended; z2 = li.nextZet){

    setLine(z1, z2, getColor(++n / 30));z1 = z2;

    }delaySec(0.5);

    }publicoverridevoid makeImage(){

    setXminXmaxYminYmax(-0.5, 1.5, -1, 1);ComList fig = newComList();

    //segmentul initialfig.nextZet = 0;fig.nextZet = 1;for (int k = 0; k < 19; k++){

    transforma(ref fig);traseaza(fig);if (!resetScreen()) return;

    }}

    }

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    12/16

    12

    5. Arbori

    publicclassCopac : FractalForm{

    staticComplex i = newComplex(0, 1);staticdouble roTrunchi = 0.9;staticdouble roRam = 0.6;staticdouble thetaRam = Math.PI / 7;staticComplex omega_stg = Complex.setRoTheta(roRam, thetaRam);staticComplex omega_drp = Complex.setRoTheta(roRam, -thetaRam);

    void transforma(refComList li) //Copac{

    ComList rez = newComList();Complex z0, z1, zV, zA, zB, delta;for (z0 = li.firstZet; !li.ended; z0 = li.nextZet)

    {z1 = li.nextZet;zV = z1 + roTrunchi * (z1 - z0);delta = zV - z1;zA = z1 + omega_stg * delta;zB = z1 + omega_drp * delta;rez.nextZet = z1;rez.nextZet = zA;rez.nextZet = z1;rez.nextZet = zV;rez.nextZet = z1;rez.nextZet = zB;

    }

    li = rez;}

    void whiteScreen(){

    initScreen();for (int i = imin; i

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    13/16

    13

    for (int k = 1; k

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    14/16

    14

    6. Pduri

    Pentru ca arborii desenai de noi s aibe un aspect ct de ct natural trebuie s adaugm

    mai mult culoare i, mai ales, s folosim numere aleatoare pentru a aduga un zgomot de fond

    n procesul de generare. Iat un rezultat obinut detul de uor:

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    15/16

    15

    publicclassPadure : FractalForm{

    void unPom(Complex zz0, Complex deltaz0, int nrEtape,Color colTr, Color colFl)

    {Complex z0, z1, zV, zA, zB, delta, i = newComplex(0, 1);Color col = colTr;double roTrunchi1, roTrunchi = 0.8;double roRam1, roRam = 0.7;double thetaRam1, thetaRam = Math.PI / 15;double thetaTrunchi;Complex omega_stg, omega_drp, omega_tr;

    ComList oldList = newComList();ComList newList = newComList();z0 = zz0;z1 = zz0 + deltaz0;oldList.nextZet = z0;oldList.nextZet = z1;setLine(z0, z1, col);Random nRand = newRandom();for (int k = 1; k

  • 7/30/2019 Curs 04 Fractali Motive Iterate

    16/16

    16

    setLine(z1, zV, col);}if (nRand.NextDouble() < 0.9)

    { newList.nextZet = z1;newList.nextZet = zB;setLine(z1, zB, col);

    }}roTrunchi *= 0.92;oldList = newList;newList = newComList();if (!resetScreen()) return;//delaySec(0.2);

    }for (z1 = oldList.firstZet; !oldList.ended; z1 = oldList.nextZet){

    col = colFl;

    z1 = oldList.nextZet;int ix = getI(z1.Re);int jy = getJ(z1.Im);setPixel(Complex.setReIm(getX(ix), getY(jy)), col);setPixel(Complex.setReIm(getX(ix + 1), getY(jy)), col);setPixel(Complex.setReIm(getX(ix - 1), getY(jy)), col);setPixel(Complex.setReIm(getX(ix), getY(jy + 1)), col);setPixel(Complex.setReIm(getX(ix), getY(jy - 1)), col);//if (!resetScreen()) return;

    }if (!resetScreen()) return;

    }

    publicoverridevoid makeImage(){

    setXminXmaxYminYmax(0, 1, 0, 1);//setAxis();Random nRand = newRandom();Complex z0, delta;Color colTr, colFr;

    for (int n = 0; n < 100; n++){

    z0 = Complex.setReIm(nRand.NextDouble(),nRand.NextDouble() / 2);

    delta = Complex.setReIm((nRand.NextDouble() - 0.5) / 100,0.05 + nRand.NextDouble() / 5);

    colTr = getColor(500 + nRand.Next(100));colFr = getColor(600 + nRand.Next(100));if (nRand.Next(100) < 20) colFr = getColor(200 +

    nRand.Next(100));unPom(z0, delta, 8, colTr, colFr);

    }}

    }