mercoledì 21 gennaio 2015

Frattali

I frattali sono particolari figure geometriche che rappresentano se stesse qualunque ingrandimento fatto.
Mi sono esercitato un po' con la formula per quello di Mandenlbrot e il risultato è stato il seguente:
Nel caso di Mandrelbrot il calcolo iterativo è dato da P(z+1)= P(z)^2 + P(x,y). Il codice è il seguente:


on mouseUp
   put "1,1" into cc
   put cc & CR into nn
   delete image "frattale"
   create image "frattale"
   put field "grandezza" into kk #deve essere un numero pari
   put field "maxiter" into rip   
   set the width of image frattale to kk
   set the height of image frattale to kk
   #centriamo l'immagine
   set the loc of image "frattale" to the loc of scrollbar "progr"
   set the top of image "frattale" to the bottom of scrollbar "progr"
   set the layer of image "frattale" to 1
   repeat with x= 1 to kk
      repeat with y= 1 to kk
         put x / kk * 4 - 2 into tx
         put y / kk * 4 - 2 into ty
         put fract(tx,ty,rip) & CR after contlist
         add 1 to cont
         set the thumbpos of scrollbar "progr" to (round(100/(kk * kk) * cont) )
      end repeat
   end repeat   
   set the lista of me to contlist #per debug
   repeat for each line tLine in contlist      
      put numtochar(255) after timag
      put numtochar(tLine* 255 / rip) after timag
      put numtochar(tLine*255 / rip) after timag
      put numtochar(tLine*255 / rip) after timag      
   end repeat
   set the imagedata of image "frattale" to timag
   #answer "fatto"
end mouseUp

function fract x,y,rip
   put x & comma & y into cc
   put "0,0" into xy
   put 0 into d
   put 0 into cont
   repeat while d < 2
      add 1 to cont      
      if cont > rip then
         put 0 into cont
         exit repeat
      end if
      put compadd(compmult(xy,xy), cc) into xy
      put item 1 of xy into xx
      put item 2 of xy into yy
      put sqrt(xx^2 + yy^2) into d         
   end repeat
   return cont
end fract

function compadd aa,bb
   put item 1 of aa into aax
   put item 2 of aa into aay
   put item 1 of bb into bbx
   put item 2 of bb into bby
   put (aax + bbx) , (aay + bby) into temp
   return temp
end compadd


function compmult aa,bb
   put item 1 of aa into aax
   put item 2 of aa into aay
   put item 1 of bb into bbx
   put item 2 of bb into bby
   put (aax * bbx - aay * bby) , (aax * bby + aay * bbx) into temp   
   return temp
end compmult




Il programma completo lo potete scaricare da qui. Non è che sia entusiasta della velocità di calcolo ottenuta, voi riuscireste a migliorare il codice?

1 commento:

  1. Bello
    In effetti è lento ma se metti in "lock screen" con il cursore a orologio guadagni molto molto tempo
    Marvi

    RispondiElimina