float x, y, dx, dy; void avance(float d) { //avance d'une distance d dans la direction du vecteur vitesse float xSuiv = x + d * dx; // calcul de la position suivantedans deux variables locales xSuiv, ySuiv float ySuiv = y + d * dy; line(x, y, xSuiv, ySuiv); // tracé d'un segment x = xSuiv; // mise à jour de la position y = ySuiv; } void av (float d) { avance(d); } void re (float d) { avance(-d); } void droite(float a) { // mise à jour du vecteur vitesse par rotation d'angle a. // Attention à l'orientation des axes informatiques ! float cosa = cos(a); // Pour ne pas les recalculer ! float sina = sin(a); float dxSuiv = dx * cosa -dy * sina; float dySuiv = dx * sina + dy * cosa; dx = dxSuiv; dy = dySuiv; } void td (float a) { droite(a); } void tg (float a) { droite(-a); } void mt () { droite(PI/2); avance (10); tg (2*PI/3); avance (20); tg (2*PI/3); avance (20); tg (2*PI/3); avance (10); tg (PI/2); } void vonKoch(float t, int n) { if (n == 0) { av (t); return; } vonKoch(t/3, n-1); td(PI/3); vonKoch(t/3, n-1); tg(2*PI/3); vonKoch(t/3, n-1); td(PI/3); vonKoch(t/3, n-1); } int n =0; void setup() { size(180, 180); frameRate(1); } void draw() { background(80, 80, 80); stroke(0, 160, 255); x = 15; y =133; // point de départ dx = 1; dy = 0; // vitesse initiale vers la droite vonKoch(150, n); tg(2*PI/3); vonKoch(150, n); tg(2*PI/3); vonKoch(150, n); tg(2*PI/3); n = (n+1)%6; }