Cours2 4.Simulation: marcheur aléatoire, distribution non uniforme - picardlimpens/TechArtsNum GitHub Wiki
// On définit des variables
int positionx ,positiony, positionx_old, positiony_old;
int directionx, directiony, taille;
void setup(){
background(255); // on donne un fond blanc
size(500,300); // on choisi la taille du sketch
smooth(); // on ameliore le rendu (en option)
// on place notre future ellipse au centre de l'ecran
positionx = width/2;
positionx_old = width/2;
positiony = height/2;
positiony_old = height/2;
}
void draw(){
// Un fondu pour l'arière plan
fill(255,3);
noStroke();
rect(0,0,width,height);
// On choisit au hasard un mouvement pour x et y compris entre -1 et 1
//directionx = int(random(-2, 2))*4;
//directiony = int(random(-2, 2))*4;
float r = random(1);
if (r < 0.3) { // 30% de chance d'avoir un large pas
directionx = int(random(-2,2))*2;
directiony = int(random(-2,2))*2;
} else if (r>=0.3 && r < 0.5) { // 20% de chance d'avoir un très large pas
directionx = int(random(-2,2))*10;
directiony = int(random(-2,2))*10;
} else {
directionx = int(random(-2, 2));
directiony = int(random(-2, 2));
}
// La nouvelle position de l'ellispe est egale a son ancienne position (x et y) + un int entre -1 et 1
positiony = positiony + directiony;
positionx = positionx + directionx;
positionx = constrain(positionx,0,width);
positiony = constrain(positiony,0,height);
stroke(0);
strokeWeight(2);
point(positionx, positiony);
}