Étape 14 : fractales
Il s’agit d’une fractale interactive : déplacez la souris horizontalement au-dessus de l’image va changer la profondeur de la fractale.
Pour découvrir l’interactivité, suivre http://www.openprocessing.org/sketch/138956
Formule
WIDTH = 700; X_MIN = 0; X_MAX = 1; Y_MIN = 0; Y_MAX = 1; RATIO = AUTO; MOUSE_MOVE = true; // recursion // --------- int sierpinski(u, v, w, h, depth, maxdepth) { int val = ((int)(u/w * 3) % 2) * ((int)(v/h * 3) % 2); if(val==0 && depth<maxdepth) { return sierpinski((u*3 % w)/3, (v*3 % h)/3, w/3, h/3, depth+1, maxdepth); } else return val; } // start the computation and convert 0/1 to color // ---------------------------------------------- color rgb(x, y) { return color(sierpinski(x,y,X_SPAN,Y_SPAN, 1, maxdepth) * 255); } // redraw only on effective maxdepth change // ---------------------------------------- int maxdepth = -1; bool preDraw(t) { int d = (int)(mouseX/width*6) +1; bool doit = maxdepth!=d; maxdepth = d; return doit; }