beginShape(), Vertex() et endShape()
La fonction Vertex() s’utilise à l’intérieur de deux fonctions : beginShape() et endShape(), qui servent à délimiter le dessin.
beginShape();
vertex(20,20);
vertex(780,20);
endShape();
}vertex() définit un point dans l’espace avec deux arguments en x et y (dans le cas d’un espace 2D). Parce qu’elles sont écrites entre beginShape() et endShape(), les différents vertex() sont reliés entre eux. On peut relier autant de points que l’on veut.
On peut aussi fermer la forme de manière automatique en rajoutant un argument dans endShape(), comme ceci :
beginShape();
vertex(20,20);
vertex(780,20);
vertex(750,750);
vertex(20,780);
endShape(CLOSE);
Avec un peu de random, ça donnerait ça :
fill(255,0,0);
beginShape();
for(int i=0;i<40;i++){
vertex(random(20,width-20), random(20,height-20));
}
endShape(CLOSE);
ou encore :
fill(0);
noStroke();
for (int x=15; x<width-10; x+=30) {
for (int y=15; y<height-10; y+=30) {
beginShape();
vertex(random(x-5, x+5), random(y-5, y+5));
vertex(random(x+15, x+25), random(y-5, y+5));
vertex(random(x+15, x+25), random(y+15, y+25));
vertex(random(x-5, x+5), random(y+15, y+25));
endShape(CLOSE);
}
}